1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
"""
This module provides one function, languageName(), that returns
the human-readable name of a language from a codename (like 'nl').
The data is in the data.py file.
The file generate.py can be used by developers to (re-)generate
the data file.
Thanks go to the KDE developers for their translated language names
which are used currently in data.py.
"""
import itertools
import locale
from .data import language_names
__all__ = ['languageName']
def languageName(code, language=None):
"""Returns a human-readable name for a language.
The language must be given in the 'code' attribute.
The 'language' attribute specifies in which language
the name must be translated and defaults to the current locale.
"""
if language is None:
try:
language = locale.getdefaultlocale()[0]
except ValueError:
pass
langs = []
if language:
langs.append(language)
if '_' in language:
langs.append(language.split('_')[0])
langs.append("C")
codes = [code]
if '_' in code:
codes.append(code.split('_')[0])
for lang in langs:
try:
d = language_names[lang]
except KeyError:
continue
for c in codes:
try:
return d[c]
except KeyError:
continue
break
return code
|