Question

I need to test my application translation to non-English language very often, and this is very uncomfortable to change the whole operating system language just to do this simple check. How can i change Qt-detected system language using environment variables for example? or with command-line parameter.

I try to change LANG, LANGUAGE environment variables, but it has no effect. However, under GNOME it has!

UPD: code i'm using such code to determine the system locale and load appropriate translation:

QTranslator app_translator;
if (!app_translator.load ("app_" + QLocale::system ().name (), app_tr_dir))
    qWarning ("Can't load app translator file for locale %s from %s", qPrintable (QLocale::system ().name ()), app_tr_dir.toLocal8Bit().data());
else
    app.installTranslator (&app_translator);

P.S. My OS is Kubuntu 13.10, Qt version is 4.8.

Was it helpful?

Solution 2

For testing you can use something like that (just correct main function):

int main(int argc, char **argv) {
    QApplication app(argc, argv);

    QLocale localeUsedToDeterminateTranslators = QLocale::system();
    Q_FOREACH(QString a, app.arguments()) {
        const static localeParam = "-locale:";
        if (a.startsWith(localeParam)) {
           localeUsedToDeterminateTranslators = QLocale(a.mid(sizeof(localeParam)-1));
           break;
        }
    }
    ... // your normal code

Then when you run you app you can just run it with extra parameter: ./yourAppName -locale:nl. See documentation of QLocale for possible values.


Edit: I've found even better approach, there is a method QLocale::setDefault, so this should work even better:

int main(int argc, char **argv) {
    QApplication app(argc, argv);

    Q_FOREACH(QString a, app.arguments()) {
        const static localeParam = "-locale:";
        if (a.startsWith(localeParam)) {
           QLocale::setDefault(QLocale(a.mid(sizeof(localeParam)-1)));
           break;
        }
    }
    ...
    QTranslator app_translator;
    if (!app_translator.load ("app_" + QLocale().name (), app_tr_dir))
         qWarning ("Can't load app translator file for locale %s from %s", qPrintable (QLocale().name()), app_tr_dir.toLocal8Bit().data());
    else
         app.installTranslator (&app_translator);

OTHER TIPS

You can always change the locale by QLocale::setDefault() method. here's an example from one project:

void Language::setCurrentLanguage(Language::Languages language)
{
    if (language == Language::Arabic) {
        QLocale l(QLocale::Arabic, QLocale::SaudiArabia);
        QLocale::setDefault(l);
        dynamic_cast<MangoApp*>(qApp)->setLayoutDirection(Qt::RightToLeft);
        dynamic_cast<MangoApp*>(qApp)->removeAllTranslator();
        dynamic_cast<MangoApp*>(qApp)->loadQtTranslator();
        dynamic_cast<MangoApp*>(qApp)->loadMangoTranslator();

    } else {
        QLocale l(QLocale::English, QLocale::UnitedStates);
        QLocale::setDefault(l);
        dynamic_cast<MangoApp*>(qApp)->setLayoutDirection(Qt::LeftToRight);
        dynamic_cast<MangoApp*>(qApp)->removeAllTranslator();
    }
}

Using the LANGUAGE (not LANG) environment variable should definitely change the value returned by QLocale::system().name(), because this environment variable has precedence over all other ways to define the application message locale (details).

I tested it this with Qt 5.12 under Lubuntu 19.10 (means, using the LXQt desktop), and it works. The command was:

LANGUAGE=de ./application

If this really does not work under Kubuntu, it should be reported as a bug, because then Kubuntu is interfering with the way how an application is told its locale.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top