Unsolved How to prevent standard translations?
-
I have always been annoyed by translated texts, for example message box buttons, in applications which otherwise are not translated. Is there a simple way to prevent Qt library translations in cases where there's no corresponding translation for the application? It's of course possible to reset the texts for the standard buttons etc. but it's clumsy and requires extra coding. (My specific problem at the moment is with Quick Controls 2 Dialog but the same goes for other pre-translated Qt texts.)
-
You probably need to set the default locale http://doc.qt.io/qt-5/qlocale.html#setDefault
-
There are translations for such things as message box buttons. They are just not part of your customTranslation.ts/qm file.
Qt has a couple of finished or nearly finished translations to the most common languages, usually you can find them in this installation path
...\Qt\Tools\QtCreator\share\qtcreator\translations
Just install a 2nd QTranslator and everything should be fine, as long as the target language exists.
//for example: uTranslator-> load("mytr_de", trPath); qtTranslator-> load("qt_de", qtPath); qApp->installTranslator(uTranslator); qApp->installTranslator(qtTranslator);
-
-
@VRonin I tried, but it doesn't work well. This code:
QLocale loc = QLocale::system(); qDebug() << loc.country() << loc.language() << loc.decimalPoint() << loc.script() << loc.uiLanguages(); QLocale loc2(QLocale::English, loc.country()); QLocale::setDefault(loc2); loc = QLocale(); qDebug() << loc.country() << loc.language() << loc.decimalPoint() << loc.script() << loc.uiLanguages();
leads to this ouput:
QLocale::Country(UnitedStates) QLocale::Language(English) ',' QLocale::Script(LatinScript) ("en-US") QLocale::Country(UnitedStates) QLocale::Language(English) '.' QLocale::Script(LatinScript) ("en", "en-US", "en-Latn-US")
Notice how it changes decimal point, which I don't want. It's weird because my real environment locale is:
LANG=fi_FI.UTF-8 LANGUAGE=en_US LC_CTYPE="fi_FI.UTF-8" LC_NUMERIC=fi_FI.UTF-8 LC_TIME=fi_FI.UTF-8 LC_COLLATE=fi_FI.UTF-8 LC_MONETARY=fi_FI.UTF-8 LC_MESSAGES="fi_FI.UTF-8" LC_PAPER=fi_FI.UTF-8 LC_NAME=fi_FI.UTF-8 LC_ADDRESS=fi_FI.UTF-8 LC_TELEPHONE=fi_FI.UTF-8 LC_MEASUREMENT=fi_FI.UTF-8 LC_IDENTIFICATION=fi_FI.UTF-8
(in a linux terminal and in the Qt Creator Project-Run->Run Environment.)
Why doesn't QLocale work as expected? I want to change the language but keep everything else, just like in my environment. And if someone wants to wonder, the original problem came in a Windows machine, not in this Linux. I'm just coding with Linux right now.
-
I surfed through the relevant Qt code. qquickdialogbuttonbox.cpp has this code:
button->setText(QPlatformTheme::removeMnemonics(QGuiApplicationPrivate::platformTheme()->standardButtonText(standardButton)));
and qplatformtheme.cpp has this:
QString QPlatformTheme::standardButtonText(int button) const { return QPlatformTheme::defaultStandardButtonText(button); } QString QPlatformTheme::defaultStandardButtonText(int button) { switch (button) { case QPlatformDialogHelper::Ok: return QCoreApplication::translate("QPlatformTheme", "OK");
In qcoreapplication.cpp QCoreApplication::translate has:
for (it = self->d_func()->translators.constBegin(); it != self->d_func()->translators.constEnd(); ++it) { translationFile = *it; result = translationFile->translate(context, sourceText, disambiguation, n); if (!result.isNull()) break; }
and as the code and documentation says, the installed QTranslators are prepended, so the latest installed should be found first. If it can translate it with its translate() method the string is returned and no more translators are tried.
I wrote a translator:
class NoTranslator: public QTranslator { Q_OBJECT public: explicit NoTranslator(): QTranslator() {} bool isEmpty() const override {return false;} QString translate(const char *context, const char *sourceText, const char *disambiguation = Q_NULLPTR, int n = -1) const override { qDebug() << sourceText; qDebug() << context; //from qcoreapplication.cpp Q_UNUSED(context) Q_UNUSED(disambiguation) QString ret = QString::fromUtf8(sourceText); if (n >= 0) ret.replace(QLatin1String("%n"), QString::number(n)); qDebug() << ret; return ret; } };
and installed it in main.cpp:
QApplication* app{new QApplication(argc, argv)}; QTranslator* notr = new NoTranslator(); app->installTranslator(notr);
Otherwise it seems to work fine, qDebug() shows it works with qml files. However, I have in a Components2 Dialog a DialogButtonBox with some standard buttons. They are not translated with my translator, instead they obey the LANGUAGE environment variable. Why?