问题
I want to get the list of ISO 639 languages, from QLocale
. I can use this code to get all combinations of language/country.
QList<QLocale> allLocales = QLocale::matchingLocales(
QLocale::AnyLanguage,
QLocale::AnyScript,
QLocale::AnyCountry);
This is exactly what I need. I assume I can filter out the list manually, but does a better alternative exist?
回答1:
You can do either that or do something not nearly as nice (see end of this post) and filter duplicate languages from the list manually, e.g. if you want the ISO 639 language names:
QList<QLocale> allLocales = QLocale::matchingLocales(
QLocale::AnyLanguage,
QLocale::AnyScript,
QLocale::AnyCountry);
QSet<QString> iso639Languages;
for(const QLocale &locale : allLocales) {
iso639Languages.insert(QLocale::languageToString(locale.language()));
}
qDebug() << iso639Languages;
iso639Languages
then contains the names of all languages classified by ISO 639 and known by Qt. Note that it does contain the name of the language (e.g. German) and not the ISO 639 code (e.g. de).
If you need the ISO 639 code do this instead:
QList<QLocale> allLocales = QLocale::matchingLocales(
QLocale::AnyLanguage,
QLocale::AnyScript,
QLocale::AnyCountry);
QSet<QString> iso639LanguageCodes;
for(const QLocale &locale : allLocales) {
iso639LanguageCodes.insert(locale.name().split('_').first());
}
qDebug() << iso639LanguageCodes;
One could also construct QLocale
objects by iterating manually over the QLocale::Language
enum and then parsing the result, but I strongly recommend not to do that, since this enum might change (it did for example with Qt 5.3) and then your application won't catch the new languages until you manually update the iteration range.
来源:https://stackoverflow.com/questions/26307729/get-list-of-languages-in-qt5