[r21962]GrampsLocale: Make dictionaries class variables

lang_map and country_map

svn: r21979
This commit is contained in:
John Ralls 2013-04-13 14:34:56 +00:00
parent 044452805b
commit 9f1ec6daf6

View File

@ -145,6 +145,8 @@ class GrampsLocale(object):
""" """
__first_instance = None __first_instance = None
encoding = None encoding = None
_lang_map = None
_country_map = None
def __new__(cls, localedir=None, lang=None, domain=None, languages=None): def __new__(cls, localedir=None, lang=None, domain=None, languages=None):
if not GrampsLocale.__first_instance: if not GrampsLocale.__first_instance:
@ -490,6 +492,9 @@ class GrampsLocale(object):
self.translation = self._get_translation(self.localedomain, self.translation = self._get_translation(self.localedomain,
self.localedir, self.language) self.localedir, self.language)
self._set_dictionaries()
if _hdlr: if _hdlr:
LOG.removeHandler(_hdlr) LOG.removeHandler(_hdlr)
@ -533,11 +538,13 @@ class GrampsLocale(object):
def _set_dictionaries(self): def _set_dictionaries(self):
""" """
Create a dictionary of language names localized to the Create a dictionary of language names localized to the
GrampsLocale's primary language, keyed by language and GrampsLocale's primary language, keyed by language and country
country code. code. Note that _lang_map and _country_map are class
variables, so this function is no-op in secondary locales.
""" """
_ = self.translation.gettext _ = self.translation.gettext
self.lang_map = { if not self._lang_map:
self._lang_map = {
"bg" : _("Bulgarian"), "bg" : _("Bulgarian"),
"ca" : _("Catalan"), "ca" : _("Catalan"),
"cs" : _("Czech"), "cs" : _("Czech"),
@ -573,7 +580,8 @@ class GrampsLocale(object):
"zh" : _("Chinese") "zh" : _("Chinese")
} }
self.country_map = { if not self._country_map:
self._country_map = {
"BR" : _("Brazil"), "BR" : _("Brazil"),
"CN" : _("China"), "CN" : _("China"),
"PT" : _("Portugal") "PT" : _("Portugal")
@ -587,17 +595,15 @@ class GrampsLocale(object):
code_parts = lang_code.rsplit("_") code_parts = lang_code.rsplit("_")
lang = code_parts[0] lang = code_parts[0]
if not hasattr(self, 'lang_map'):
self._set_dictionaries()
if lang in self.lang_map: if lang in self._lang_map:
lang = self.lang_map[lang] lang = self._lang_map[lang]
country = None country = None
if len(code_parts) > 1: if len(code_parts) > 1:
country = code_parts[1] country = code_parts[1]
if country in self.country_map: if country in self._country_map:
country = self.country_map[country] country = self._country_map[country]
lang = "%(language)s (%(country)s)" % \ lang = "%(language)s (%(country)s)" % \
{ 'language' : lang, 'country' : country } { 'language' : lang, 'country' : country }
@ -697,14 +703,15 @@ class GrampsLocale(object):
""" """
languages = ["en"] languages = ["en"]
if not localedir and hasattr(self, 'localedir'): if not localedir and self.localedir:
localedir = self.localedir localedir = self.localedir
else:
if localedir is None:
return languages return languages
if not localedomain and hasattr(self, 'localedomain'): if not localedomain and self.localedomain:
localedomain = self.localedomain localedomain = self.localedomain
else:
localedomain = 'gramps'
for langdir in os.listdir(self.localedir): for langdir in os.listdir(self.localedir):
mofilename = os.path.join(localedir, langdir, mofilename = os.path.join(localedir, langdir,