2
* Copyright 2012-2015 Canonical Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Author: Tim Peeters <tim.peeters@canonical.om>
24
#include <glib/gi18n.h>
29
#include <QtCore/QDir>
31
#include "timeutils_p.h"
36
* \inqmlmodule Ubuntu.Components
38
* \brief i18n is a context property that provides internationalization support.
40
* i18n cannot be instantiated, and is already available as a context property.
41
* It is based on \l {https://www.gnu.org/software/gettext/} {gettext}, and thus the standard gettext tools can be used for translating
45
* import Ubuntu.Components 0.1
49
* height: units.gu(50)
52
* anchors.centerIn: parent
53
* text: i18n.tr("Press me")
58
UbuntuI18n *UbuntuI18n::m_i18 = nullptr;
60
UbuntuI18n::UbuntuI18n(QObject* parent) : QObject(parent)
64
* category = LC_ALL: All types of i18n: LC_MESSAGES, LC_NUMERIC, LC_TIME
65
* locale = "": Lookup the environment for $LC_ALL, $LC_* and $LANG in that order
66
* Returns: for example en_US.utf8, da_DK or POSIX
68
* Note: $LANGUAGE is implicitly respected by gettext() calls and
69
* defines the order of multiple locales
71
m_language = QString::fromLocal8Bit(setlocale(LC_ALL, ""));
74
UbuntuI18n::~UbuntuI18n()
80
* \qmlproperty string i18n::domain
81
* The gettext domain to be used for the translation. The default domain
82
* is the applicationName specified in the application's \l MainView, or the empty string "" if
83
* no applicationName was given or no \l MainView is used.
84
* Use dtr() functions instead of tr() to use a different domain for a single translation
85
* that ignores i18n.domain.
87
QString UbuntuI18n::domain() const {
92
* \qmlproperty string i18n::language
93
* The language that is used for the translation. The default value is
94
* the user's locale dending on $LC_ALL, $LC_MESSAGES and $LANG at the time
95
* of running the application. See the gettext manual for details.
97
QString UbuntuI18n::language() const {
102
* \qmlmethod void i18n::bindtextdomain(string domain_name, string dir_name)
103
* Specify that the domain_name message catalog can be found
104
* in dir_name rather than in the system locale data base.
106
void UbuntuI18n::bindtextdomain(const QString& domain_name, const QString& dir_name) {
107
C::bindtextdomain(domain_name.toUtf8(), dir_name.toUtf8());
108
Q_EMIT domainChanged();
111
void UbuntuI18n::setDomain(const QString &domain) {
112
if (m_domain == domain)
116
C::textdomain(domain.toUtf8());
118
The default is /usr/share/locale if we don't set a folder
119
For click we use APP_DIR/share/locale
120
e.g. /usr/share/click/preinstalled/com.example.foo/current/share/locale
122
QString appDir = QString::fromLocal8Bit(getenv("APP_DIR"));
123
if (!QDir::isAbsolutePath (appDir)) {
124
appDir = QStringLiteral("/usr");
126
QString localePath(QDir(appDir).filePath(QStringLiteral("share/locale")));
127
C::bindtextdomain(domain.toUtf8(), localePath.toUtf8());
128
Q_EMIT domainChanged();
131
void UbuntuI18n::setLanguage(const QString &lang) {
132
if (m_language == lang)
138
This is needed for LP: #1263163.
140
LANGUAGE may be set to one or more languages for example "fi" or
141
"sv:de". gettext prioritizes LANGUAGE over LC_ALL, LC_*, and
142
LANG, so if the session has already set LANGUAGE, calls to
143
gettext will only use that. We must override it here so that
144
future calls to gettext are done in the new language.
146
This only affects the current process. It does not override the
147
user's session LANGUAGE.
149
setenv("LANGUAGE", lang.toUtf8().constData(), 1);
152
The inverse form of setlocale as used in the constructor, passing
153
a valid locale string updates all category type defaults.
155
setlocale(LC_ALL, lang.toUtf8());
156
Q_EMIT languageChanged();
160
* \qmlmethod string i18n::tr(string text)
161
* Translate \a text using gettext and return the translation.
163
QString UbuntuI18n::tr(const QString& text)
165
return QString::fromUtf8(C::gettext(text.toUtf8()));
169
* \qmlmethod string i18n::tr(string singular, string plural, int n)
170
* Translate the given input string \a singular or \a plural (depending on the number of items \a n)
172
* Note that tr() does not automatically insert the values in the QString,
173
* so it should be called like this:
174
* tr("%1 file", "%1 files", count).arg(count)
176
QString UbuntuI18n::tr(const QString &singular, const QString &plural, int n)
178
return QString::fromUtf8(C::ngettext(singular.toUtf8(), plural.toUtf8(), n));
182
* \qmlmethod string i18n::dtr(string domain, string text)
183
* Translate \a text using gettext. Uses the specified domain \a domain instead of i18n.domain.
185
QString UbuntuI18n::dtr(const QString& domain, const QString& text)
187
if (domain.isNull()) {
188
return QString::fromUtf8(C::dgettext(NULL, text.toUtf8()));
190
return QString::fromUtf8(C::dgettext(domain.toUtf8(), text.toUtf8()));
195
* \qmlmethod string i18n::dtr(string domain, string singular, string plural, int n)
196
* Translate the given text using gettext. Should be called like this:
197
* tr(domain, "%1 file", "%1 files", count).arg(count)
198
* Uses \a domain for the domain instead of i18n.domain, and \a singular or \a plural
199
* as input for the translation depending on the number of items \a n.
201
QString UbuntuI18n::dtr(const QString& domain, const QString& singular, const QString& plural, int n)
203
if (domain.isNull()) {
204
return QString::fromUtf8(C::dngettext(NULL, singular.toUtf8(), plural.toUtf8(), n));
206
return QString::fromUtf8(C::dngettext(domain.toUtf8(), singular.toUtf8(), plural.toUtf8(), n));
211
* \qmlmethod string i18n::ctr(string context, string text)
212
* Translate \a text using gettext and return the translation.
213
* \a context is only visible to the translator and helps disambiguating for very short texts
215
QString UbuntuI18n::ctr(const QString& context, const QString& text)
217
return dctr(QString(), context, text);
221
* \qmlmethod string i18n::dctr(string domain, string context, string text)
222
* Translate \a text using gettext. Uses the specified domain \a domain instead of i18n.domain.
223
* \a context is only visible to the translator and helps disambiguating for very short texts
225
QString UbuntuI18n::dctr(const QString& domain, const QString& context, const QString& text)
227
if (domain.isNull()) {
228
return QString::fromUtf8(C::g_dpgettext2(NULL, context.toUtf8(), text.toUtf8()));
230
return QString::fromUtf8(C::g_dpgettext2(domain.toUtf8(), context.toUtf8(), text.toUtf8()));
235
* \qmlmethod string i18n::tag(string text)
236
* Mark \a text for translation at a later point. Typically this allows an API
237
* to take the original string and pass it to dtr (or dgettext).
241
* import UserMetrics 0.1
245
* format: i18n.tag("Distance covered today: %1 km")
246
* emptyFormat: i18n.tag("No running today")
247
* domain: "runner.forest"
251
* The strings tagged for localzation above are passed to the implementation
252
* of UserMetrics verbatim, as well as the domain of the app. Display and
253
* translation of the strings will happen in the lockscreen, where the same
254
* strings will be passed to i18n.tr.
256
QString UbuntuI18n::tag(const QString& text)
262
* \qmlmethod string i18n::tag(string context, string text)
263
* Mark \a text for translation at a later point. Typically this allows an API
264
* to take the original string and pass it to dctr (or g_dpgettext2).
265
* \a context is only visible to the translator and helps disambiguating for very short texts
267
QString UbuntuI18n::tag(const QString& context, const QString& text)
273
* \qmlmethod string i18n::relativeDateTime(datetime dateTime)
274
* Translate a datetime based on proximity to current time.
276
QString UbuntuI18n::relativeDateTime(const QDateTime& datetime)
278
static const QString ubuntuUiToolkit = QStringLiteral("ubuntu-ui-toolkit");
280
QDateTime relativeTo(QDateTime::currentDateTime());
281
const date_proximity_t prox = getDateProximity(relativeTo, datetime);
284
case DATE_PROXIMITY_NOW:
285
/* TRANSLATORS: Time based "this is happening/happened now" */
286
return dtr(ubuntuUiToolkit, QStringLiteral("Now"));
288
case DATE_PROXIMITY_HOUR:
290
qint64 diff = datetime.toMSecsSinceEpoch() - relativeTo.toMSecsSinceEpoch();
291
qint64 minutes = qRound(float(diff) / 60000);
293
return dtr(ubuntuUiToolkit, QStringLiteral("%1 minute ago"),
294
QStringLiteral("%1 minutes ago"), qAbs(minutes)).arg(qAbs(minutes));
296
return dtr(ubuntuUiToolkit, QStringLiteral("%1 minute"),
297
QStringLiteral("%1 minutes"), minutes).arg(minutes);
300
case DATE_PROXIMITY_TODAY:
301
/* en_US example: "1:00 PM" */
302
/* TRANSLATORS: Please translate these to your locale datetime
303
format using the format specified by
304
https://qt-project.org/doc/qt-5-snapshot/qdatetime.html#fromString-2 */
305
return datetime.toString(isLocale12h()
306
? dtr(ubuntuUiToolkit, QStringLiteral("h:mm ap"))
307
/* TRANSLATORS: Please translate these to your locale datetime
308
format using the format specified by
309
https://qt-project.org/doc/qt-5-snapshot/qdatetime.html#fromString-2 */
310
: dtr(ubuntuUiToolkit, QStringLiteral("HH:mm")));
312
case DATE_PROXIMITY_YESTERDAY:
313
/* en_US example: "Yesterday 13:00" */
314
/* TRANSLATORS: Please translate these to your locale datetime
315
format using the format specified by
316
https://qt-project.org/doc/qt-5-snapshot/qdatetime.html#fromString-2 */
317
return datetime.toString(isLocale12h()
318
? dtr(ubuntuUiToolkit, QStringLiteral("'Yesterday\u2003'h:mm ap"))
319
/* TRANSLATORS: Please translate these to your locale datetime
320
format using the format specified by
321
https://qt-project.org/doc/qt-5-snapshot/qdatetime.html#fromString-2 */
322
: dtr(ubuntuUiToolkit, QStringLiteral("'Yesterday\u2003'HH:mm")));
324
case DATE_PROXIMITY_TOMORROW:
325
/* en_US example: "Tomorrow 1:00 PM" */
326
/* TRANSLATORS: Please translate these to your locale datetime
327
format using the format specified by
328
https://qt-project.org/doc/qt-5-snapshot/qdatetime.html#fromString-2 */
329
return datetime.toString(isLocale12h()
330
? dtr(ubuntuUiToolkit, QStringLiteral("'Tomorrow\u2003'h:mm ap"))
331
/* TRANSLATORS: Please translate these to your locale datetime
332
format using the format specified by
333
https://qt-project.org/doc/qt-5-snapshot/qdatetime.html#fromString-2 */
334
: dtr(ubuntuUiToolkit, QStringLiteral("'Tomorrow\u2003'HH:mm")));
336
case DATE_PROXIMITY_LAST_WEEK:
337
case DATE_PROXIMITY_NEXT_WEEK:
338
/* en_US example: "Fri 1:00 PM" */
339
/* TRANSLATORS: Please translate these to your locale datetime
340
format using the format specified by
341
https://qt-project.org/doc/qt-5-snapshot/qdatetime.html#fromString-2 */
342
return datetime.toString(isLocale12h()
343
? dtr(ubuntuUiToolkit, QStringLiteral("ddd'\u2003'h:mm ap"))
344
/* TRANSLATORS: Please translate these to your locale datetime
345
format using the format specified by
346
https://qt-project.org/doc/qt-5-snapshot/qdatetime.html#fromString-2 */
347
: dtr(ubuntuUiToolkit, QStringLiteral("ddd'\u2003'HH:mm")));
349
case DATE_PROXIMITY_FAR_BACK:
350
case DATE_PROXIMITY_FAR_FORWARD:
352
/* TRANSLATORS: Please translate these to your locale datetime
353
format using the format specified by
354
https://qt-project.org/doc/qt-5-snapshot/qdatetime.html#fromString-2 */
355
return datetime.toString(isLocale12h()
356
? dtr(ubuntuUiToolkit, QStringLiteral("ddd d MMM'\u2003'h:mm ap"))
357
/* TRANSLATORS: Please translate these to your locale datetime
358
format using the format specified by
359
https://qt-project.org/doc/qt-5-snapshot/qdatetime.html#fromString-2 */
360
: dtr(ubuntuUiToolkit, QStringLiteral("ddd d MMM'\u2003'HH:mm")));
362
return datetime.toString(Qt::DefaultLocaleShortDate);