c++ - QLocale,从语言名称到语言代码(2个字母)

标签 c++ qt

在 QLocale 中,

如何从语言名称中找到“两个字母的 ISO 639 语言代码”? 例如'English',它的双字母代码是:en。 'Chinese',其二字代码为:zh

如果我知道语言名称,例如“英语”,我们是否有返回其两个字母代码的功能,例如'恩'?

我查看了“QLocale”类,但没有找到用于此目的的方法。

最佳答案

您可以遍历所有语言环境以找到您需要的实例,然后使用 bcp47Name() 方法

QString strLang = "Chinese";
QList<QLocale> allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);
for (int iLocale = 0; iLocale < allLocales.count(); iLocale++)
{
    if (QLocale::languageToString(allLocales.at(iLocale).language()) == strLang)
    {
        if (allLocales.at(iLocale).bcp47Name().length() == 2) {
            qDebug() << allLocales.at(iLocale).bcp47Name();
        }
    }
}

关于c++ - QLocale,从语言名称到语言代码(2个字母),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56957340/

相关文章:

c++ - 将函数传递给 C++ 中的函数

android - Qt Necessitas 错误 - 找不到 Ministro 服务

c++ - Qt Creator 中的 Gotoline

c++ - QLocalSocket 真的用于命名管道吗

python - 如何设置 QTableWidget 在实时数据输入期间始终滚动到底部

c++ - 从 main 返回时出现段错误(非常简短的代码,没有数组或指针)

c++ - 引用指向 int 的指针会导致错误

c++ - 相同的测试用例,但 codeblocks 和 Ideone 上的输出不同

c++ - 打印链表元素

c++ - 'objects' 的 Qt 所有权(T* 和 const T*)