获取Python中ISO 639(三字母代码)中的系统语言

获取Python中ISO 639(三字母代码)中的系统语言,python,Python,有人能告诉我一种跨平台获取系统语言格式的方法吗 谢谢 我找到了一个由三个字母组成的国家代码。我想你是想要而不是在这里。机器可读的数据可以从中获得(我使用“utf-8”编码来回答这个问题,更多信息请参见) 以下是如何加载此文件的示例: import codecs def getisocodes_dict(data_path): # Provide a map from ISO code (both bibliographic and terminologic) # in ISO

有人能告诉我一种跨平台获取系统语言格式的方法吗

谢谢


我找到了一个由三个字母组成的国家代码。

我想你是想要而不是在这里。机器可读的数据可以从中获得(我使用“utf-8”编码来回答这个问题,更多信息请参见)

以下是如何加载此文件的示例:

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()]

你也可以使用
pycountry
,那里似乎有ISO 639 2代码(仅使用google:-)

我确实查找了pycountry,但它似乎依赖于
lxml
,我不想使用另一个库来完成一个小的琐碎任务。我确实找到了一个包含三个字母代码的列表。(看我修改过的帖子)我怎么能得到系统语言呢?然后我可以试着找出它的三个字母代码。谢谢(在我的电脑上提供
('en_AU','cp932')
,因此我将修改上面的示例以接受ISO 639 2代码)我认为现在一些linux发行版可能会使用ISO 639 3,但不要引用我的话-我只是想警告你-看起来Ubuntu首先使用了
ISO 639-1
两个字母,
ISO 639-2
,然后
ISO 639-3
,如果没有(可能不是非常通用的语言)(对不起,那应该是
接受ISO 639-1两个字母的代码
两个注释之前)