python - 在Python中获取ISO 639(3字母代码)中的系统语言

标签 python

有人可以告诉我一种在 ISO 639 (3 letter code) 中获取系统语言的方法吗?以跨平台方式格式化?

谢谢。

我找到了list三字母国家代码。

最佳答案

我假设你想要ISO 639 2而不是ISO 639 3这里。机器可读数据可从 Library of Congress 获取。 (我对此答案使用“utf-8”编码,另请参阅 http://www.loc.gov/standards/iso639-2/ascii_8bits.html 了解更多信息。)

以下是如何加载它的示例:

import codecs

def getisocodes_dict(data_path):
    # Provide a map from ISO code (both bibliographic and terminologic)
    # in ISO 639-2 to a dict with the two letter ISO 639-2 codes (alpha2)
    # English and french names
    #
    # "bibliographic" iso codes are derived from English word for the language
    # "terminologic" iso codes are derived from the pronunciation in the target 
    # language (if different to the bibliographic code)

    D = {}
    f = codecs.open(data_path, 'rb', 'utf-8')
    for line in f:
        iD = {}
        iD['bibliographic'], iD['terminologic'], iD['alpha2'], \
            iD['english'], iD['french'] = line.strip().split('|')
        D[iD['bibliographic']] = iD

        if iD['terminologic']:
            D[iD['terminologic']] = iD

        if iD['alpha2']:
            D[iD['alpha2']] = iD

        for k in iD:
            # Assign `None` when columns not available from the data
            iD[k] = iD[k] or None
    f.close()
    return D

if __name__ == '__main__':
    D = getisocodes_dict('ISO-639-2_utf-8.txt')
    print D['eng']
    print D['fr']

    # Print my current locale
    import locale
    print D[locale.getdefaultlocale()[0].split('_')[0].lower()]

关于python - 在Python中获取ISO 639(3字母代码)中的系统语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2879856/

相关文章:

python - 将所有文档的列更新为Elasticsearch的最佳实践

python - 创建 self 循环的链条

python - 通过索引更改值字典

python - 如何从远程主机连接到 Tor 控制端口 (9051)?

python - 在 python 的静态方法中使用 AssertEquals

python - 看似快速的过滤字段查找却很慢

python - 在列表中搜索字符串

python - 结合 Tkinter 和 win32ui 使 Python 在退出时崩溃

Python 将列表值从索引复制到另一个

python - 在for循环python中捕获错误