Fix translations in non standard prefix

This drops a lot of code at the same time. The code in
__init__ was:

* Using a SUGAR_PREFIX environment variable that is not
defined anywhere
* Hardcoding prefix to /usr
* Setting up the sugar-base domain which doesn't exist anymore

The i18n module code was overcomplex because it was thought
for language packs which we don't support anymore, it was
not handling correctly locales with a country prefix, it
was hard coding the python prefix and it was not sorting
the directories as it intended too anyway.

The logic is very simple now. Use the locale directory in
the same prefix sugar-toolkit-gtk3 was installed, unless
SUGAR_LOCALEDIR is defined (for self contained bundles).
This commit is contained in:
Daniel Narvaez 2013-09-10 22:42:48 +02:00
parent 4259cc4fd1
commit 9717e5954b
6 changed files with 30 additions and 85 deletions

View File

@ -34,6 +34,7 @@ DBusGMainLoop(set_as_default=True)
from sugar3.activity import activityhandle
from sugar3.activity import i18n
from sugar3 import config
import sugar3
from sugar3.bundle.activitybundle import ActivityBundle
from sugar3 import logger
@ -107,10 +108,11 @@ def main():
os.environ['SUGAR_BUNDLE_VERSION'] = str(bundle.get_activity_version())
# must be done early, some activities set translations globally, SL #3654
locale_path = i18n.get_locale_path(bundle.get_bundle_id())
if locale_path:
gettext.bindtextdomain(bundle.get_bundle_id(), locale_path)
gettext.bindtextdomain('sugar-toolkit', sugar3.locale_path)
activity_locale_path = os.environ.get("SUGAR_LOCALEDIR",
config.locale_path)
gettext.bindtextdomain(bundle.get_bundle_id(), activity_locale_path)
gettext.bindtextdomain('sugar-toolkit', config.locale_path)
gettext.textdomain(bundle.get_bundle_id())
splitted_module = args[0].rsplit('.', 1)

View File

@ -49,5 +49,6 @@ src/sugar3/presence/Makefile
src/sugar3/datastore/Makefile
src/sugar3/dispatch/Makefile
src/sugar3/test/Makefile
src/sugar3/config.py
po/Makefile.in
])

View File

@ -17,6 +17,8 @@ sugar_PYTHON = \
profile.py \
util.py
nodist_sugar_PYTHON = config.py
XDG_MIME_SOURCES = \
xdgmime.c \
xdgmime.h \

View File

@ -1,30 +0,0 @@
# Copyright (C) 2006-2007, Red Hat, Inc.
# Copyright (C) 2007-2008, One Laptop Per Child
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import os
import gettext
if 'SUGAR_PREFIX' in os.environ:
prefix = os.environ['SUGAR_PREFIX']
else:
prefix = '/usr'
locale_path = os.path.join(prefix, 'share', 'locale')
gettext.bindtextdomain('sugar-base', locale_path)

View File

@ -108,53 +108,3 @@ def pgettext(context, message):
if '\x04' in translation:
return message
return translation
def get_locale_path(bundle_id):
""" Returns the locale path, which is the directory where the preferred
MO file is located.
The preferred MO file is the one with the latest translation.
@type bundle_id: string
@param bundle_id: The bundle id of the activity in question
@rtype: string
@return: the preferred locale path or None in the
case of an error
"""
# Note: We pre-assign weights to the directories so that if no translations
# exist, the appropriate fallbacks (eg: bn for bn_BD) can be loaded
# The directory with the highest weight is returned, and if a MO file is
# found, the weight of the directory is set to the MO's modification time
# (as described in the MO header, and _not_ the filesystem mtime)
candidate_dirs = {}
if 'SUGAR_LOCALEDIR' in os.environ:
candidate_dirs[os.environ['SUGAR_LOCALEDIR']] = 2
candidate_dirs[os.path.join(sys.prefix, 'share', 'locale')] = 0
default_locale = locale.getdefaultlocale()[0]
if not default_locale:
return None
for candidate_dir in candidate_dirs.keys():
if os.path.exists(candidate_dir):
full_path = os.path.join(candidate_dir,
default_locale, 'LC_MESSAGES',
bundle_id + '.mo')
if os.path.exists(full_path):
try:
candidate_dirs[candidate_dir] = \
_extract_modification_time(full_path)
except (IOError, ValueError):
# The mo file is damaged or has not been initialized
# Set lowest priority
candidate_dirs[candidate_dir] = -1
available_paths = sorted(candidate_dirs.iteritems(), key=lambda (k, v):
(v, k), reverse=True)
preferred_path = available_paths[0][0]
return preferred_path

20
src/sugar3/config.py.in Normal file
View File

@ -0,0 +1,20 @@
# Copyright (C) 2006-2007 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# flake8: noqa
locale_path = '@prefix@/share/locale'