2005-12-06 12:08:09 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-03-17 01:54:27 +05:30
|
|
|
# Copyright (C) 2000-2006 Donald N. Allingham
|
2009-10-19 03:48:29 +05:30
|
|
|
# Copyright (C) 2009 Brian G. Matherly
|
2005-12-06 12:08:09 +05:30
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
2008-02-24 19:25:55 +05:30
|
|
|
# $Id:TransUtils.py 9912 2008-01-22 09:17:46Z acraphae $
|
2005-12-06 12:08:09 +05:30
|
|
|
|
2007-09-10 08:33:46 +05:30
|
|
|
"""
|
2008-02-24 19:25:55 +05:30
|
|
|
Provide translation assistance
|
2007-09-10 08:33:46 +05:30
|
|
|
"""
|
|
|
|
|
2009-10-22 09:52:24 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# python modules
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
2010-01-08 21:24:47 +05:30
|
|
|
import gettext as translate
|
2010-01-17 19:14:53 +05:30
|
|
|
import sys
|
2009-10-19 03:48:29 +05:30
|
|
|
import os
|
2009-10-22 09:52:24 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# gramps modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-10-19 03:48:29 +05:30
|
|
|
import const
|
2009-07-16 17:53:51 +05:30
|
|
|
|
2009-10-22 09:52:24 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Public Constants
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-10-19 03:48:29 +05:30
|
|
|
if "GRAMPSI18N" in os.environ:
|
|
|
|
LOCALEDIR = os.environ["GRAMPSI18N"]
|
|
|
|
elif os.path.exists( os.path.join(const.ROOT_DIR, "lang") ):
|
|
|
|
LOCALEDIR = os.path.join(const.ROOT_DIR, "lang")
|
|
|
|
else:
|
|
|
|
LOCALEDIR = os.path.join(const.PREFIXDIR, "share/locale")
|
2009-07-16 17:53:51 +05:30
|
|
|
|
2009-10-19 03:48:29 +05:30
|
|
|
LOCALEDOMAIN = 'gramps'
|
2009-07-16 17:53:51 +05:30
|
|
|
|
2009-10-22 09:52:24 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Public Functions
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-10-19 03:48:29 +05:30
|
|
|
def setup_gettext():
|
2009-07-16 17:53:51 +05:30
|
|
|
"""
|
2009-10-19 03:48:29 +05:30
|
|
|
Setup the gettext environment.
|
2009-07-16 17:53:51 +05:30
|
|
|
|
2009-10-19 03:48:29 +05:30
|
|
|
:returns: Nothing.
|
|
|
|
|
|
|
|
"""
|
2010-01-08 21:24:47 +05:30
|
|
|
translate.bindtextdomain(LOCALEDOMAIN, LOCALEDIR)
|
|
|
|
translate.textdomain(LOCALEDOMAIN)
|
2009-07-16 17:53:51 +05:30
|
|
|
|
|
|
|
#following installs _ as a python function, we avoid this as TransUtils is
|
|
|
|
#used sometimes:
|
2009-10-19 03:48:29 +05:30
|
|
|
#gettext.install(LOCALEDOMAIN, LOCALEDIR, unicode=1)
|
2009-12-11 11:25:30 +05:30
|
|
|
|
|
|
|
def get_localedomain():
|
|
|
|
"""
|
|
|
|
Get the LOCALEDOMAIN used for the Gramps application.
|
|
|
|
"""
|
|
|
|
return LOCALEDOMAIN
|
2009-11-01 19:56:21 +05:30
|
|
|
|
2010-01-17 19:14:53 +05:30
|
|
|
def get_addon_translator(filename=None, domain="addon"):
|
2009-11-01 19:56:21 +05:30
|
|
|
"""
|
|
|
|
Get a translator for an addon.
|
2010-01-17 19:14:53 +05:30
|
|
|
|
|
|
|
filename - filename of a file in directory with full path, or
|
|
|
|
None to get from running code
|
|
|
|
domain - the name of the .mo file under the LANG/LC_MESSAGES dir
|
|
|
|
returns - a gettext.translation object
|
2009-11-05 04:50:35 +05:30
|
|
|
|
|
|
|
The return object has the following properties and methods:
|
|
|
|
.gettext
|
|
|
|
.info
|
|
|
|
.lgettext
|
|
|
|
.lngettext
|
|
|
|
.ngettext
|
|
|
|
.output_charset
|
|
|
|
.plural
|
|
|
|
.set_output_charset
|
|
|
|
.ugettext
|
|
|
|
.ungettext
|
|
|
|
|
|
|
|
Assumes path/filename
|
|
|
|
path/locale/LANG/LC_MESSAGES/addon.mo.
|
2009-11-01 19:56:21 +05:30
|
|
|
"""
|
2010-01-17 19:14:53 +05:30
|
|
|
if filename is None:
|
|
|
|
filename = sys._getframe(1).f_code.co_filename
|
2010-01-08 21:24:47 +05:30
|
|
|
gramps_translator = translate.translation(LOCALEDOMAIN, LOCALEDIR,
|
2009-11-05 04:50:35 +05:30
|
|
|
fallback=True)
|
2009-11-01 19:56:21 +05:30
|
|
|
path = os.path.dirname(os.path.abspath(filename))
|
2010-01-08 21:24:47 +05:30
|
|
|
addon_translator = translate.translation(domain, os.path.join(path,"locale"),
|
2009-11-05 04:50:35 +05:30
|
|
|
fallback=True)
|
|
|
|
gramps_translator.add_fallback(addon_translator)
|
|
|
|
return gramps_translator # with a language fallback
|
2009-10-22 09:52:24 +05:30
|
|
|
|
|
|
|
def get_available_translations():
|
|
|
|
"""
|
|
|
|
Get a list of available translations.
|
2006-03-17 01:54:27 +05:30
|
|
|
|
2009-10-22 09:52:24 +05:30
|
|
|
: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
|
2010-01-08 21:24:47 +05:30
|
|
|
|
|
|
|
def gettext(msgid):
|
|
|
|
"""
|
|
|
|
Obtain translation of gettext, return a unicode object
|
|
|
|
:param msgid: The string to translated.
|
|
|
|
:type msgid: unicode
|
|
|
|
:returns: Translation or the original with context stripped.
|
|
|
|
:rtype: unicode
|
|
|
|
"""
|
|
|
|
return unicode(translate.gettext(msgid))
|
2009-10-22 09:52:24 +05:30
|
|
|
|
2007-09-10 08:33:46 +05:30
|
|
|
def sgettext(msgid, sep='|'):
|
2005-12-06 12:08:09 +05:30
|
|
|
"""
|
|
|
|
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 '|'.
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
: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
|
2005-12-06 12:08:09 +05:30
|
|
|
|
|
|
|
"""
|
2010-01-08 21:24:47 +05:30
|
|
|
msgval = translate.gettext(msgid)
|
2006-03-17 01:54:27 +05:30
|
|
|
if msgval == msgid:
|
|
|
|
sep_idx = msgid.rfind(sep)
|
2005-12-06 12:08:09 +05:30
|
|
|
msgval = msgid[sep_idx+1:]
|
2010-01-08 21:24:47 +05:30
|
|
|
return unicode(msgval)
|
2009-01-31 14:04:15 +05:30
|
|
|
|
|
|
|
def sngettext(singular, plural, n, sep='|'):
|
|
|
|
"""
|
|
|
|
Strip the context used for resolving translation ambiguities.
|
|
|
|
|
|
|
|
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 '|'.
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:param singular: The singular form of the string to be translated.
|
2009-01-31 14:04:15 +05:30
|
|
|
may contain a context seperator
|
2009-06-25 03:26:07 +05:30
|
|
|
: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
|
|
|
|
:param sep: The separator marking the context.
|
|
|
|
:type sep: unicode
|
|
|
|
:returns: Translation or the original with context stripped.
|
|
|
|
:rtype: unicode
|
2009-01-31 14:04:15 +05:30
|
|
|
|
|
|
|
"""
|
2010-01-08 21:24:47 +05:30
|
|
|
msgval = translate.ngettext(singular, plural, n)
|
2009-01-31 14:04:15 +05:30
|
|
|
if msgval == singular:
|
|
|
|
sep_idx = singular.rfind(sep)
|
|
|
|
msgval = singular[sep_idx+1:]
|
2010-01-08 21:24:47 +05:30
|
|
|
return unicode(msgval)
|