4

I need to detect user's language and country code in Qt. That codes must be matching standards at http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s04.html.

I've tried QLocale, but it returned full country and language name in countryToString and languageToString. (I need short code, like "en" instead of "English".)

One of the ways is creating map of QLocale::Language and QString. But is there any faster and simpler way?

Ivan Akulov
  • 4,323
  • 5
  • 37
  • 64

3 Answers3

6

See QLocale::name()

Returns the language and country of this locale as a string of the form "language_country", where language is a lowercase, two-letter ISO 639 language code, and country is an uppercase, two- or three-letter ISO 3166 country code.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • Hi, just like in Qt, if the user decides to change the locale from an option in the app, it can trigger a language change event in the app that can be listened to and made to refresh the UI accordingly, is there any event which Qt can capture if the user changes the locale in the OS? – SexyBeast May 29 '15 at 17:18
2

In addition to Paul's answer, there are QLocale::uiLanguages() and QLocale::bcp47Name() which should give variations.

Samuel Harmer
  • 4,264
  • 5
  • 33
  • 67
0

When we talk about correct detection of country actually set in user preferences (Control Panel/Location on Windows, Preferences/Region on OS X), you should be using https://github.com/crystalidea/qt-detect-user-country

GeekUser
  • 410
  • 4
  • 12