将语言代码转换为它们的本地语言名称在python中?

huangapple go评论78阅读模式
英文:

Convert language codes to their native language names in python?

问题

我知道如何根据语言代码获取语言的名称。但我不知道如何以其本土形式和本土字母获取该名称。例如,(south) Korean would be 한국말 or 한국어. Japanese would be 日本語.

import pycountry
ko = pycountry.languages.get(alpha_2='ko')
print(ko)

结果:

Language(alpha_2='ko', alpha_3='kor', name='Korean', scope='I', type='L')
英文:

I know how to get the name of a language based on its language code. But I don't know how to get that name in its native form with native letters. For example (south) Korean would be 한국말 or 한국어. Japanese would be 日本語.

import pycountry
ko = pycountry.languages.get(alpha_2='ko')
print(ko)

Result:

Language(alpha_2='ko', alpha_3='kor', name='Korean', scope='I', type='L')

答案1

得分: 1

你可以使用pycountry localesgettext模块:

import gettext
import pycountry

def get_native_name(code):
    translator = gettext.translation("iso639-3", pycountry.LOCALES_DIR, languages=[code])
    language = pycountry.languages.get(alpha_2=code)
    return translator.gettext(language.name)

print(get_native_name("ko"))
print(get_native_name("ja"))
英文:

You could use the pycountry locales with the gettext module:

import gettext
import pycountry

def get_native_name(code):
    translator = gettext.translation("iso639-3", pycountry.LOCALES_DIR, languages = [code])
    language = pycountry.languages.get(alpha_2 = code)
    return translator.gettext(language.name)

print(get_native_name("ko"))
print(get_native_name("ja"))

huangapple
  • 本文由 发表于 2023年7月20日 19:46:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定