From d674eed2d4e49910ea045729c7014939e63faa79 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sat, 13 Apr 2013 14:35:30 +0000 Subject: [PATCH] [r21967]GrampsLocale: Integrate libtranslate functions into GrampsLocale And remove libtranslate svn: r21984 --- gramps/gen/plug/report/_reportbase.py | 18 +- gramps/gen/utils/grampslocale.py | 8 +- gramps/plugins/lib/libnarrate.py | 15 +- gramps/plugins/lib/libplugins.gpr.py | 17 -- gramps/plugins/lib/libtranslate.py | 166 ------------------ gramps/plugins/textreport/ancestorreport.py | 4 +- .../plugins/textreport/detancestralreport.py | 4 +- .../plugins/textreport/detdescendantreport.py | 4 +- 8 files changed, 25 insertions(+), 211 deletions(-) delete mode 100644 gramps/plugins/lib/libtranslate.py diff --git a/gramps/gen/plug/report/_reportbase.py b/gramps/gen/plug/report/_reportbase.py index 1bd3b27eb..769690913 100644 --- a/gramps/gen/plug/report/_reportbase.py +++ b/gramps/gen/plug/report/_reportbase.py @@ -28,7 +28,7 @@ # Report # #------------------------------------------------------------------------- -from gramps.plugins.lib.libtranslate import Translator +from gramps.gen.utils.grampslocale import GrampsLocale class Report(object): """ @@ -56,18 +56,18 @@ class Report(object): def begin_report(self): pass - def set_translation(self, language): + def set_locale(self, language): """ Set the translator to one selected with stdoptions.add_localization_option(). """ - if not language: - return - trans = Translator(lang=language) - self._ = trans.gettext - self.__get_date = trans.get_date - self.__get_type = trans.get_type - return trans + if language == GrampsLocale.DEFAULT_TRANSLATION_STR: + language = None + locale = GrampsLocale(lang=language) + self._ = locale.translation.gettext + self.__get_date = locale.get_date + self.__get_type = locale.get_type + return locale def write_report(self): pass diff --git a/gramps/gen/utils/grampslocale.py b/gramps/gen/utils/grampslocale.py index 1c91448c8..6ca2dcc0d 100644 --- a/gramps/gen/utils/grampslocale.py +++ b/gramps/gen/utils/grampslocale.py @@ -477,9 +477,6 @@ class GrampsLocale(object): else: self._init_secondary_locale() - - - self.icu_locales = {} self.collator = None if HAVE_ICU: @@ -496,14 +493,15 @@ class GrampsLocale(object): self.translation = self._get_translation(self.localedomain, self.localedir, self.language) + # This is a no-op for secondaries but needs the translation + # set, so it needs to be here. self._set_dictionaries() - if _hdlr: LOG.removeHandler(_hdlr) self._dd = self._dp = None - + #Guards against running twice on the first instance. self.initialized = True def _get_translation(self, domain = None, diff --git a/gramps/plugins/lib/libnarrate.py b/gramps/plugins/lib/libnarrate.py index 10840570b..9e3afeeb2 100644 --- a/gramps/plugins/lib/libnarrate.py +++ b/gramps/plugins/lib/libnarrate.py @@ -42,7 +42,7 @@ from gramps.gen.lib.familyreltype import FamilyRelType from gramps.gen.display.name import displayer as _nd from gramps.gen.utils.alive import probably_alive from gramps.gen.plug.report import utils as ReportUtils -from gramps.plugins.lib.libtranslate import Translator +from gramps.gen.const import GRAMPS_LOCALE as glocale #------------------------------------------------------------------------- # @@ -1349,7 +1349,7 @@ class Narrator(object): def __init__(self, dbase, verbose=True, use_call_name=False,use_fulldate=False, empty_date="", empty_place="", - translator=None, + locale=None, get_endnote_numbers=_get_empty_endnote_numbers): """ Initialize the narrator class. @@ -1386,12 +1386,11 @@ class Narrator(object): self.__person = None self.__first_name = "" self.__first_name_used = False - - if translator is None: - translator = Translator(Translator.DEFAULT_TRANSLATION_STR) - - self.__translate_text = translator.gettext - self.__get_date = translator.get_date + + if locale is None: + locale = glocale + self.__translate_text = locale.translation.gettext + self.__get_date = locale.get_date def set_subject(self, person): """ diff --git a/gramps/plugins/lib/libplugins.gpr.py b/gramps/plugins/lib/libplugins.gpr.py index c3d480f22..88c01664c 100644 --- a/gramps/plugins/lib/libplugins.gpr.py +++ b/gramps/plugins/lib/libplugins.gpr.py @@ -199,23 +199,6 @@ authors = ["The Gramps project"], authors_email = ["http://gramps-project.org"], ) -#------------------------------------------------------------------------ -# -# libtranslate -# -#------------------------------------------------------------------------ -register(GENERAL, -id = 'libtranslate', -name = "translation lib", -description = _("Provides Textual Translation.") , -version = '1.0', -gramps_target_version = '4.0', -status = STABLE, -fname = 'libtranslate.py', -authors = ["Brian Matherly"], -authors_email = ["brian@gramps-project.org"], -) - #------------------------------------------------------------------------ # # libpersonview diff --git a/gramps/plugins/lib/libtranslate.py b/gramps/plugins/lib/libtranslate.py deleted file mode 100644 index fe5a8da02..000000000 --- a/gramps/plugins/lib/libtranslate.py +++ /dev/null @@ -1,166 +0,0 @@ -# -# Gramps - a GTK+/GNOME based genealogy program -# -# Copyright (C) 2009 Brian G. Matherly -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# - -# $Id$ - -""" -Translator class for use by plugins. -""" - -#------------------------------------------------------------------------ -# -# python modules -# -#------------------------------------------------------------------------ - -#------------------------------------------------------------------------ -# -# GRAMPS modules -# -#------------------------------------------------------------------------ -from gramps.gen.const import GRAMPS_LOCALE as glocale -_ = glocale.get_translation().gettext -from gramps.gen.datehandler import displayer, LANG_TO_DISPLAY -from gramps.gen.config import config -from gramps.gen.lib.grampstype import GrampsType -from gramps.gen.constfunc import cuni - - -#------------------------------------------------------------------------- -# -# Translator -# -#------------------------------------------------------------------------- -class Translator(object): - """ - This class provides translated strings for the configured language. - """ - DEFAULT_TRANSLATION_STR = "default" - - def __init__(self, lang=DEFAULT_TRANSLATION_STR): - """ - :param lang: The language to translate to. - The language can be: - * The name of any installed .mo file - * "en" to use the message strings in the code - * "default" to use the default translation being used by gettext. - :type lang: string - :return: nothing - - """ - if lang == Translator.DEFAULT_TRANSLATION_STR: - self.__trans = glocale.get_translation() - self.__dd = displayer - else: - # If lang isn't supported, this will fallback to the - # current global language - self.__trans = glocale.get_translation(languages=[lang]) - val = config.get('preferences.date-format') - if lang in LANG_TO_DISPLAY: - self.__dd = LANG_TO_DISPLAY[lang](val) - else: - self.__dd = displayer - - def gettext(self, message): - """ - Return the unicode translated string. - - :param message: The message to be translated. - :type message: string - :returns: The translated message - :rtype: unicode - - """ - return self.__trans.gettext(message) - - def ngettext(self, singular, plural, n): - """ - Return the unicode translated singular/plural string. - - The translation of singular/plural is returned unless the translation is - not available and the singular contains the separator. In that case, - the returned value is the portion of singular following the last - separator. Default separator is '|'. - - :param singular: The singular form of the string to be translated. - may contain a context separator - :type singular: unicode - :param plural: The plural form of the string to be translated. - :type plural: unicode - :param n: the amount for which to decide the translation - :type n: int - :returns: The translated singular/plural message - :rtype: unicode - - """ - return self.__trans.ngettext(singular, plural, n) - - def sgettext(self, msgid, sep='|'): - """ - Strip the context used for resolving translation ambiguities. - - The translation of msgid is returned unless the translation is - not available and the msgid contains the separator. In that case, - the returned value is the portion of msgid following the last - separator. Default separator is '|'. - - :param msgid: The string to translated. - :type msgid: unicode - :param sep: The separator marking the context. - :type sep: unicode - :returns: Translation or the original with context stripped. - :rtype: unicode - - """ - try: - return self.__trans.sgettext(msgid) - except AttributeError: - return self.__trans.gettext(msgid) - - def get_date(self, date): - """ - Return a string representing the date appropriate for the language being - translated. - - :param date: The date to be represented. - :type date: :class:`~gen.lib.date.Date` - :returns: The date as text in the proper language. - :rtype: unicode - """ - return self.__dd.display(date) - - def get_type(self, name): - """ - Return a string representing the name appropriate for the language being - translated. - - :param name: The name type to be represented. - :returns: The name as text in the proper language. - :rtype: unicode - """ - return GrampsType.xml_str(name) - - # List of translated strings used here - # Dead code for l10n; added on translation template - # Translation string should be same as key name - # ex: AttributeType - #(FATHER_AGE , _("Father's Age"), "Father Age"), - #(MOTHER_AGE , _("Mother's Age"), "Mother Age"), - _("Father Age"), _("Mother Age") diff --git a/gramps/plugins/textreport/ancestorreport.py b/gramps/plugins/textreport/ancestorreport.py index 5b0bd12ef..ae0116af9 100644 --- a/gramps/plugins/textreport/ancestorreport.py +++ b/gramps/plugins/textreport/ancestorreport.py @@ -113,9 +113,9 @@ class AncestorReport(Report): self._name_display.set_default_format(name_format) lang = menu.get_option_by_name('trans').get_value() - translator = self.set_translation(lang) + locale = self.set_locale(lang) self.__narrator = Narrator(self.database, use_fulldate=True, - translator=translator) + locale=locale) def apply_filter(self, person_handle, index, generation=1): """ diff --git a/gramps/plugins/textreport/detancestralreport.py b/gramps/plugins/textreport/detancestralreport.py index 05a681ccd..d674839e6 100644 --- a/gramps/plugins/textreport/detancestralreport.py +++ b/gramps/plugins/textreport/detancestralreport.py @@ -159,10 +159,10 @@ class DetAncestorReport(Report): empty_place = "" lang = menu.get_option_by_name('trans').get_value() - translator = self.set_translation(lang) + locale = self.set_locale(lang) self.__narrator = Narrator(self.database, self.verbose, use_call, use_fulldate, empty_date, empty_place, - translator=translator, + locale=locale, get_endnote_numbers=self.endnotes) self.bibli = Bibliography(Bibliography.MODE_DATE|Bibliography.MODE_PAGE) diff --git a/gramps/plugins/textreport/detdescendantreport.py b/gramps/plugins/textreport/detdescendantreport.py index 087c54c06..378c5de95 100644 --- a/gramps/plugins/textreport/detdescendantreport.py +++ b/gramps/plugins/textreport/detdescendantreport.py @@ -180,11 +180,11 @@ class DetDescendantReport(Report): if name_format != 0: self._name_display.set_default_format(name_format) - translator = self.set_translation(get_value('trans')) + locale = self.set_locale(get_value('trans')) self.__narrator = Narrator(self.database, self.verbose, use_call, use_fulldate, empty_date, empty_place, - translator=translator, + locale=locale, get_endnote_numbers=self.endnotes)