Add translation detection, translation class and report translation option. Leading up to user configurable report translations.

svn: r13391
This commit is contained in:
Brian Matherly
2009-10-22 04:22:24 +00:00
parent c202f43e9b
commit 4f55365e97
5 changed files with 163 additions and 1 deletions

View File

@@ -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)