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

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

Convert language codes to their native language names in python?

问题

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

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

结果:

  1. 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 日本語.

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

Result:

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

答案1

得分: 1

你可以使用pycountry localesgettext模块:

  1. import gettext
  2. import pycountry
  3. def get_native_name(code):
  4. translator = gettext.translation("iso639-3", pycountry.LOCALES_DIR, languages=[code])
  5. language = pycountry.languages.get(alpha_2=code)
  6. return translator.gettext(language.name)
  7. print(get_native_name("ko"))
  8. print(get_native_name("ja"))
英文:

You could use the pycountry locales with the gettext module:

  1. import gettext
  2. import pycountry
  3. def get_native_name(code):
  4. translator = gettext.translation("iso639-3", pycountry.LOCALES_DIR, languages = [code])
  5. language = pycountry.languages.get(alpha_2 = code)
  6. return translator.gettext(language.name)
  7. print(get_native_name("ko"))
  8. 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:

确定