File: kdelocalizer.cpp

package info (click to toggle)
kf6-ktexttemplate 6.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,272 kB
  • sloc: cpp: 19,464; javascript: 6,043; python: 297; ruby: 24; makefile: 5
file content (195 lines) | stat: -rw-r--r-- 5,924 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
  This file is part of the KTextTemplate library

  SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>

  SPDX-License-Identifier: LGPL-2.1-or-later

*/

#include "kdelocalizer.h"

#include <KDE/KComponentData>
#include <KDE/KGlobal>
#include <KDE/KLocale>
#include <KDE/KLocalizedString>
#include <kcurrencycode.h>
#include <kstandarddirs.h>

#include <QStack>

class KDELocalizerPrivate
{
    KDELocalizerPrivate(KDELocalizer *qq, KLocale *locale)
        : q_ptr(qq)
        , m_locale(locale ? locale : KGlobal::locale())
    {
    }
    Q_DECLARE_PUBLIC(KDELocalizer)
    KDELocalizer *const q_ptr;

    QStringList m_applicationCatalogs;
    QStringList m_catalogs;
    QStack<KLocale *> m_localeStack;
    KLocale *m_locale;
};

KDELocalizer::KDELocalizer(KLocale *locale)
    : d_ptr(new KDELocalizerPrivate(this, locale))
{
}

KDELocalizer::~KDELocalizer()
{
    delete d_ptr;
}

void KDELocalizer::insertApplicationCatalogs(const QStringList &catalogs)
{
    Q_D(KDELocalizer);
    d->m_applicationCatalogs = catalogs;
    for (const QString &catalog : d->m_applicationCatalogs) {
        d->m_locale->insertCatalog(catalog);
    }
}

QString KDELocalizer::currentLocale() const
{
    Q_D(const KDELocalizer);
    return d->m_locale->language();
}

void KDELocalizer::pushLocale(const QString &localeName)
{
    Q_D(KDELocalizer);
    d->m_localeStack.push(d->m_locale);
    const QStringList parts = localeName.split("_");
    QString country;
    if (parts.size() == 2)
        country = parts.at(1).toLower();
    d->m_locale = new KLocale(KGlobal::mainComponent().catalogName(), localeName, country);
    for (const QString &catalog : d->m_applicationCatalogs) {
        d->m_locale->insertCatalog(catalog);
    }
    for (const QString &catalog : d->m_catalogs) {
        d->m_locale->insertCatalog(catalog);
    }
}

void KDELocalizer::popLocale()
{
    Q_D(KDELocalizer);
    Q_ASSERT(d->m_locale);
    delete d->m_locale;
    d->m_locale = d->m_localeStack.top();
    d->m_localeStack.pop();
}

void KDELocalizer::loadCatalog(const QString &path, const QString &catalog)
{
    Q_D(KDELocalizer);
    KGlobal::dirs()->addResourceDir("locale", path, true);
    d->m_catalogs.append(catalog);
    Q_ASSERT(d->m_localeStack.isEmpty());
    d->m_locale->insertCatalog(catalog);
}

void KDELocalizer::unloadCatalog(const QString &catalog)
{
    Q_D(KDELocalizer);
    //   Does not exist (probably not necessary either):
    // KGlobal::dirs()->removeResourceDir("locale", path + '/' + catalog, true);
    d->m_catalogs.removeAll(catalog);
    d->m_locale->removeCatalog(catalog);
}

QString KDELocalizer::localizeDate(const QDate &date, QLocale::FormatType formatType) const
{
    Q_D(const KDELocalizer);
    KLocale::DateFormat klocaleFormat = KLocale::ShortDate;
    if (formatType == QLocale::LongFormat)
        klocaleFormat = KLocale::LongDate;

    return d->m_locale->formatDate(date, klocaleFormat);
}

QString KDELocalizer::localizeTime(const QTime &time, QLocale::FormatType formatType) const
{
    Q_D(const KDELocalizer);
    KLocale::TimeFormatOptions klocaleFormat = KLocale::TimeDefault;
    if (formatType == QLocale::ShortFormat)
        klocaleFormat = KLocale::TimeWithoutSeconds;

    return d->m_locale->formatLocaleTime(time, klocaleFormat);
}

QString KDELocalizer::localizeDateTime(const QDateTime &dateTime, QLocale::FormatType formatType) const
{
    Q_D(const KDELocalizer);
    KLocale::DateFormat klocaleFormat = KLocale::ShortDate;
    if (formatType == QLocale::LongFormat)
        klocaleFormat = KLocale::LongDate;

    return d->m_locale->formatDateTime(dateTime, klocaleFormat);
}

QString KDELocalizer::localizeMonetaryValue(qreal value, const QString &currencyCode) const
{
    Q_D(const KDELocalizer);
    KCurrencyCode code(currencyCode);
    return d->m_locale->formatMoney(value, code.defaultSymbol());
}

QString KDELocalizer::localizeNumber(int number) const
{
    Q_D(const KDELocalizer);
    return d->m_locale->formatLong(number);
}

QString KDELocalizer::localizeNumber(qreal number) const
{
    Q_D(const KDELocalizer);
    return d->m_locale->formatNumber(number);
}

static KLocalizedString substituteArguments(const KLocalizedString &_string, const QVariantList &args)
{
    KLocalizedString string = _string;
    for (const QVariant &arg : args) {
        if (arg.type() == QMetaType::Int)
            string = string.subs(arg.toInt());
        else if (arg.type() == QMetaType::Double)
            string = string.subs(arg.toDouble());
        else
            string = string.subs(arg.toString());
    }
    return string;
}

QString KDELocalizer::localizeContextString(const QString &string, const QString &context, const QVariantList &arguments) const
{
    Q_D(const KDELocalizer);
    KLocalizedString localizedString = ki18nc(context.toUtf8(), string.toUtf8());
    return substituteArguments(localizedString, arguments).toString(d->m_locale);
}

QString KDELocalizer::localizePluralContextString(const QString &string, const QString &pluralForm, const QString &context, const QVariantList &arguments) const
{
    Q_D(const KDELocalizer);
    KLocalizedString localizedString = ki18ncp(context.toUtf8(), string.toUtf8(), pluralForm.toUtf8());
    return substituteArguments(localizedString, arguments).toString(d->m_locale);
}

QString KDELocalizer::localizePluralString(const QString &string, const QString &pluralForm, const QVariantList &arguments) const
{
    Q_D(const KDELocalizer);
    KLocalizedString localizedString = ki18np(string.toUtf8(), pluralForm.toUtf8());
    return substituteArguments(localizedString, arguments).toString(d->m_locale);
}

QString KDELocalizer::localizeString(const QString &string, const QVariantList &arguments) const
{
    Q_D(const KDELocalizer);
    KLocalizedString localizedString = ki18n(string.toUtf8());
    return substituteArguments(localizedString, arguments).toString(d->m_locale);
}