在Python中获取系统语言的ISO 639(三个字母代码)
2 个回答
3
你也可以使用 pycountry
,它可以在这个网址找到:http://pypi.python.org/pypi/pycountry/。这个库似乎包含了ISO 639 2的代码(我只是用谷歌查的 :-)
3
我猜你想要的是ISO 639 2,而不是ISO 639 3。可以从国会图书馆获取机器可读的数据(我在这个回答中使用的是“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()]