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-18 10:12:17 +05:30
|
|
|
import gettext
|
2010-01-17 19:14:53 +05:30
|
|
|
import sys
|
2009-10-19 03:48:29 +05:30
|
|
|
import os
|
2010-02-06 22:47:20 +05:30
|
|
|
import locale
|
2010-02-08 20:26:30 +05:30
|
|
|
|
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")
|
2010-04-03 17:29:53 +05:30
|
|
|
elif os.path.exists(os.path.join(const.PREFIXDIR, "share/locale")):
|
2009-10-19 03:48:29 +05:30
|
|
|
LOCALEDIR = os.path.join(const.PREFIXDIR, "share/locale")
|
2010-04-03 17:29:53 +05:30
|
|
|
else:
|
2010-08-26 18:45:09 +05:30
|
|
|
lang = os.environ.get('LANG', 'en')
|
|
|
|
if lang and lang[:2] == 'en':
|
|
|
|
pass # No need to display warning, we're in English
|
|
|
|
else:
|
|
|
|
print 'Locale dir does not exist at ' + os.path.join(const.PREFIXDIR, "share/locale")
|
|
|
|
print 'Running ./configure --prefix=YourPrefixDir might fix the problem'
|
2010-04-03 17:29:53 +05:30
|
|
|
LOCALEDIR = None
|
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-18 10:12:17 +05:30
|
|
|
gettext.bindtextdomain(LOCALEDOMAIN, LOCALEDIR)
|
|
|
|
gettext.textdomain(LOCALEDOMAIN)
|
2010-02-06 22:47:20 +05:30
|
|
|
try:
|
|
|
|
locale.bindtextdomain(LOCALEDOMAIN, LOCALEDIR)
|
|
|
|
except ValueError:
|
|
|
|
print 'Failed to bind text domain, gtk.Builder() has no translation'
|
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)
|
2010-02-20 18:51:26 +05:30
|
|
|
|
|
|
|
def find_intl(fname):
|
|
|
|
"""
|
|
|
|
Routine for finding if fname is in path
|
|
|
|
Returns path to fname or None
|
|
|
|
"""
|
|
|
|
os_path = os.environ['PATH']
|
|
|
|
for subpath in os_path.split(';'):
|
|
|
|
path2file = subpath + '\\' + fname
|
|
|
|
if os.path.isfile(path2file):
|
|
|
|
return path2file
|
|
|
|
return None
|
|
|
|
|
|
|
|
def test_trans(str2trans,libintl):
|
|
|
|
"""
|
|
|
|
Routine to see if translation works
|
|
|
|
Returns translated string
|
|
|
|
"""
|
|
|
|
transstr = libintl.gettext(str2trans, LOCALEDOMAIN)
|
|
|
|
return transstr
|
2010-02-06 22:47:20 +05:30
|
|
|
|
2010-02-20 18:51:26 +05:30
|
|
|
def init_windows_gettext(intl_path):
|
|
|
|
"""
|
|
|
|
Help routine for loading and setting up libintl attributes
|
|
|
|
Returns libintl
|
2010-02-06 22:47:20 +05:30
|
|
|
"""
|
2010-02-15 02:36:31 +05:30
|
|
|
import ctypes
|
2010-02-20 18:51:26 +05:30
|
|
|
libintl = ctypes.cdll.LoadLibrary(intl_path)
|
2010-02-18 20:15:35 +05:30
|
|
|
libintl.bindtextdomain(LOCALEDOMAIN,
|
2010-02-20 18:51:26 +05:30
|
|
|
LOCALEDIR.encode(sys.getfilesystemencoding()))
|
2010-02-15 02:36:31 +05:30
|
|
|
libintl.textdomain(LOCALEDOMAIN)
|
|
|
|
libintl.bind_textdomain_codeset(LOCALEDOMAIN, "UTF-8")
|
2010-02-20 18:51:26 +05:30
|
|
|
libintl.gettext.restype = ctypes.c_char_p
|
|
|
|
return libintl
|
|
|
|
|
|
|
|
def setup_windows_gettext():
|
|
|
|
"""
|
|
|
|
Windows specific function for migrating from LibGlade to GtkBuilder
|
|
|
|
Glade had a gtk.glade.bindtextdomain() function to define the directory
|
|
|
|
where to look for translations (.mo-files). It is now replaced with call
|
|
|
|
to locale.bindtextdomain() which exposes the C librarys gettext
|
|
|
|
interface on systems that provide this interface.
|
|
|
|
As MS Standard Runtime C library have not such interface call to
|
|
|
|
Python's locale.bindtextdomain() is not supported on Windows systems.
|
|
|
|
To get translation to work we must use gettext runtime library directly
|
|
|
|
using ctypes.
|
|
|
|
|
|
|
|
SEE: https://bugzilla.gnome.org/show_bug.cgi?id=574520
|
|
|
|
|
2010-02-21 18:08:02 +05:30
|
|
|
NOTE: officially GTK is built in a way that allows deployment without
|
2010-02-20 18:51:26 +05:30
|
|
|
gettext runtime library in addition to that for historic reason and
|
2010-02-21 18:08:02 +05:30
|
|
|
compability libraries are built with MS name style convention like
|
|
|
|
"intl.dll" but private builds may use posix/ld-linker tradition like
|
2010-02-20 18:51:26 +05:30
|
|
|
"libintlX-X.dll" which in recent gettext version would be libintl-8.dll
|
|
|
|
"""
|
2010-02-21 18:08:02 +05:30
|
|
|
|
2010-02-20 18:51:26 +05:30
|
|
|
str2translate = "Family Trees - Gramps"
|
|
|
|
translated = ""
|
|
|
|
# 1. See if there is a intl.dll in Windows/system
|
|
|
|
os_path = os.environ['PATH']
|
2010-02-20 22:47:32 +05:30
|
|
|
intl_path = 'c:\\WINDOWS\\system\\intl.dll'
|
2010-04-04 14:55:18 +05:30
|
|
|
if os.path.isfile(intl_path) and not LOCALEDIR is None:
|
2010-02-20 18:51:26 +05:30
|
|
|
libintl = init_windows_gettext(intl_path)
|
|
|
|
# Now check for translation.
|
|
|
|
translated = test_trans(str2translate,libintl)
|
|
|
|
if str2translate != translated:
|
|
|
|
#Translation complete
|
|
|
|
return
|
|
|
|
|
|
|
|
#2. See if there is a libintl-8.dll in the current path
|
|
|
|
intl_path = find_intl('\\libintl-8.dll')
|
2010-04-04 14:55:18 +05:30
|
|
|
if intl_path and not LOCALEDIR is None:
|
2010-02-20 18:51:26 +05:30
|
|
|
libintl = init_windows_gettext(intl_path)
|
|
|
|
# Now check for translation.
|
|
|
|
translated = test_trans(str2translate,libintl)
|
|
|
|
if str2translate != translated:
|
|
|
|
#Translation complete
|
|
|
|
return
|
|
|
|
|
|
|
|
#3. See if there is another intl.dll in current path
|
|
|
|
intl_path = find_intl('\\intl.dll')
|
2010-04-04 14:55:18 +05:30
|
|
|
if intl_path and not LOCALEDIR is None:
|
2010-02-20 18:51:26 +05:30
|
|
|
libintl = init_windows_gettext(intl_path)
|
|
|
|
# Now check for translation.
|
|
|
|
translated = test_trans(str2translate,libintl)
|
|
|
|
if str2translate != translated:
|
|
|
|
#Translation complete
|
|
|
|
return
|
|
|
|
|
|
|
|
# 4. If strings are equal, see if we have English as language
|
|
|
|
lang = ' '
|
|
|
|
try:
|
|
|
|
lang = os.environ["LANG"]
|
|
|
|
except:
|
|
|
|
# if LANG is not set
|
|
|
|
lang = locale.getlocale()[0]
|
|
|
|
if not lang:
|
|
|
|
# if lang is empty/None
|
|
|
|
lang = locale.getdefaultlocale()[0]
|
|
|
|
# See if lang begins with en_, English_ or english_
|
|
|
|
enlang = lang.split('_')[0].lower()
|
|
|
|
if enlang in ('en', 'english', 'c'):
|
|
|
|
return
|
|
|
|
|
|
|
|
# No complete/working translation found
|
2010-02-21 18:08:02 +05:30
|
|
|
print "Translation might not be complete/not working for",\
|
|
|
|
locale.getlocale()[0]
|
2010-02-20 18:51:26 +05:30
|
|
|
|
2010-02-06 22:47:20 +05:30
|
|
|
|
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-08-13 09:25:54 +05:30
|
|
|
def get_addon_translator(filename=None, domain="addon", languages=None):
|
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
|
2010-08-13 09:25:54 +05:30
|
|
|
languages - a list of languages to force
|
2010-01-17 19:14:53 +05:30
|
|
|
returns - a gettext.translation object
|
2009-11-05 04:50:35 +05:30
|
|
|
|
2010-08-13 09:25:54 +05:30
|
|
|
Example:
|
|
|
|
_ = get_addon_translator(languages=["fr_BE.utf8"]).gettext
|
|
|
|
|
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-18 10:12:17 +05:30
|
|
|
gramps_translator = gettext.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-10-09 18:03:47 +05:30
|
|
|
# Check if path is of type str. Do import and conversion if so.
|
|
|
|
# The import cannot be done at the top as that will conflict with the translation system.
|
|
|
|
if type(path) == str:
|
|
|
|
from Utils import get_unicode_path_from_env_var
|
|
|
|
path = get_unicode_path_from_env_var(path)
|
2010-08-13 09:25:54 +05:30
|
|
|
if languages:
|
|
|
|
addon_translator = gettext.translation(domain, os.path.join(path,"locale"),
|
|
|
|
languages=languages,
|
|
|
|
fallback=True)
|
|
|
|
else:
|
|
|
|
addon_translator = gettext.translation(domain, os.path.join(path,"locale"),
|
|
|
|
fallback=True)
|
2009-11-05 04:50:35 +05:30
|
|
|
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"]
|
|
|
|
|
2010-04-03 17:29:53 +05:30
|
|
|
if LOCALEDIR is None:
|
|
|
|
return languages
|
|
|
|
|
2009-10-22 09:52:24 +05:30
|
|
|
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
|