0
QLocale systemLocale;
LOG_ERROR() << "SYSTEM LANGUAGE:" << systemLocale.languageToString(systemLocale.language());
LOG_ERROR() << QObject::tr("Welcome");

The second line prints the correct language, when I change the language from the phone settings, however, "Welcome" doesn't get translated to the current system language. What could be the issue with this?

Kartik
  • 45
  • 1
  • 4

1 Answers1

0

The translation is probably not loaded. Here's how you can load a file:

QTranslator translator;
QString locale_string = QLocale().name();
QString filename = QString("my_app_%1").arg(locale_string);
if (translator.load(filename, "app/native/qm")) {
    app.installTranslator(&translator);
}

This would try to load translations from app/native/qm/my_app_fr.qm on a french device, for example.

Note that by default, you'll have to restart the application after changing the device language. You can use a LocaleHandler to update the translation when the phone language changes. Listen to onSystemLanguageChanged() signal, remove the old translator, then load the new one (same code as above).

Marc Plano-Lesay
  • 6,808
  • 10
  • 44
  • 75