PDA

View Full Version : [SOLVED] Get system language on Mac OS X



zaphod.b
30th December 2013, 13:57
Hi all,

On Mac OS X, QLocale::system() refers to Region rather than Language.
Is there a (Qt or other) way to access the Language from C++ code?
Edit: I'm currently on Qt 5.2.

Thx

Added after 12 minutes:

Supplement:
I found this AppleScript approach (http://stackoverflow.com/questions/7160462/get-language-of-currently-logged-in-user-via-applescript) which might help. However I'd prefer something more convenient... Qt-ish or at least C++-ish...

zaphod.b
30th December 2013, 16:49
I'll solve myself:

From the QSettings doc (http://qt-project.org/doc/qt-5/QSettings.html#platform-limitations):

On Mac OS X, allKeys() will return some extra keys for global settings that apply to all applications. These keys can be read using value() but cannot be changed, only shadowed. Calling setFallbacksEnabled(false) will hide these global settings.
QSettings::allKeys() lists, among many others, "AppleLanguages" - which I have come across before, see above.

So the solution is simple:


QSettings settings; //no user specific settings needed
QVariant osxLanguageSettings = settings.value("AppleLanguages"); //list of all languages (as opposed to regions) as set in the system preferences
QStringList displayLanguages = osxLanguageSettings.toStringList(); //e.g. {"en", "de", "fr", "it"} on my system
QString preferredLanguage = displayLanguages.first(); //first in list is the preferred one
qDebug() << "preferred language is:" << preferredLanguage; //"en"
//however...
qDebug() << "system locale is:" << QLocale::system(); //e.g. "nb_NO" in case of 'Norwegian Bokmål' region/locale

You can iterate the list for a fallback.

Hth.