Add translation detection, translation class and report translation option. Leading up to user configurable report translations.
svn: r13391
This commit is contained in:
parent
c202f43e9b
commit
4f55365e97
@ -163,6 +163,7 @@ src/gen/plug/menu/_string.py
|
||||
src/gen/plug/menu/_style.py
|
||||
src/gen/plug/menu/_surnamecolor.py
|
||||
src/gen/plug/menu/_text.py
|
||||
src/gen/plug/menu/_translation.py
|
||||
src/gen/plug/docgen/__init__.py
|
||||
src/gen/plug/docgen/basedoc.py
|
||||
src/gen/plug/docgen/drawdoc.py
|
||||
|
@ -25,10 +25,26 @@
|
||||
Provide translation assistance
|
||||
"""
|
||||
|
||||
#------------------------------------------------------------------------
|
||||
#
|
||||
# python modules
|
||||
#
|
||||
#------------------------------------------------------------------------
|
||||
import gettext
|
||||
import os
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
# gramps modules
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
import const
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
# Public Constants
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
if "GRAMPSI18N" in os.environ:
|
||||
LOCALEDIR = os.environ["GRAMPSI18N"]
|
||||
elif os.path.exists( os.path.join(const.ROOT_DIR, "lang") ):
|
||||
@ -38,6 +54,11 @@ else:
|
||||
|
||||
LOCALEDOMAIN = 'gramps'
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
# Public Functions
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
def setup_gettext():
|
||||
"""
|
||||
Setup the gettext environment.
|
||||
@ -51,7 +72,27 @@ def setup_gettext():
|
||||
#following installs _ as a python function, we avoid this as TransUtils is
|
||||
#used sometimes:
|
||||
#gettext.install(LOCALEDOMAIN, LOCALEDIR, unicode=1)
|
||||
|
||||
def get_available_translations():
|
||||
"""
|
||||
Get a list of available translations.
|
||||
|
||||
:returns: A list of translation languages.
|
||||
:rtype: unicode[]
|
||||
|
||||
"""
|
||||
languages = ["en"]
|
||||
|
||||
for langdir in os.listdir(LOCALEDIR):
|
||||
mofilename = os.path.join( LOCALEDIR, langdir,
|
||||
"LC_MESSAGES", "%s.mo" % LOCALEDOMAIN )
|
||||
if os.path.exists(mofilename):
|
||||
languages.append(langdir)
|
||||
|
||||
languages.sort()
|
||||
|
||||
return languages
|
||||
|
||||
def sgettext(msgid, sep='|'):
|
||||
"""
|
||||
Strip the context used for resolving translation ambiguities.
|
||||
@ -102,3 +143,48 @@ def sngettext(singular, plural, n, sep='|'):
|
||||
sep_idx = singular.rfind(sep)
|
||||
msgval = singular[sep_idx+1:]
|
||||
return msgval
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
# Translator
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
class Translator:
|
||||
"""
|
||||
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 = None
|
||||
else:
|
||||
# fallback=True will cause the translator to use English if
|
||||
# lang = "en" or if something goes wrong.
|
||||
self.trans = gettext.translation(LOCALEDOMAIN, languages=[lang],
|
||||
fallback=True)
|
||||
|
||||
def gettext(self, message):
|
||||
"""
|
||||
Return the translated string.
|
||||
|
||||
:param message: The message to be translated.
|
||||
:type message: string
|
||||
:returns: The translated message
|
||||
:rtype: unicode
|
||||
|
||||
"""
|
||||
if self.trans is None:
|
||||
return gettext.gettext(message)
|
||||
else:
|
||||
return self.trans.gettext(message)
|
||||
|
@ -24,7 +24,8 @@ pkgdata_PYTHON = \
|
||||
_string.py \
|
||||
_style.py \
|
||||
_surnamecolor.py \
|
||||
_text.py
|
||||
_text.py \
|
||||
_translation.py
|
||||
|
||||
pkgpyexecdir = @pkgpyexecdir@/gen/plug/menu
|
||||
pkgpythondir = @pkgpythondir@/gen/plug/menu
|
||||
|
@ -39,3 +39,4 @@ from _placelist import PlaceListOption
|
||||
from _surnamecolor import SurnameColorOption
|
||||
from _destination import DestinationOption
|
||||
from _style import StyleOption
|
||||
from _translation import TranslationOption
|
||||
|
73
src/gen/plug/menu/_translation.py
Normal file
73
src/gen/plug/menu/_translation.py
Normal file
@ -0,0 +1,73 @@
|
||||
#
|
||||
# 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: _filter.py 12756 2009-07-02 20:01:28Z gbritton $
|
||||
|
||||
"""
|
||||
Option class representing a list of available translators.
|
||||
"""
|
||||
|
||||
#------------------------------------------------------------------------
|
||||
#
|
||||
# python modules
|
||||
#
|
||||
#------------------------------------------------------------------------
|
||||
from gettext import gettext as _
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
# gramps modules
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
from gen.plug.menu import EnumeratedListOption
|
||||
from TransUtils import get_available_translations, Translator
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
# TranslationOption class
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
class TranslationOption(EnumeratedListOption):
|
||||
"""
|
||||
This class describes an option that provides a list of available
|
||||
translations. Each possible value represents one of the possible
|
||||
translations.
|
||||
"""
|
||||
def __init__(self, label):
|
||||
"""
|
||||
@param label: A friendly label to be applied to this option.
|
||||
Example: "Translation"
|
||||
@type label: string
|
||||
@return: nothing
|
||||
"""
|
||||
EnumeratedListOption.__init__(self, label,
|
||||
Translator.DEFAULT_TRANSLATION_STR)
|
||||
|
||||
self.add_item(Translator.DEFAULT_TRANSLATION_STR, _("default"))
|
||||
for tran in get_available_translations():
|
||||
self.add_item(tran, tran)
|
||||
|
||||
def get_translator(self):
|
||||
"""
|
||||
Return a translator for the currently selected translation.
|
||||
|
||||
@return: a translator object
|
||||
@rtype: TransUtils.Translator
|
||||
"""
|
||||
return Translator(self.get_value())
|
Loading…
Reference in New Issue
Block a user