mirror of
https://github.com/QuasarApp/QuasarAppLib.git
synced 2025-05-07 23:19:42 +00:00
fix work with translations. Now selection not load empty qm files
All checks were successful
buildbot/DocsGenerator Build finished.
buildbot/AndroidBuilder_v8Qt6 Build finished.
buildbot/LinuxCMakeBuilderQt6 Build finished.
buildbot/LinuxCMakeBuilder Build finished.
buildbot/AndroidBuilder_v8 Build finished.
buildbot/AndroidBuilder_v7 Build finished.
buildbot/WindowsCMakeBuilder Build finished.
buildbot/IOSCMakeBuilder Build finished.
All checks were successful
buildbot/DocsGenerator Build finished.
buildbot/AndroidBuilder_v8Qt6 Build finished.
buildbot/LinuxCMakeBuilderQt6 Build finished.
buildbot/LinuxCMakeBuilder Build finished.
buildbot/AndroidBuilder_v8 Build finished.
buildbot/AndroidBuilder_v7 Build finished.
buildbot/WindowsCMakeBuilder Build finished.
buildbot/IOSCMakeBuilder Build finished.
This commit is contained in:
parent
f06e1e290a
commit
00c013beac
82
locales.cpp
82
locales.cpp
@ -14,32 +14,77 @@
|
|||||||
#include <QLibraryInfo>
|
#include <QLibraryInfo>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include "params.h"
|
#include "params.h"
|
||||||
#include "qdebug.h"
|
|
||||||
|
|
||||||
using namespace QuasarAppUtils;
|
using namespace QuasarAppUtils;
|
||||||
|
|
||||||
bool QuasarAppUtils::Locales::findQm(const QString& localePrefix,
|
bool QuasarAppUtils::Locales::findQmPrivate(const QString &prefix,
|
||||||
QFileInfoList &qmFiles) {
|
QList<QTranslator*> &qmFiles) {
|
||||||
|
|
||||||
if (localePrefix.size() < 2)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
const auto prefixes = localePrefix.split(QRegularExpression("[_-]"));
|
|
||||||
|
|
||||||
auto prefixIt = prefixes.begin();
|
|
||||||
|
|
||||||
for (const auto &location: qAsConst(_locations)) {
|
for (const auto &location: qAsConst(_locations)) {
|
||||||
qmFiles += QDir(location).entryInfoList({"*" + (*prefixIt).toLower() + "*.qm"}, QDir::Files);
|
|
||||||
|
const auto availableFiles = QDir(location).entryInfoList(
|
||||||
|
{"*" + prefix + "*.qm"}, QDir::Files);
|
||||||
|
|
||||||
|
for (const auto &file : availableFiles) {
|
||||||
|
auto qmFile = new QTranslator();
|
||||||
|
|
||||||
|
if(!qmFile->load(file.absoluteFilePath())) {
|
||||||
|
QuasarAppUtils::Params::log("Failed to load translation file : "
|
||||||
|
+ file.absoluteFilePath(),
|
||||||
|
QuasarAppUtils::Warning);
|
||||||
|
delete qmFile;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (qmFile->isEmpty()) {
|
||||||
|
QuasarAppUtils::Params::log("Translation file is Empty: " +
|
||||||
|
file.absoluteFilePath(),
|
||||||
|
QuasarAppUtils::Debug);
|
||||||
|
delete qmFile;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto language = qmFile->language();
|
||||||
|
if (language.size() && !language.contains(prefix, Qt::CaseInsensitive)) {
|
||||||
|
auto message = QString("The target language (%0) and a choosed qm file (%1) "
|
||||||
|
"is different, Loading will be skiped: ").
|
||||||
|
arg(language, file.absoluteFilePath());
|
||||||
|
QuasarAppUtils::Params::log(message, QuasarAppUtils::Debug);
|
||||||
|
|
||||||
|
delete qmFile;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
qmFiles += qmFile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return qmFiles.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool QuasarAppUtils::Locales::findQm(const QString &localePrefix,
|
||||||
|
QList<QTranslator *> &qmFiles) {
|
||||||
|
|
||||||
|
if (localePrefix.size() < 2) {
|
||||||
|
if (localePrefix.compare('c', Qt::CaseInsensitive) == 0) {
|
||||||
|
return findQmPrivate("en", qmFiles);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return qmFiles.size();
|
static QRegularExpression regexp("[_-]");
|
||||||
|
const auto prefixes = localePrefix.split(regexp);
|
||||||
|
|
||||||
|
auto prefixIt = prefixes.begin();
|
||||||
|
return findQmPrivate((*prefixIt).toLower(), qmFiles);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Locales::setLocalePrivate(const QLocale &locale) {
|
bool Locales::setLocalePrivate(const QLocale &locale) {
|
||||||
removeOldTranslation();
|
removeOldTranslation();
|
||||||
|
|
||||||
const auto list = locale.uiLanguages();
|
const auto list = locale.uiLanguages();
|
||||||
QFileInfoList qmFiles;
|
QList<QTranslator *> qmFiles;
|
||||||
|
|
||||||
auto it = list.begin();
|
auto it = list.begin();
|
||||||
while (it != list.end() && !findQm(*it, qmFiles)) {
|
while (it != list.end() && !findQm(*it, qmFiles)) {
|
||||||
@ -49,16 +94,7 @@ bool Locales::setLocalePrivate(const QLocale &locale) {
|
|||||||
if (qmFiles.isEmpty())
|
if (qmFiles.isEmpty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (const auto & file: qAsConst(qmFiles)) {
|
for (const auto & translator: qAsConst(qmFiles)) {
|
||||||
auto translator = new QTranslator();
|
|
||||||
|
|
||||||
if(!translator->load(file.absoluteFilePath())) {
|
|
||||||
QuasarAppUtils::Params::log("Failed to load translation file : " + file.absoluteFilePath(),
|
|
||||||
QuasarAppUtils::Warning);
|
|
||||||
delete translator;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!QCoreApplication::installTranslator(translator)) {
|
if (!QCoreApplication::installTranslator(translator)) {
|
||||||
|
|
||||||
QuasarAppUtils::Params::log("Failed to install translation file : " + translator->filePath(),
|
QuasarAppUtils::Params::log("Failed to install translation file : " + translator->filePath(),
|
||||||
|
@ -94,7 +94,9 @@ private:
|
|||||||
const QLocale ¤tLocatePrivate() const;
|
const QLocale ¤tLocatePrivate() const;
|
||||||
|
|
||||||
bool findQm(const QString &localePrefix,
|
bool findQm(const QString &localePrefix,
|
||||||
QFileInfoList& result);
|
QList<QTranslator *> &result);
|
||||||
|
bool findQmPrivate(const QString &prefix,
|
||||||
|
QList<QTranslator *> &qmFiles);
|
||||||
|
|
||||||
QLocale _currentLocate;
|
QLocale _currentLocate;
|
||||||
QSet<QString> _locations;
|
QSet<QString> _locations;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user