11#include <QCoreApplication>
14#include <QLibraryInfo>
15#include <QRegularExpression>
21bool QuasarAppUtils::Locales::findQmPrivate(
const QString &prefix,
22 QList<QTranslator*> &qmFiles) {
24 for (
const auto &location: std::as_const(_locations)) {
26 const auto availableFiles = QDir(location).entryInfoList(
27 {prefix +
".qm"}, QDir::Files);
29 for (
const auto &file : availableFiles) {
30 auto qmFile =
new QTranslator();
32 if(!qmFile->load(file.absoluteFilePath())) {
33 qWarning() <<
"Failed to load translation file : "
34 + file.absoluteFilePath();
39 if (qmFile->isEmpty()) {
40 qDebug() <<
"Translation file is Empty: " +
41 file.absoluteFilePath();
46 auto language = qmFile->language();
47 if (language.size() && !language.contains(prefix, Qt::CaseInsensitive)) {
48 auto message = QString(
"The target language (%0) and a choosed qm file (%1) "
49 "is different, Loading will be skiped: ").
50 arg(language, file.absoluteFilePath());
60 return qmFiles.size();
63bool QuasarAppUtils::Locales::findQm(QString localePrefix,
64 QList<QTranslator *> &qmFiles) {
66 if (localePrefix.size() < 2) {
67 if (localePrefix.compare(
'c', Qt::CaseInsensitive) == 0) {
68 return findQmPrivate(
"en", qmFiles);
73 }
else if (localePrefix.size() >= 4) {
74 return findQmPrivate(localePrefix.replace(
'-',
'_'), qmFiles);
77 return findQmPrivate(localePrefix, qmFiles);
80void QuasarAppUtils::Locales::installTranslations( QList<QTranslator *> &qmFiles)
82 for (
const auto & translator: std::as_const(qmFiles)) {
83 if (!QCoreApplication::installTranslator(translator)) {
85 qWarning() <<
"Failed to install translation file : " + translator->filePath();
88 qmFiles.removeAll(translator);
95QString Locales::translatePrivate(
const char *source,
const QLocale &locale) {
96 auto translations = _translations.value(locale);
98 for (
const auto&
tr : translations) {
99 auto result =
tr->translate(
"QuasarAppUtils::Locales", source);
108bool Locales::setLocalePrivate(
const QLocale &locale,
bool force,
bool install) {
113 removeOldTranslation(_currentLocate);
116 QList<QTranslator *> &qmFiles = _translations[locale];
118 if (qmFiles.isEmpty()) {
120 const auto list = locale.uiLanguages();
122 auto it = list.rbegin();
123 while (qmFiles.isEmpty() && it != list.rend() && !findQm(*it, qmFiles)) {
127 if (qmFiles.isEmpty())
133 installTranslations(qmFiles);
137 _currentLocate = locale;
139 return _translations[locale].size();
144 return obj->currentLocatePrivate();
149 return obj->translatePrivate(source, locale);
154 return obj->setLocalePrivate(locale, force);
157bool Locales::init(
const QList<QLocale> &locales,
const QSet<QString> &location) {
159 return obj->initPrivate(locales, location);
164 return obj->initPrivate(locale, location);
167bool Locales::initPrivate(
const QLocale &locale,
const QSet<QString> & locations) {
169#if QT_VERSION <= QT_VERSION_CHECK(6, 0, 0)
170 auto defaultTr = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
172 auto defaultTr = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
174 _locations = locations;
175 if (!_locations.contains(defaultTr)) {
176 _locations += defaultTr;
179 return setLocalePrivate(locale);
182bool Locales::initPrivate(
const QList<QLocale> &locales,
const QSet<QString> &locations) {
183#if QT_VERSION <= QT_VERSION_CHECK(6, 0, 0)
184 auto defaultTr = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
186 auto defaultTr = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
188 _locations = locations;
189 if (!_locations.contains(defaultTr)) {
190 _locations += defaultTr;
193 for (
const auto& locale: locales) {
194 if (!setLocalePrivate(locale,
false,
false)) {
202void Locales::clearCache(
const QLocale &locale) {
203 for (
const auto &
tr :std::as_const(_translations[locale])) {
204 QCoreApplication::removeTranslator(
tr);
208 _translations[locale].clear();
211void Locales::clearCache() {
212 for (
auto it = _translations.keyBegin(); it != _translations.keyEnd(); it = std::next(it)) {
215 _translations.clear();
223void Locales::removeOldTranslation(
const QLocale &locale) {
224 for (
const auto &
tr :std::as_const(_translations[locale])) {
225 QCoreApplication::removeTranslator(
tr);
229void Locales::addLocationPrivate(
const QString &location) {
230 _locations += location;
233const QLocale &Locales::currentLocatePrivate()
const {
234 return _currentLocate;
239 obj->addLocationPrivate(location);
The Locales class for parese local files Example :
static bool setLocale(const QLocale &locale, bool force=false)
setLocale This method sets locale for application and loaded all translations for this locale.
static void addLocation(const QString &location)
addLocation This method add location for qm files. Use This method if you create a own library with t...
static QString tr(const char *source, const QLocale &locale)
tr This method will translate single string to choosed language.
void sigTranslationChanged()
sigTranslationChanged Emited when set new locale for application.
static bool init(const QList< QLocale > &locales, const QSet< QString > &location={})
init This method initialize translations of applictaion.
static Locales * instance()
instance This method return pointer to the Locales service.
static const QLocale & currentLocate()
currentLocate This method return current locate of applicatuon.
The QuasaraAppUtils class This lib include base functions for the all applications of QuasarApp group...