Consolidate mslocales and self._lang_map

into a single dict for easier maintenance

Thanks to Paul Franklin for suggesting it

svn: r22840
This commit is contained in:
John Ralls 2013-08-09 22:11:58 +00:00
parent b8f62b8917
commit 5880dc49f6

View File

@ -54,66 +54,69 @@ ICU_LOCALES = None
if HAVE_ICU: if HAVE_ICU:
ICU_LOCALES = Locale.getAvailableLocales() ICU_LOCALES = Locale.getAvailableLocales()
#Map our translated language codes to Microsoft Locale Names # Map of languages for converting to Microsoft locales and naming
#Important: Maintain this list with new translations or they # locales for display to the user. It's important to add to this list
#won't work under MS Windows! # when a new translation is added. Note the dummy _(): That's just to
mslocales = { # get xgettext to include the string in gramps.pot; actual translation
'ar': ('Arabic_Saudi Arabia', '1256'), # is done in _get_language_string() below.
'bg': ('Bulgrian_Bulgaria', '1251'), _ = lambda x: x
'br': None, #Windows has no translation for Breton _LOCALE_NAMES = {
'ca': ('Catalang_Spain', '1252'), 'ar': ('Arabic_Saudi Arabia', '1256', _("Arabic")),
'cs': ('Czech_Czech Republic', '1250'), 'bg': ('Bulgrian_Bulgaria', '1251', _("Bulgarian")),
'da': ('Danish_Denmark', '1252'), 'br': (None, None, _("Breton")), #Windows has no translation for Breton
'de': ('German_Germany', '1252'), 'ca': ('Catalan_Spain', '1252', _("Catalan")),
'en': ('English_United States', '1252'), 'cs': ('Czech_Czech Republic', '1250', _("Czech")),
'en_GB': ('English_United Kingdom', '1252'), 'da': ('Danish_Denmark', '1252', _("Danish")),
'eo': None, #Windows has no translation for Esperanto 'de': ('German_Germany', '1252', _("German")),
'es': ('Spanish_Spain', '1252'), 'el': ('Greek_Greece', '1253', _("Greek")),
'fi': ('Finnish_Finland', '1252'), 'en': ('English_United States', '1252', _("English (USA)")),
'fr': ('French_France', '1252'), 'en_GB': ('English_United Kingdom', '1252', _("English")),
'ga': None, #Windows has no translation for Gaelic 'eo': (None, None, _("Esperanto")), #Windows has no translation for Esperanto
'gr': ('Greek_Greece', '1253'), 'es': ('Spanish_Spain', '1252', _("Spanish")),
'he': ('Hebrew_Israel', '1255'), 'fi': ('Finnish_Finland', '1252', _("Finnish")),
'hr': ('Croatian_Croatia', '1250'), 'fr': ('French_France', '1252', _("French")),
'hu': ('Hungarian_Hungary', '1250'), 'ga': (None, None, _("Gaelic")), #Windows has no translation for Gaelic
'it': ('Italian_Italy', '1252'), 'he': ('Hebrew_Israel', '1255', _("Hebrew")),
'ja': ('Japanese_Japan', '932'), 'hr': ('Croatian_Croatia', '1250', _("Croatian")),
'lt': ('Lithuanian_Lithuania', '1252'), 'hu': ('Hungarian_Hungary', '1250', _("Hungarian")),
'mk': None, #Windows has no translation for Macedonian 'it': ('Italian_Italy', '1252', _("Italian")),
'nb': ('Norwegian_Norway', '1252'), 'ja': ('Japanese_Japan', '932', _("Japanese")),
'nl': ('Dutch_Netherlands', '1252'), 'lt': ('Lithuanian_Lithuania', '1252', _("Lithuanian")),
'nn': ('Norwegian-Nynorsk_Norway', '1252'), 'mk': (None, None, _("Macedonian")), #Windows has no translation for Macedonian
'pl': ('Polish_Poland', '1250'), 'nb': ('Norwegian_Norway', '1252', _("Norwegian Bokmal")),
'pt_BR': ('Portuguese_Brazil', '1252'), 'nl': ('Dutch_Netherlands', '1252', _("Dutch")),
'pt_PT': ('Portuguese_Portugal', '1252'), 'nn': ('Norwegian-Nynorsk_Norway', '1252', _("Norwegian Nynorsk")),
'ro': ('Romanian_Romania', '1250'), 'pl': ('Polish_Poland', '1250', _("Polish")),
'ru': ('Russian_Russia', '1251'), 'pt_BR': ('Portuguese_Brazil', '1252', _("Portuguese (Brazil)")),
'sk': ('Slovak_Slovakia', '1250'), 'pt_PT': ('Portuguese_Portugal', '1252', _("Portuguese (Portugal)")),
'sl': ('Slovenian_Slovenia', '1250'), 'ro': ('Romanian_Romania', '1250', _("Romanian")),
'sq': ('Albanian_Albania', '1250'), 'ru': ('Russian_Russia', '1251', _("Russian")),
'sr': ('Serbian(Cyrillic)_Serbia and Montenegro', '1251'), 'sk': ('Slovak_Slovakia', '1250', _("Slovak"),),
'sv': ('Swedish_Sweden', '1252'), 'sl': ('Slovenian_Slovenia', '1250', _("Slovenian")),
'tr': ('Turkish_Turkey', '1254'), 'sq': ('Albanian_Albania', '1250', _("Albanian")),
'uk': ('Ukrainian_Ukraine', '1251'), 'sr': ('Serbian(Cyrillic)_Serbia and Montenegro', '1251', None), #Gramps's Serbian translation is not yet ready
'vi': ('Vietnamese_Viet Nam', '1258'), 'sv': ('Swedish_Sweden', '1252', _("Swedish")),
'zh_CN': ('Chinese_China', '936'), 'tr': ('Turkish_Turkey', '1254', _("Turkish")),
'uk': ('Ukrainian_Ukraine', '1251', _("Ukrainian")),
'vi': ('Vietnamese_Viet Nam', '1258', _("Vietnamese")),
'zh_CN': ('Chinese_China', '936', _("Chinese Simplified")),
} }
def _check_mswin_locale(locale): def _check_mswin_locale(locale):
msloc = None msloc = None
try: try:
msloc = mslocales[locale[:5]] msloc = _LOCALE_NAMES[locale[:5]]
locale = locale[:5] locale = locale[:5]
except KeyError: except KeyError:
try: try:
msloc = mslocales[locale[:2]] msloc = _LOCALE_NAMES[locale[:2]]
locale = locale[:2] locale = locale[:2]
except KeyError: except KeyError:
return (None, None) return (None, None)
return (locale, msloc) return (locale, msloc)
def _check_mswin_locale_reverse(locale): def _check_mswin_locale_reverse(locale):
for (loc, msloc) in mslocales.items(): for (loc, msloc) in _LOCALE_NAMES.items():
if msloc and locale == msloc[0]: if msloc and locale == msloc[0]:
return (loc, msloc[1]) return (loc, msloc[1])
@ -545,10 +548,6 @@ class GrampsLocale(object):
self.translation = GrampsNullTranslations() self.translation = GrampsNullTranslations()
self.translation._language = "en" self.translation._language = "en"
# This is a no-op for secondaries but needs the translation
# set, so it needs to be here.
self._set_dictionaries()
if _hdlr: if _hdlr:
LOG.removeHandler(_hdlr) LOG.removeHandler(_hdlr)
@ -599,72 +598,26 @@ class GrampsLocale(object):
code. Note that _lang_map and _country_map are class code. Note that _lang_map and _country_map are class
variables, so this function is no-op in secondary locales. variables, so this function is no-op in secondary locales.
""" """
_ = self.translation.gettext
if not self._lang_map:
self._lang_map = {
"ar" : _("Arabic"),
"bg" : _("Bulgarian"),
"ca" : _("Catalan"),
"cs" : _("Czech"),
"da" : _("Danish"),
"de" : _("German"),
"el" : _("Greek"),
"en" : _("English"),
"eo" : _("Esperanto"),
"es" : _("Spanish"),
"fi" : _("Finnish"),
"fr" : _("French"),
"he" : _("Hebrew"),
"hr" : _("Croatian"),
"hu" : _("Hungarian"),
"it" : _("Italian"),
"ja" : _("Japanese"),
"lt" : _("Lithuanian"),
"mk" : _("Macedonian"),
"nb" : _("Norwegian Bokmal"),
"nl" : _("Dutch"),
"nn" : _("Norwegian Nynorsk"),
"pl" : _("Polish"),
"pt" : _("Portuguese"),
"ro" : _("Romanian"),
"ru" : _("Russian"),
"sk" : _("Slovak"),
"sl" : _("Slovenian"),
"sq" : _("Albanian"),
"sv" : _("Swedish"),
"tr" : _("Turkish"),
"uk" : _("Ukrainian"),
"vi" : _("Vietnamese"),
"zh" : _("Chinese")
}
if not self._country_map:
self._country_map = {
"BR" : _("Brazil"),
"CN" : _("China"),
"PT" : _("Portugal")
}
def _get_language_string(self, lang_code): def _get_language_string(self, lang_code):
""" """
Given a language code of the form "lang_region", return a text string Given a language code of the form "lang_region", return a text string
representing that language. representing that language.
""" """
code_parts = lang_code.rsplit("_") try:
lang = _LOCALE_NAMES[lang_code][2]
lang = code_parts[0] except KeyError:
try:
if lang in self._lang_map: lang = _LOCALE_NAMES[lang_code[:2]][2]
lang = self._lang_map[lang] except KeyError:
LOG.debug("Gramps has no translation for %s", lang_code)
country = None lang = None
if len(code_parts) > 1: except IndexError as err:
country = code_parts[1] LOG.debug("Bad Index for tuple %s\n" % _LOCALE_NAMES[lang_code][0])
if country in self._country_map: lang = None
country = self._country_map[country]
lang = "%(language)s (%(country)s)" % \
{ 'language' : lang, 'country' : country }
if lang:
return self.translation.gettext(lang)
return lang return lang
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -845,12 +798,14 @@ class GrampsLocale(object):
return a dictionary of language names : codes for use by language return a dictionary of language names : codes for use by language
pickers. pickers.
''' '''
langs = {} # langs = {}
for code in self.get_available_translations(): # for code in self.get_available_translations():
langs[self._get_language_string(code)] = code # lang = self._get_language_string(code)
# if not lang is None:
return langs # langs[lang] = code
# return langs
return {self._get_language_string(code) : code for code in self.get_available_translations() if not self._get_language_string(code) is None}
def trans_objclass(self, objclass_str): def trans_objclass(self, objclass_str):
""" """