Allow to debug locating of translations

This commit is contained in:
Martchus 2019-06-16 16:56:38 +02:00
parent 18e3a907af
commit 7e8766cdb6
1 changed files with 40 additions and 27 deletions

View File

@ -114,17 +114,21 @@ void loadQtTranslationFile(std::initializer_list<QString> repositoryNames)
*/ */
void loadQtTranslationFile(initializer_list<QString> repositoryNames, const QString &localeName) void loadQtTranslationFile(initializer_list<QString> repositoryNames, const QString &localeName)
{ {
for (const QString &repoName : repositoryNames) { const auto debugTranslations = qEnvironmentVariableIsSet("QT_DEBUG_TRANSLATIONS");
QTranslator *const qtTranslator = new QTranslator; for (const auto &repoName : repositoryNames) {
const QString fileName(repoName % QChar('_') % localeName); auto *const qtTranslator = new QTranslator;
if (!additionalTranslationFilePath().isEmpty() && qtTranslator->load(fileName, additionalTranslationFilePath())) { const auto fileName = repoName % QChar('_') % localeName;
QCoreApplication::installTranslator(qtTranslator);
} else if (qtTranslator->load(fileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) { QString path;
QCoreApplication::installTranslator(qtTranslator); if ((!additionalTranslationFilePath().isEmpty() && qtTranslator->load(fileName, path = additionalTranslationFilePath()))
} else if (qtTranslator->load(fileName, QStringLiteral("../share/qt/translations"))) { || qtTranslator->load(fileName, path = QLibraryInfo::location(QLibraryInfo::TranslationsPath))
QCoreApplication::installTranslator(qtTranslator); || qtTranslator->load(fileName, path = QStringLiteral("../share/qt/translations"))
} else if (qtTranslator->load(fileName, QStringLiteral(":/translations"))) { || qtTranslator->load(fileName, path = QStringLiteral(":/translations"))) {
QCoreApplication::installTranslator(qtTranslator); QCoreApplication::installTranslator(qtTranslator);
if (debugTranslations) {
cerr << "Loading translation file for Qt repository \"" << repoName.toLocal8Bit().data() << "\" and the locale \""
<< localeName.toLocal8Bit().data() << "\" from \"" << path.toLocal8Bit().data() << "\"." << endl;
}
} else { } else {
delete qtTranslator; delete qtTranslator;
if (localeName.startsWith(QLatin1String("en"))) { if (localeName.startsWith(QLatin1String("en"))) {
@ -143,9 +147,12 @@ void loadQtTranslationFile(initializer_list<QString> repositoryNames, const QStr
* \param applicationName Specifies the name of the application. * \param applicationName Specifies the name of the application.
* \remarks * \remarks
* - Translation files have to be placed in one of the following locations: * - Translation files have to be placed in one of the following locations:
* * ./
* * ../$application
* * ../../$application
* * ./translations * * ./translations
* * /usr/share/$application/translations (used in UNIX) * * ../share/$application/translations
* * ../share/$application/translations (used in Windows) * * $install_prefix/share/$application/translations
* - Translation files must be named using the following scheme: * - Translation files must be named using the following scheme:
* * $application_$language.qm * * $application_$language.qm
* - Translation files can also be built-in using by setting the CMake variable * - Translation files can also be built-in using by setting the CMake variable
@ -171,9 +178,12 @@ void loadApplicationTranslationFile(const QString &applicationName)
* \param localeName Specifies the name of the locale. * \param localeName Specifies the name of the locale.
* \remarks * \remarks
* - Translation files have to be placed in one of the following locations: * - Translation files have to be placed in one of the following locations:
* * ./
* * ../$application
* * ../../$application
* * ./translations * * ./translations
* * /usr/share/$application/translations (used in UNIX) * * ../share/$application/translations
* * ../share/$application/translations (used in Windows) * * $install_prefix/share/$application/translations
* - Translation files must be named using the following scheme: * - Translation files must be named using the following scheme:
* * $application_$language.qm * * $application_$language.qm
* - Translation files can also be built-in using by setting the CMake variable * - Translation files can also be built-in using by setting the CMake variable
@ -183,20 +193,23 @@ void loadApplicationTranslationFile(const QString &applicationName)
*/ */
void loadApplicationTranslationFile(const QString &applicationName, const QString &localeName) void loadApplicationTranslationFile(const QString &applicationName, const QString &localeName)
{ {
QTranslator *const appTranslator = new QTranslator; auto *const appTranslator = new QTranslator;
const QString fileName(applicationName % QChar('_') % localeName); const auto fileName = applicationName % QChar('_') % localeName;
if (!additionalTranslationFilePath().isEmpty() && appTranslator->load(fileName, additionalTranslationFilePath())) {
QCoreApplication::installTranslator(appTranslator); QString path;
} else if (appTranslator->load(fileName, QStringLiteral("."))) { if ((!additionalTranslationFilePath().isEmpty() && appTranslator->load(fileName, path = additionalTranslationFilePath()))
QCoreApplication::installTranslator(appTranslator); || appTranslator->load(fileName, path = QStringLiteral(".")) || appTranslator->load(fileName, path = QStringLiteral("../") % applicationName)
} else if (appTranslator->load(fileName, QStringLiteral("./translations"))) { || appTranslator->load(fileName, path = QStringLiteral("../") % applicationName)
QCoreApplication::installTranslator(appTranslator); || appTranslator->load(fileName, path = QStringLiteral("../../") % applicationName)
} else if (appTranslator->load(fileName, QStringLiteral(APP_INSTALL_PREFIX "/share/") % applicationName % QStringLiteral("/translations"))) { || appTranslator->load(fileName, path = QStringLiteral("./translations"))
QCoreApplication::installTranslator(appTranslator); || appTranslator->load(fileName, path = QStringLiteral("../share/") % applicationName % QStringLiteral("/translations"))
} else if (appTranslator->load(fileName, QStringLiteral("../share/") % applicationName % QStringLiteral("/translations"))) { || appTranslator->load(fileName, path = QStringLiteral(APP_INSTALL_PREFIX "/share/") % applicationName % QStringLiteral("/translations"))
QCoreApplication::installTranslator(appTranslator); || appTranslator->load(fileName, path = QStringLiteral(":/translations"))) {
} else if (appTranslator->load(fileName, QStringLiteral(":/translations"))) {
QCoreApplication::installTranslator(appTranslator); QCoreApplication::installTranslator(appTranslator);
if (qEnvironmentVariableIsSet("QT_DEBUG_TRANSLATIONS")) {
cerr << "Loading translation file for \"" << applicationName.toLocal8Bit().data() << "\" and the locale \""
<< localeName.toLocal8Bit().data() << "\" from \"" << path.toLocal8Bit().data() << "\"." << endl;
}
} else { } else {
delete appTranslator; delete appTranslator;
if (localeName.startsWith(QLatin1String("en"))) { if (localeName.startsWith(QLatin1String("en"))) {