gpt4 book ai didi

python - 在Python中获取ISO 639(3字母代码)中的系统语言

转载 作者:行者123 更新时间:2023-11-28 21:30:23 27 4
gpt4 key购买 nike

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

谢谢。

我找到了list三字母国家代码。

最佳答案

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

关于python - 在Python中获取ISO 639(3字母代码)中的系统语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2879856/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com