mirror of
https://github.com/QuasarApp/QuasarAppLib.git
synced 2025-04-26 01:34:39 +00:00
fix translations
This commit is contained in:
parent
81e4ef9bb4
commit
1fe4fa3b25
56
locales.cpp
56
locales.cpp
@ -19,19 +19,27 @@
|
||||
using namespace QuasarAppUtils;
|
||||
|
||||
bool Locales::setLocalePrivate(const QLocale &locale) {
|
||||
auto translator = getTranslator();
|
||||
removeOldTranslation();
|
||||
QFileInfoList qmFiles;
|
||||
|
||||
const auto qmFiles = QDir(_location).entryInfoList({"*" + locale.bcp47Name() + "*.qm"}, QDir::Files);
|
||||
|
||||
for (const auto & file: qmFiles) {
|
||||
if(!translator->load(file.absoluteFilePath())) {
|
||||
return false;
|
||||
}
|
||||
for (const auto &location: qAsConst(_locations)) {
|
||||
qmFiles += QDir(location).entryInfoList({"*" + locale.bcp47Name() + "*.qm"}, QDir::Files);
|
||||
}
|
||||
|
||||
for (const auto & file: qAsConst(qmFiles)) {
|
||||
auto translator = new QTranslator();
|
||||
|
||||
if(!(translator->load(file.absoluteFilePath()) && QCoreApplication::installTranslator(translator))) {
|
||||
delete translator;
|
||||
continue;
|
||||
}
|
||||
|
||||
_translations.push_back(translator);
|
||||
|
||||
}
|
||||
emit sigTranslationChanged();
|
||||
|
||||
return true;
|
||||
return _translations.size();
|
||||
}
|
||||
|
||||
bool Locales::setLocale(const QLocale &locale) {
|
||||
@ -39,26 +47,20 @@ bool Locales::setLocale(const QLocale &locale) {
|
||||
return obj->setLocalePrivate(locale);
|
||||
}
|
||||
|
||||
bool Locales::init(const QLocale &locale, QString location) {
|
||||
bool Locales::init(const QLocale &locale, const QSet<QString> & location) {
|
||||
auto obj = instance();
|
||||
return obj->initPrivate(locale, location);
|
||||
}
|
||||
|
||||
bool Locales::initPrivate(const QLocale &locale, QString location) {
|
||||
auto translator = getTranslator();
|
||||
bool Locales::initPrivate(const QLocale &locale, const QSet<QString> & locations) {
|
||||
auto defaultTr = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
|
||||
|
||||
if (location.isEmpty())
|
||||
_location = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
|
||||
else
|
||||
_location = location;
|
||||
|
||||
if (!setLocale(locale)) {
|
||||
return false;
|
||||
_locations = locations;
|
||||
if (!_locations.contains(defaultTr)) {
|
||||
_locations += defaultTr;
|
||||
}
|
||||
|
||||
QCoreApplication::installTranslator(translator);
|
||||
|
||||
return true;
|
||||
return setLocalePrivate(locale);
|
||||
}
|
||||
|
||||
Locales *Locales::instance() {
|
||||
@ -66,7 +68,13 @@ Locales *Locales::instance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
QTranslator *Locales::getTranslator() {
|
||||
static QTranslator *translator = new QTranslator();
|
||||
return translator;
|
||||
void Locales::removeOldTranslation() {
|
||||
for (const auto & tr :qAsConst(_translations)) {
|
||||
QCoreApplication::removeTranslator(tr);
|
||||
delete tr;
|
||||
}
|
||||
}
|
||||
|
||||
Locales::~Locales() {
|
||||
removeOldTranslation();
|
||||
}
|
||||
|
16
locales.h
16
locales.h
@ -45,8 +45,7 @@ public:
|
||||
* @return return true if locale set for application
|
||||
*/
|
||||
static bool init(const QLocale &locale = QLocale::system(),
|
||||
QString location = "");
|
||||
|
||||
const QSet<QString> & location = {});
|
||||
/**
|
||||
* @brief instance
|
||||
* @return return static object
|
||||
@ -61,16 +60,15 @@ signals:
|
||||
|
||||
private:
|
||||
Locales() = default;
|
||||
~Locales();
|
||||
|
||||
bool setLocalePrivate(const QLocale &locale = QLocale::system());
|
||||
bool initPrivate(const QLocale &locale = QLocale::system(),
|
||||
QString location = "");
|
||||
const QSet<QString> &location = {});
|
||||
void removeOldTranslation();
|
||||
|
||||
/**
|
||||
* @brief getTranslator
|
||||
* @return instance of Translation
|
||||
*/
|
||||
static QTranslator* getTranslator();
|
||||
QString _location;
|
||||
QSet<QString> _locations;
|
||||
QList<QTranslator *> _translations;
|
||||
|
||||
};
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user