[r21967]GrampsLocale: Integrate libtranslate functions into GrampsLocale
And remove libtranslate svn: r21984
This commit is contained in:
parent
751ac2aef0
commit
d674eed2d4
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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):
|
||||
"""
|
||||
|
@ -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
|
||||
|
@ -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")
|
@ -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):
|
||||
"""
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user