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

时间:2010-05-21 06:00:40

标签: python

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

感谢。

我找到了list三个字母的国家/地区代码。

3 个答案:

答案 0 :(得分:3)

我假设你想要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()]

答案 1 :(得分:3)

您还可以在http://pypi.python.org/pypi/pycountry/使用pycountry,{{3}}似乎有ISO 639 2代码(仅使用谷歌: - )

答案 2 :(得分:-1)

您可以使用getdefaultlocale function in the locale module。它返回元组中系统默认语言环境的语言代码和编码。然后你可以使用一小段额外的代码来获得ISO 639 2代码:

>>> import locale
>>> lang = list(locale.getdefaultlocale())
['en_GB', 'cp1252']
>>> lang = lang[1][0:2]
'en'