首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Python中获取ISO 639 (3个字母的代码)格式的系统语言

在Python中获取ISO 639 (3个字母的代码)格式的系统语言
EN

Stack Overflow用户
提问于 2010-05-21 14:00:40
回答 2查看 4.6K关注 0票数 2

谁能告诉我一种以跨平台方式获得ISO 639 (3 letter code)格式的系统语言的方法?

谢谢。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-05-21 14:22:38

我猜你想要的是ISO 639 2而不是ISO 639 3。机器可读的数据可以从Library of Congress获得(对于这个答案,我使用"utf-8“编码,有关更多信息,请参阅http://www.loc.gov/standards/iso639-2/ascii_8bits.html )。

下面是一个如何加载此代码的示例:

代码语言:javascript
运行
复制
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()]
票数 3
EN

Stack Overflow用户

发布于 2010-05-21 14:36:11

你也可以在http://pypi.python.org/pypi/pycountry/上使用pycountry,它似乎有ISO639 2代码(只需使用谷歌:-)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2879856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档