简体   繁体   English

获取Qt5中的语言列表

[英]Get list of languages in Qt5

I want to get the list of ISO 639 languages, from QLocale . 我想从QLocale获取ISO 639语言的列表。 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? 我假设可以手动过滤列表,但是是否存在更好的选择?

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, eg if you want the ISO 639 language names: 您可以执行此操作,也可以执行不尽人意的操作(请参阅本文结尾),并手动从列表中过滤重复的语言,例如,如果您想要ISO 639语言名称:

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. 然后, iso639Languages包含由ISO 639分类并由Qt已知的所有语言的名称。 Note that it does contain the name of the language (eg German) and not the ISO 639 code (eg de). 请注意,它确实包含语言名称(例如德语), 而不包含ISO 639代码(例如de)。

If you need the ISO 639 code do this instead: 如果您需要ISO 639代码,请执行以下操作:

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. 也可以通过在QLocale::Language枚举上手动迭代然后解析结果来构造QLocale对象, 我强烈建议不要这样做,因为此枚举可能会更改(例如Qt 5.3会更改),然后您的应用程序将赢得在您手动更新迭代范围之前,请不要使用新的语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM