是否有任何python库可以获取特定语言代码的国家/地区列表,该语言代码是官方语言还是常用语言?将语言代码与此语言为官方或常用语言的国家/地区的语言代码匹配
例如,“fr”的语言代码与法语为官方语言的29个国家以及常用的8个国家相关联。
是否有任何python库可以获取特定语言代码的国家/地区列表,该语言代码是官方语言还是常用语言?将语言代码与此语言为官方或常用语言的国家/地区的语言代码匹配
例如,“fr”的语言代码与法语为官方语言的29个国家以及常用的8个国家相关联。
pycountry(严重)。你可以从Package Index得到它。
寻找Babel套餐。它为每个受支持的语言环境提供了一个pickle文件。请参阅localedata模块中的list()函数以获取所有语言环境的列表。然后写一些代码的语言环境分成(语言,国家)等等等等
尽管接受了答案,据我所知,pycountry下的所有xml文件都不包含映射语言到国家的方法。它包含语言和他们的iso代码列表,国家和他们的iso代码列表,以及其他有用的东西,但不是。
同样,巴别克包是伟大的,但经过一段时间后,我找不到任何方式列出所有语言为一个特定的国家。你能做的最好的是“最有可能的”语言:https://stackoverflow.com/a/22199367/202168
因此,我不得不把它自己...
def get_territory_languages():
import lxml
import urllib
langxml = urllib.urlopen('http://unicode.org/repos/cldr/trunk/common/supplemental/supplementalData.xml')
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']
我只是看看它的文档,它不似乎你可以提供一个语言代码,并获得所有使用该语言的国家列表 – 2010-04-21 06:08:25
可能值得再次检查 - 我之所以这么说是因为我使用这个软件包用于类似的目的(货币) - *但*我无法使用该界面。相反,我不得不直接使用包中提供的五个XML数据库。 – doug 2010-04-21 06:25:10
@ a_m0d:您可能需要自己编写一些代码。 – 2010-06-05 23:39:11