~sil2100/unity-2d/precise-security

« back to all changes in this revision

Viewing changes to libunity-2d-private/src/unity2dtr.cpp

  • Committer: Aurelien Gateau
  • Date: 2010-11-10 08:57:29 UTC
  • mto: This revision was merged to the branch mainline in revision 284.
  • Revision ID: aurelien.gateau@canonical.com-20101110085729-fl1ye7impkqhm0w6
Added a section about const correct-ness

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is part of unity-2d
3
 
 *
4
 
 * Copyright 2011 Canonical Ltd.
5
 
 *
6
 
 * Authors:
7
 
 * - Aurélien Gâteau <aurelien.gateau@canonical.com>
8
 
 *
9
 
 * This program is free software; you can redistribute it and/or modify
10
 
 * it under the terms of the GNU General Public License as published by
11
 
 * the Free Software Foundation; version 3.
12
 
 *
13
 
 * This program is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
 * GNU General Public License for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU General Public License
19
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
 */
21
 
// Self
22
 
#include <unity2dtr.h>
23
 
 
24
 
// local
25
 
#include <config.h>
26
 
 
27
 
// Qt
28
 
#include <QDeclarativeContext>
29
 
 
30
 
// libc
31
 
#include <libintl.h>
32
 
 
33
 
namespace Unity2dTr
34
 
{
35
 
 
36
 
void init(const char* domain, const char* localeDir)
37
 
{
38
 
    setlocale(LC_ALL, "");
39
 
    bindtextdomain(domain, localeDir);
40
 
    textdomain(domain);
41
 
}
42
 
 
43
 
void qmlInit(QDeclarativeContext* context)
44
 
{
45
 
    static QmlHelper helper;
46
 
    context->setContextProperty("u2d", &helper);
47
 
}
48
 
 
49
 
QString QmlHelper::tr(const QString& text, const QString& domain)
50
 
{
51
 
    if (domain.isNull()) {
52
 
        return ::u2dTr(text.toUtf8().constData());
53
 
    } else {
54
 
        return ::u2dTr(text.toUtf8().constData(), domain.toUtf8().constData());
55
 
    }
56
 
}
57
 
 
58
 
QString QmlHelper::tr(const QString& singular, const QString& plural, int n, const QString& domain)
59
 
{
60
 
    if (domain.isNull()) {
61
 
        return ::u2dTr(singular.toUtf8().constData(), plural.toUtf8().constData(), n);
62
 
    } else {
63
 
        return ::u2dTr(singular.toUtf8().constData(), plural.toUtf8().constData(), n, domain.toUtf8().constData());
64
 
    }
65
 
}
66
 
 
67
 
} // namespace
68
 
 
69
 
QString u2dTr(const char* text, const char* domain)
70
 
{
71
 
    return QString::fromUtf8(dgettext(domain, text));
72
 
}
73
 
 
74
 
QString u2dTr(const char* singular, const char* plural, int n, const char* domain)
75
 
{
76
 
    QString text = QString::fromUtf8(dngettext(domain, singular, plural, n));
77
 
    // Note: if `text` is "%%n" (meaning the string on screen should be "%n"
78
 
    // literally), this will fail. I think we don't care for now.
79
 
    text.replace("%n", QString::number(n));
80
 
    return text;
81
 
}
82
 
 
83
 
#include <unity2dtr.moc>