windowsqtlocalizationinternationalizationqlocale

QLocale detects system language incorrectly on Windows with language pack installed


I am attempting to detect current system language with QLocale:

QLocale::Language sysLangId = QLocale::system().language();

However, it's not working correctly. I'm on Russian Windows 7 with English language pack applied, but language() returns Russian instead of English. Is there any workaround?


Solution

  • I've found 2 ways to solve my problem. The Qt way is to use QLocale::system().uiLanguages(). On my system it returns a list with a single item "en-US". The problem with that is I need a language name, like "english", so I'd have to add a map for converting language code to language name. It's no big deal, but I've decided to use WinAPI:

    QString sysLangName;
    const LANGID langId = GetUserDefaultUILanguage();
    WCHAR langName[1000] = {0};
    if (GetLocaleInfoW(MAKELCID(langId, SORT_DEFAULT), LOCALE_SENGLANGUAGE, langName, sizeof langName / sizeof langName[0] - 1) != 0)
        sysLangName = QString::fromWcharArray(langName);