File indexing completed on 2024-12-22 03:46:49
0001 /* 0002 This file is part of the KTextTemplate library 0003 0004 SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com> 0005 0006 SPDX-License-Identifier: LGPL-2.1-or-later 0007 0008 */ 0009 0010 #include "kdelocalizer.h" 0011 0012 #include <KDE/KComponentData> 0013 #include <KDE/KGlobal> 0014 #include <KDE/KLocale> 0015 #include <KDE/KLocalizedString> 0016 #include <kcurrencycode.h> 0017 #include <kstandarddirs.h> 0018 0019 #include <QStack> 0020 0021 class KDELocalizerPrivate 0022 { 0023 KDELocalizerPrivate(KDELocalizer *qq, KLocale *locale) 0024 : q_ptr(qq) 0025 , m_locale(locale ? locale : KGlobal::locale()) 0026 { 0027 } 0028 Q_DECLARE_PUBLIC(KDELocalizer) 0029 KDELocalizer *const q_ptr; 0030 0031 QStringList m_applicationCatalogs; 0032 QStringList m_catalogs; 0033 QStack<KLocale *> m_localeStack; 0034 KLocale *m_locale; 0035 }; 0036 0037 KDELocalizer::KDELocalizer(KLocale *locale) 0038 : d_ptr(new KDELocalizerPrivate(this, locale)) 0039 { 0040 } 0041 0042 KDELocalizer::~KDELocalizer() 0043 { 0044 delete d_ptr; 0045 } 0046 0047 void KDELocalizer::insertApplicationCatalogs(const QStringList &catalogs) 0048 { 0049 Q_D(KDELocalizer); 0050 d->m_applicationCatalogs = catalogs; 0051 for (const QString &catalog : d->m_applicationCatalogs) { 0052 d->m_locale->insertCatalog(catalog); 0053 } 0054 } 0055 0056 QString KDELocalizer::currentLocale() const 0057 { 0058 Q_D(const KDELocalizer); 0059 return d->m_locale->language(); 0060 } 0061 0062 void KDELocalizer::pushLocale(const QString &localeName) 0063 { 0064 Q_D(KDELocalizer); 0065 d->m_localeStack.push(d->m_locale); 0066 const QStringList parts = localeName.split("_"); 0067 QString country; 0068 if (parts.size() == 2) 0069 country = parts.at(1).toLower(); 0070 d->m_locale = new KLocale(KGlobal::mainComponent().catalogName(), localeName, country); 0071 for (const QString &catalog : d->m_applicationCatalogs) { 0072 d->m_locale->insertCatalog(catalog); 0073 } 0074 for (const QString &catalog : d->m_catalogs) { 0075 d->m_locale->insertCatalog(catalog); 0076 } 0077 } 0078 0079 void KDELocalizer::popLocale() 0080 { 0081 Q_D(KDELocalizer); 0082 Q_ASSERT(d->m_locale); 0083 delete d->m_locale; 0084 d->m_locale = d->m_localeStack.top(); 0085 d->m_localeStack.pop(); 0086 } 0087 0088 void KDELocalizer::loadCatalog(const QString &path, const QString &catalog) 0089 { 0090 Q_D(KDELocalizer); 0091 KGlobal::dirs()->addResourceDir("locale", path, true); 0092 d->m_catalogs.append(catalog); 0093 Q_ASSERT(d->m_localeStack.isEmpty()); 0094 d->m_locale->insertCatalog(catalog); 0095 } 0096 0097 void KDELocalizer::unloadCatalog(const QString &catalog) 0098 { 0099 Q_D(KDELocalizer); 0100 // Does not exist (probably not necessary either): 0101 // KGlobal::dirs()->removeResourceDir("locale", path + '/' + catalog, true); 0102 d->m_catalogs.removeAll(catalog); 0103 d->m_locale->removeCatalog(catalog); 0104 } 0105 0106 QString KDELocalizer::localizeDate(const QDate &date, QLocale::FormatType formatType) const 0107 { 0108 Q_D(const KDELocalizer); 0109 KLocale::DateFormat klocaleFormat = KLocale::ShortDate; 0110 if (formatType == QLocale::LongFormat) 0111 klocaleFormat = KLocale::LongDate; 0112 0113 return d->m_locale->formatDate(date, klocaleFormat); 0114 } 0115 0116 QString KDELocalizer::localizeTime(const QTime &time, QLocale::FormatType formatType) const 0117 { 0118 Q_D(const KDELocalizer); 0119 KLocale::TimeFormatOptions klocaleFormat = KLocale::TimeDefault; 0120 if (formatType == QLocale::ShortFormat) 0121 klocaleFormat = KLocale::TimeWithoutSeconds; 0122 0123 return d->m_locale->formatLocaleTime(time, klocaleFormat); 0124 } 0125 0126 QString KDELocalizer::localizeDateTime(const QDateTime &dateTime, QLocale::FormatType formatType) const 0127 { 0128 Q_D(const KDELocalizer); 0129 KLocale::DateFormat klocaleFormat = KLocale::ShortDate; 0130 if (formatType == QLocale::LongFormat) 0131 klocaleFormat = KLocale::LongDate; 0132 0133 return d->m_locale->formatDateTime(dateTime, klocaleFormat); 0134 } 0135 0136 QString KDELocalizer::localizeMonetaryValue(qreal value, const QString ¤cyCode) const 0137 { 0138 Q_D(const KDELocalizer); 0139 KCurrencyCode code(currencyCode); 0140 return d->m_locale->formatMoney(value, code.defaultSymbol()); 0141 } 0142 0143 QString KDELocalizer::localizeNumber(int number) const 0144 { 0145 Q_D(const KDELocalizer); 0146 return d->m_locale->formatLong(number); 0147 } 0148 0149 QString KDELocalizer::localizeNumber(qreal number) const 0150 { 0151 Q_D(const KDELocalizer); 0152 return d->m_locale->formatNumber(number); 0153 } 0154 0155 static KLocalizedString substituteArguments(const KLocalizedString &_string, const QVariantList &args) 0156 { 0157 KLocalizedString string = _string; 0158 for (const QVariant &arg : args) { 0159 if (arg.type() == QMetaType::Int) 0160 string = string.subs(arg.toInt()); 0161 else if (arg.type() == QMetaType::Double) 0162 string = string.subs(arg.toDouble()); 0163 else 0164 string = string.subs(arg.toString()); 0165 } 0166 return string; 0167 } 0168 0169 QString KDELocalizer::localizeContextString(const QString &string, const QString &context, const QVariantList &arguments) const 0170 { 0171 Q_D(const KDELocalizer); 0172 KLocalizedString localizedString = ki18nc(context.toUtf8(), string.toUtf8()); 0173 return substituteArguments(localizedString, arguments).toString(d->m_locale); 0174 } 0175 0176 QString KDELocalizer::localizePluralContextString(const QString &string, const QString &pluralForm, const QString &context, const QVariantList &arguments) const 0177 { 0178 Q_D(const KDELocalizer); 0179 KLocalizedString localizedString = ki18ncp(context.toUtf8(), string.toUtf8(), pluralForm.toUtf8()); 0180 return substituteArguments(localizedString, arguments).toString(d->m_locale); 0181 } 0182 0183 QString KDELocalizer::localizePluralString(const QString &string, const QString &pluralForm, const QVariantList &arguments) const 0184 { 0185 Q_D(const KDELocalizer); 0186 KLocalizedString localizedString = ki18np(string.toUtf8(), pluralForm.toUtf8()); 0187 return substituteArguments(localizedString, arguments).toString(d->m_locale); 0188 } 0189 0190 QString KDELocalizer::localizeString(const QString &string, const QVariantList &arguments) const 0191 { 0192 Q_D(const KDELocalizer); 0193 KLocalizedString localizedString = ki18n(string.toUtf8()); 0194 return substituteArguments(localizedString, arguments).toString(d->m_locale); 0195 }