英文:
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 locales与gettext
模块:
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"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论