【问题标题】:Match language code with countries where this language is an official or commonly used language将语言代码与该语言为官方语言或常用语言的国家/地区匹配
【发布时间】:2010-04-21 05:47:35
【问题描述】:

是否有任何 python 库可以获取特定语言代码的国家/地区列表,其中该代码是官方或常用语言?

例如,语言代码“fr”与 29 个以法语为官方语言的国家和 8 个常用语言的国家/地区相关联。

【问题讨论】:

    标签: python localization country-codes


    【解决方案1】:

    尽管已接受答案,但据我所知,pycountry 底层的任何 xml 文件都不包含将语言映射到国家/地区的方法。它包含语言列表及其 iso 代码,国家列表及其 iso 代码,以及其他有用的东西,但并非如此。

    同样,Babel 包也很棒,但经过一段时间的研究,我找不到任何方法来列出特定国家/地区的所有语言。你能做的最好的就是“最有可能”的语言:https://stackoverflow.com/a/22199367/202168

    所以我必须自己去拿……

    import lxml.etree
    import urllib.request
    
    def get_territory_languages():
        url = "https://raw.githubusercontent.com/unicode-org/cldr/master/common/supplemental/supplementalData.xml"
        langxml = urllib.request.urlopen(url)
        langtree = lxml.etree.XML(langxml.read())
    
        territory_languages = {}
        for t in langtree.find('territoryInfo').findall('territory'):
            langs = {}
            for l in t.findall('languagePopulation'):
                langs[l.get('type')] = {
                    'percent': float(l.get('populationPercent')),
                    'official': bool(l.get('officialStatus'))
                }
            territory_languages[t.get('type')] = langs
        return territory_languages
    

    您可能希望将此结果存储在一个文件中,而不是每次需要时都通过网络调用。

    此数据集也包含“非官方”语言,您可能不想包含这些语言,这里有更多示例代码:

    TERRITORY_LANGUAGES = get_territory_languages()
    
    def get_official_locale_ids(country_code):
        country_code = country_code.upper()
        langs = TERRITORY_LANGUAGES[country_code].items()
        # most widely-spoken first:
        langs.sort(key=lambda l: l[1]['percent'], reverse=True)
        return [
            '{lang}_{terr}'.format(lang=lang, terr=country_code)
            for lang, spec in langs if spec['official']
        ]
    
    get_official_locale_ids('es')
    >>> ['es_ES', 'ca_ES', 'gl_ES', 'eu_ES', 'ast_ES']
    

    【讨论】:

    • 我无法访问提供的“xml”。请给我一些关于如何下载它的建议吗?
    • 看起来 url 已经改变了,我已经用新的 url 更新了答案(对于 Python 3)
    【解决方案2】:

    寻找 Babel 包。对于每个受支持的语言环境,它都有一个 pickle 文件。请参阅 localedata 模块中的 list() 函数以获取所有语言环境的列表。然后编写一些代码将语言环境拆分为(语言,国家)等

    【讨论】:

    • 使用babel.languages.get_territory_language_info()真的很简单
    • @Rmatt 令人惊讶的是,一个包在六年内变得更容易使用 :-)
    • 当然,这就是为什么我也赞成你的回答!你带来了一条不错的道路,只是让新手更精确;)
    • @Rmatt,您应该将此添加为答案。这是迄今为止最简单的方法!
    【解决方案3】:

    根据@NoahSantacruz 的要求,我将其添加为单独的答案,以便更容易获取。至少自 2017 年以来,最简单的方法是:

    babel.languages.get_territory_language_info()

    请参阅文档http://babel.pocoo.org/en/latest/api/languages.html#babel.languages.get_territory_language_info

    【讨论】:

      【解决方案4】:

      查看Ethnologue

      还是要小心……

      印度有a lot of official languages

      【讨论】:

        【解决方案5】:

        pycountry(说真的)。您可以从Package Index 获取。

        【讨论】:

        • 我只是查看了它的文档,您似乎无法提供语言代码,并获取使用该语言的所有国家/地区的列表
        • 可能值得再次检查——我之所以这么说是因为我将这个包用于类似的目的(货币)——我无法使用界面。相反,我必须直接使用包中提供的五个 XML 数据库。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多