2

I want to detect the languages that are installed on my OS. To do that I an run:

qDebug() << QLocale::system();
// result = QLocale(English, Latin, UnitedStates)

but I want to save it in a QString, but it's not possible to do just:

QString langs = QLocale::system().toString();

How can I save this result in a string? Or is there a better way to detect OS languages? Another thing, is it possible to detect keyboard language settings (with Qt if possible)?

demonplus
  • 5,613
  • 12
  • 49
  • 68
vitor13almeida
  • 139
  • 1
  • 11
  • 1
    What does "detect the languages that are installed on my OS" mean really? Could you please elaborate? – peppe Nov 25 '16 at 22:32
  • I want to know the list of language packs that are installed on the computer where my app is running – vitor13almeida Nov 28 '16 at 14:57
  • What's a "language pack"? Under which OS? – peppe Nov 28 '16 at 15:13
  • I'm using Ubuntu and I have ENG and PT languages installed, and I want to get this info: "languages installed/used - ENG, PT," ... If my app is running on a computer with FR, IT and ENG, I want to get: "languages installed/used - FR, IT, ENG". I need this to work on Linux and Windows, that's why I wanted to use Qt. – vitor13almeida Nov 28 '16 at 15:23
  • 1
    But why a system language pack should be reflected in *your* application? Are you sure you don't want a way for *your* application to install additional translations, find them and list them to the user? – peppe Nov 28 '16 at 17:44

3 Answers3

5

The documentation for QLocale indicates that:

QLocale supports the concept of a default locale, which is determined from the system's locale settings at application startup

So to get differing representations of the system default language, one or more of the following methods on QLocale might help you:

qDebug() << QLocale::system().name();
qDebug() << QLocale::system().nativeCountryName();
qDebug() << QLocale::system().nativeLanguageName();

Example output:

"en_GB"
"United Kingdom"
"British English"

In a GUI application, you can get similar details for the input method as follows:

qDebug() << QGuiApplication::inputMethod()->locale().name();
Benp44
  • 1,548
  • 1
  • 10
  • 14
  • QLocale::system() shows me 3 languages but I only have 2 installed. Is it possible to do what I want with Qt, to show language packs installed? – vitor13almeida Nov 28 '16 at 14:55
  • @vitor13almeida How do you know what languages you have "installed"? What exact OS and distribution you're on? – Kuba hasn't forgotten Monica Nov 28 '16 at 16:45
  • Ubuntu with ENG and PT lang packs. Menus, windows, apps and these kind of stuff in ENG, keyboard it's PT or ENG. I need to save OS, OS version, langs, eyboard layout/langs.... on windows and linux – vitor13almeida Nov 28 '16 at 16:59
  • @vitor13almeida I don't think it's possible to get the list of language packs installed on your system via a built-in Qt function, as the way localisation is done varies wildly by environment. In Ubuntu there may be some useful commands, for example if you're using KDE you can search for installed language packages via `apt-cache search kde-l10n`. Similarly for keymaps: `ls /usr/share/X11/xkb/symbols/`. On my system the contents of these folders are a bit inconsistent, however many entries use two-letter language codes, which you can then convert to a human-readable name using QLocale. – Benp44 Nov 29 '16 at 17:40
1

Is it possible to do what I want with Qt, to show language packs installed?

No. There's no functionality to do that in Qt. You'll need a platform-specific approach for each platform you're targeting.

Also note that the concept of "language packs" is not portable. E.g. most Unix applications (including those on OS X) come with support for all languages that they can to support, and you can switch the language instantaneously without having to install anything.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • ok. Any suggestion how to do it for linux OS? Check files, use some c++ class, ...? – vitor13almeida Nov 28 '16 at 15:11
  • @vitor13almeida In most cases, there's nothing for you to do. Any application will display in the currently selected locale language if it has translations for such. How to check if it has translations? You'll have to inspect source package or the binaries for translation files of some sort, or their cookie-crumbs, etc. There's really no simple all-encompassing way of doing that. No portable way of querying an app what languages it supports either. You do realize, I hope, that most X desktop environments include all translations by default. – Kuba hasn't forgotten Monica Nov 28 '16 at 15:37
  • 1
    @vitor13almeida You have to answer the fundamental question of *why* do you think you need this information? For exactly what applications? You probably know that different applications will support multiple languages to a different extent - for OSS it depends on the availability of volunteers to work on translations. You might have e.g. a KDE control center with 15 complete translations, but a window manager with only 10. That's made up numbers just to illustrate the issue. – Kuba hasn't forgotten Monica Nov 28 '16 at 15:40
  • I don't want to check OS langs to change my app language settings. I need it so I can compare app/user location (country) VS user/OS languages... for example, my app can be used in UK, but the user can use ENG, or FR, or IT, ... In Windows it's possible to get the info I need, for example with GetKeyboardLayoutList(). And in Linux OS? Is there some cross-platform library for this? – vitor13almeida Nov 28 '16 at 15:45
  • There is nothing for you to do other than to provide the translation(s) into the languages you intend to support. The application will start in the language of user's choosing. Qt handles all that for you in its translation system. You should also use the locale system to deal with localized I/O (c.f. [this answer](http://stackoverflow.com/a/38086266/1329652)). Don't conflate the chosen UI language with keyboard layouts. You'll really annoy the users who can use several languages, and thus have the requisite keyboard layouts set up, but stick to one language for their UI. – Kuba hasn't forgotten Monica Nov 28 '16 at 15:57
  • Also don't forget to include the translations bundled with Qt: these will cover standard UI elements such as `Ok` and `Cancel` button role text, etc. – Kuba hasn't forgotten Monica Nov 28 '16 at 16:01
  • I don't want to change my app lang based on the OS/user lang. I need to detect OS languages and user/keyboard languages just to save this info in a log file. for example. If someone logs into my app I want to save: username, country, OS installed, OS version, languages installed, keyboard layout/language, date of access, ... – vitor13almeida Nov 28 '16 at 16:04
  • 1
    You won't be able to log the "languages installed" on a Unix system, because those systems come with *all* "languages" installed, usually, and if they don't then it's a matter of the installed set of packages. E.g. on CentOS/RHEL you might log the output of `rpm -qa|sort`. As for keyboard layouts, you will need to get that information from the X server, or from OS X or Windows using platform-specific API. Please fix your question to indicate specifically what bits of information you're after, and what exact platforms you care about. – Kuba hasn't forgotten Monica Nov 28 '16 at 16:42
0

You can get a list of languages, in the order of user preference (e.g. on macOS that is defined in System Preferences), with QLocale::system().uiLanguages().

For a user presentable name of a locale, you can use something like

QString("%1 (%2)").arg(QLocale::languageToString(locale.language()),
                       QLocale::countryToString(locale.country()));
E4z9
  • 1,713
  • 9
  • 11