获取Qt5中的语言列表 [英] Get list of languages in Qt5

查看:354
本文介绍了获取Qt5中的语言列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从QLocale获取ISO 639语言的列表.我可以使用此代码来获取语言/国家/地区的所有组合.

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?

推荐答案

您可以执行此操作,也可以执行不尽如人意的操作(请参见本文结尾),并手动从列表中过滤出重复的语言,例如如果要使用ISO 639语言名称:

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包含由ISO 639分类并由Qt已知的所有语言的名称.请注意,它确实包含语言名称(例如德语)和 not ISO 639代码(例如de).

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).

如果您需要ISO 639代码,请执行以下操作:

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;

还可以通过手动遍历QLocale::Language枚举然后解析结果来构造QLocale对象,但但是我强烈建议不要这样做,因为此枚举可能会更改(它确实例如使用Qt 5.3),那么在您手动更新迭代范围之前,您的应用程序将无法捕获新语言.

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.

这篇关于获取Qt5中的语言列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆