Fix crash when default locale is ru.

Additionally fix a common antipattern when loading translations in Qt.
This commit is contained in:
Roman Telezhynskyi 2022-04-22 12:42:46 +03:00
parent 7d45ca1447
commit 7c1c6deeae
7 changed files with 65 additions and 27 deletions

View File

@ -1,3 +1,6 @@
# Valentina 0.7.52 (unreleased)
- Fix crash when default locale is ru.
# Valentina 0.7.51 April 18, 2022
- Z value change for a layout piece.
- Fix issue with Custom template.

View File

@ -399,7 +399,7 @@ void VPApplication::InitOptions()
QPixmapCache::setCacheLimit(50 * 1024 /* 50 MB */);
LoadTranslation(QLocale().name());// By default the console version uses system locale
LoadTranslation(QString());// By default the console version uses system locale
VPCommandLine::Instance();

View File

@ -423,7 +423,7 @@ void MApplication::InitOptions()
qCDebug(mApp, "Command-line arguments: %s", qUtf8Printable(arguments().join(", ")));
qCDebug(mApp, "Process ID: %s", qUtf8Printable(QString().setNum(applicationPid())));
LoadTranslation(QLocale().name());// By default the console version uses system locale
LoadTranslation(QString());// By default the console version uses system locale
static const char * GENERIC_ICON_TO_CHECK = "document-open";
if (QIcon::hasThemeIcon(GENERIC_ICON_TO_CHECK) == false)

View File

@ -617,7 +617,7 @@ void VApplication::InitOptions()
qDebug()<<"Command-line arguments:"<<arguments();
qDebug()<<"Process ID:"<<applicationPid();
LoadTranslation(QLocale().name());// By default the console version uses system locale
LoadTranslation(QString());// By default the console version uses system locale
// Create command line parser after loading translations to show localized version.
VCommandLine::Get(*this);

View File

@ -473,7 +473,7 @@ void InitLanguages(QComboBox *combobox)
combobox->clear();
QStringList fileNames;
QDirIterator it(VAbstractApplication::VApp()->translationsPath(), QStringList("valentina_*.qm"), QDir::Files,
QDirIterator it(VAbstractApplication::translationsPath(), QStringList("valentina_*.qm"), QDir::Files,
QDirIterator::Subdirectories);
while (it.hasNext())
{

View File

@ -50,6 +50,50 @@
# include "appimage.h"
#endif // defined(APPIMAGE) && defined(Q_OS_LINUX)
namespace
{
auto FilterLocales(const QStringList &locales) -> QStringList
{
QStringList filtered;
for (const auto &locale : locales)
{
if (not locale.startsWith(QLatin1String("ru")))
{
filtered.append(locale);
}
}
return filtered;
}
//---------------------------------------------------------------------------------------------------------------------
auto LoadQM(QTranslator *translator, const QString &filename, const QString &locale, const QString &qmDir) -> bool
{
QStringList languages;
if (not locale.isEmpty())
{
languages.append(locale);
}
else
{
languages = QLocale().uiLanguages();
}
languages = FilterLocales(languages);
for (auto &locale : languages)
{
const bool loaded = translator->load(filename + locale, qmDir);
if (loaded)
{
return loaded;
}
}
return false;
}
} // namespace
const QString VAbstractApplication::warningMessageSignature = QStringLiteral("[PATTERN MESSAGE]");
//---------------------------------------------------------------------------------------------------------------------
@ -109,7 +153,7 @@ VAbstractApplication::VAbstractApplication(int &argc, char **argv)
* subdirectory inside an app bundle.
* @return path to a directory that contain QM files.
*/
QString VAbstractApplication::translationsPath(const QString &locale) const
auto VAbstractApplication::translationsPath(const QString &locale) -> QString
{
const QString trPath = QStringLiteral("/translations");
#ifdef Q_OS_WIN
@ -201,51 +245,42 @@ void VAbstractApplication::LoadTranslation(const QString &locale)
{
if (locale.isEmpty())
{
qDebug()<<"Locale is empty.";
return;
qDebug()<<"Default locale";
}
if (locale.startsWith(QLatin1String("ru")))
else
{
return;
}
qDebug()<<"Checked locale:"<<locale;
}
ClearTranslation();
const QString appQmDir = VAbstractApplication::translationsPath(locale);
qtTranslator = new QTranslator(this);
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
qtTranslator->load("qt_" + locale, translationsPath(locale));
const QString qtQmDir = appQmDir;
#else
qtTranslator->load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
const QString qtQmDir = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
#endif
LoadQM(qtTranslator, QStringLiteral("qt_"), locale, qtQmDir);
installTranslator(qtTranslator);
qtxmlTranslator = new QTranslator(this);
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
qtxmlTranslator->load("qtxmlpatterns_" + locale, translationsPath(locale));
#else
qtxmlTranslator->load("qtxmlpatterns_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
#endif
LoadQM(qtxmlTranslator, QStringLiteral("qtxmlpatterns_"), locale, qtQmDir);
installTranslator(qtxmlTranslator);
qtBaseTranslator = new QTranslator(this);
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
qtBaseTranslator->load("qtbase_" + locale, translationsPath(locale));
#else
qtBaseTranslator->load("qtbase_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
#endif
LoadQM(qtBaseTranslator, QStringLiteral("qtbase_"), locale, qtQmDir);
installTranslator(qtBaseTranslator);
appTranslator = new QTranslator(this);
appTranslator->load("valentina_" + locale, translationsPath(locale));
LoadQM(appTranslator, QStringLiteral("valentina_"), locale, appQmDir);
installTranslator(appTranslator);
const QString system = Settings()->GetPMSystemCode();
pmsTranslator = new QTranslator(this);
pmsTranslator->load("measurements_" + system + "_" + locale, translationsPath(locale));
LoadQM(pmsTranslator, QStringLiteral("measurements_") + Settings()->GetPMSystemCode() + '_', locale, appQmDir);
installTranslator(pmsTranslator);
InitTrVars();//Very important do it after load QM files.

View File

@ -62,7 +62,7 @@ public:
virtual const VTranslateVars *TrVars()=0;
QString translationsPath(const QString &locale = QString()) const;
static QString translationsPath(const QString &locale = QString());
void LoadTranslation(const QString &locale);