Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the language name in native language?

Tags:

python

icu

This is my first time using ICU API, and I'm having a very hard time trying to find out something that I assumed to be very simple: to get a given locale/language name in the native language (instead of in English)

Examples:

fr    -> Français
en    -> English
pt_BR -> Português Brasileiro, or "Português (Brasil)"
es_ES -> Español Iberico, or "Español (España)"

As a reference, in babel I can get a given locale name in any language, native being the default:

>>> import babel
>>> locale = babel.Locale.parse('pt_BR')
>>> locale.get_display_name()
português (Brasil)
>>> locale.get_display_name('fr')
portugais (Brésil)
>>> locale.get_display_name('en')
Portuguese (Brazil)

So, how to do the same in ICU? Examples in python are most welcome, since I'm using PyICU, but Java/C/C++ is fine too, since my problem is with the API, and not the language.

Thanks!

like image 937
MestreLion Avatar asked May 01 '12 23:05

MestreLion


1 Answers

I've just found out: the best method is getDisplayName(), but, instead of passing an string as argument, I must pass... a Locale instance!

The complete code goes like:

>>> import icu
>>> locale = icu.Locale("pt_BR")
>>> print icu.getDisplayName()
u'portuguese (Brazil)'
>>> print icu.getDisplayName(locale)
u'portugu\xeas (Brasil)'

So, unlike babel, Locale methods by default return names in the user's current locale. I must pass the instance itself to get name in native language. So it's easy if you want names in your language (in my case, English), but if I wanted in French, for example, I would have to create a new Locale instance of 'fr_FR'. Weird API, but still...

like image 165
MestreLion Avatar answered Oct 04 '22 00:10

MestreLion