From efa53a0537b4f9041d283bd75ab8c8a0755fd68b Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sun, 24 Feb 2013 00:54:56 +0000 Subject: [PATCH] [r21236]GrampsLocale: Replace use of get_language_string ... with GrampsLocale.get_language_dict Permits sorting by localized language name. Also hides the language code, which the user doesn't really care about. Removes get_language_string from libtranslate.py, no longer needed. svn: r21411 --- gramps/gen/utils/grampslocale.py | 83 +++++++++++++++++++ gramps/plugins/lib/libtranslate.py | 73 ---------------- gramps/plugins/textreport/ancestorreport.py | 7 +- .../plugins/textreport/detancestralreport.py | 7 +- .../plugins/textreport/detdescendantreport.py | 7 +- 5 files changed, 95 insertions(+), 82 deletions(-) diff --git a/gramps/gen/utils/grampslocale.py b/gramps/gen/utils/grampslocale.py index fa1201576..da9eb7d78 100644 --- a/gramps/gen/utils/grampslocale.py +++ b/gramps/gen/utils/grampslocale.py @@ -223,6 +223,7 @@ class GrampsLocale(object): self.translation = self._get_translation(self.localedomain, self.localedir, self.language) + self._set_dictionaries() @@ -251,6 +252,76 @@ class GrampsLocale(object): domain, languages, localedir) return GrampsNullTranslations() + def _set_dictionaries(self): + """ + Create a dictionary of language names localized to the + GrampsLocale's primary language, keyed by language and + country code. + """ + _ = self.translation.gettext + self.lang_map = { + "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") + } + + self.country_map = { + "BR" : _("Brazil"), + "CN" : _("China"), + "PT" : _("Portugal") + } + + def _get_language_string(self, lang_code): + """ + Given a language code of the form "lang_region", return a text string + representing that language. + """ + code_parts = lang_code.rsplit("_") + + lang = code_parts[0] + if lang in self.lang_map: + lang = self.lang_map[lang] + + country = None + if len(code_parts) > 1: + country = code_parts[1] + if country in self.country_map: + country = self.country_map[country] + lang = "%(language)s (%(country)s)" % \ + { 'language' : lang, 'country' : country } + + return lang + #------------------------------------------------------------------------- # # Public Functions @@ -368,6 +439,18 @@ class GrampsLocale(object): return languages + def get_language_dict(self): + ''' + return a dictionary of language names : codes for use by language + pickers. + ''' + langs = {} + for code in self.get_available_translations(): + langs[self._get_language_string(code)] = code + + return langs + + def trans_objclass(self, objclass_str): """ Translates objclass_str into "... %s", where objclass_str diff --git a/gramps/plugins/lib/libtranslate.py b/gramps/plugins/lib/libtranslate.py index 436e62894..fe5a8da02 100644 --- a/gramps/plugins/lib/libtranslate.py +++ b/gramps/plugins/lib/libtranslate.py @@ -42,79 +42,6 @@ from gramps.gen.config import config from gramps.gen.lib.grampstype import GrampsType from gramps.gen.constfunc import cuni -#------------------------------------------------------------------------ -# -# Private Constants -# -#------------------------------------------------------------------------ -_LANG_MAP = { - "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") -} - -_COUNTRY_MAP = { - "BR" : _("Brazil"), - "CN" : _("China"), - "PT" : _("Portugal") -} - -#------------------------------------------------------------------------ -# -# Public Functions -# -#------------------------------------------------------------------------ -def get_language_string(lang_code): - """ - Given a language code of the form "lang_region", return a text string - representing that language. - """ - code_parts = lang_code.rsplit("_") - - lang = code_parts[0] - if lang in _LANG_MAP: - lang = _LANG_MAP[lang] - - if len(code_parts) > 1: - country = code_parts[1] - if country in _COUNTRY_MAP: - country = _COUNTRY_MAP[country] - retstr = _("%(language)s (%(country)s)") % \ - { 'language' : lang, 'country' : country } - else: - retstr = lang - - return retstr #------------------------------------------------------------------------- # diff --git a/gramps/plugins/textreport/ancestorreport.py b/gramps/plugins/textreport/ancestorreport.py index e1301122b..c5ea761dc 100644 --- a/gramps/plugins/textreport/ancestorreport.py +++ b/gramps/plugins/textreport/ancestorreport.py @@ -53,7 +53,7 @@ from gramps.gen.plug.report import Report from gramps.gen.plug.report import utils as ReportUtils from gramps.gen.plug.report import MenuReportOptions from gramps.plugins.lib.libnarrate import Narrator -from gramps.plugins.lib.libtranslate import Translator, get_language_string +from gramps.plugins.lib.libtranslate import Translator #------------------------------------------------------------------------ # @@ -300,8 +300,9 @@ class AncestorOptions(MenuReportOptions): trans = EnumeratedListOption(_("Translation"), Translator.DEFAULT_TRANSLATION_STR) trans.add_item(Translator.DEFAULT_TRANSLATION_STR, _("Default")) - for language in glocale.get_available_translations(): - trans.add_item(language, get_language_string(language)) + languages = glocale.get_language_dict() + for language in sorted(languages): + trans.add_item(languages[language], language) trans.set_help(_("The translation to be used for the report.")) menu.add_option(category_name, "trans", trans) diff --git a/gramps/plugins/textreport/detancestralreport.py b/gramps/plugins/textreport/detancestralreport.py index 6515b621c..0d1e51040 100644 --- a/gramps/plugins/textreport/detancestralreport.py +++ b/gramps/plugins/textreport/detancestralreport.py @@ -56,7 +56,7 @@ from gramps.gen.plug.report import endnotes from gramps.gen.plug.report import utils as ReportUtils from gramps.gen.plug.report import MenuReportOptions from gramps.plugins.lib.libnarrate import Narrator -from gramps.plugins.lib.libtranslate import Translator, get_language_string +from gramps.plugins.lib.libtranslate import Translator #------------------------------------------------------------------------ # @@ -754,8 +754,9 @@ class DetAncestorOptions(MenuReportOptions): trans = EnumeratedListOption(_("Translation"), Translator.DEFAULT_TRANSLATION_STR) trans.add_item(Translator.DEFAULT_TRANSLATION_STR, _("Default")) - for language in glocale.get_available_translations(): - trans.add_item(language, get_language_string(language)) + languages = glocale.get_language_dict() + for language in sorted(languages): + trans.add_item(languages[language], language) trans.set_help(_("The translation to be used for the report.")) addopt("trans", trans) diff --git a/gramps/plugins/textreport/detdescendantreport.py b/gramps/plugins/textreport/detdescendantreport.py index 47d32f94e..160028270 100644 --- a/gramps/plugins/textreport/detdescendantreport.py +++ b/gramps/plugins/textreport/detdescendantreport.py @@ -60,7 +60,7 @@ from gramps.gen.plug.report import endnotes from gramps.gen.plug.report import utils as ReportUtils from gramps.gen.plug.report import MenuReportOptions from gramps.plugins.lib.libnarrate import Narrator -from gramps.plugins.lib.libtranslate import Translator, get_language_string +from gramps.plugins.lib.libtranslate import Translator #------------------------------------------------------------------------ # @@ -929,8 +929,9 @@ class DetDescendantOptions(MenuReportOptions): trans = EnumeratedListOption(_("Translation"), Translator.DEFAULT_TRANSLATION_STR) trans.add_item(Translator.DEFAULT_TRANSLATION_STR, _("Default")) - for language in glocale.get_available_translations(): - trans.add_item(language, get_language_string(language)) + languages = glocale.get_language_dict() + for language in sorted(languages): + trans.add_item(languages[language], language) trans.set_help(_("The translation to be used for the report.")) add_option("trans", trans)