From 9717e5954bdb99a2b9260c0ec4e14a85d8dbb781 Mon Sep 17 00:00:00 2001 From: Daniel Narvaez Date: Tue, 10 Sep 2013 22:42:48 +0200 Subject: [PATCH] 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). --- bin/sugar-activity | 12 +++++---- configure.ac | 1 + src/sugar3/Makefile.am | 2 ++ src/sugar3/__init__.py | 30 ---------------------- src/sugar3/activity/i18n.py | 50 ------------------------------------- src/sugar3/config.py.in | 20 +++++++++++++++ 6 files changed, 30 insertions(+), 85 deletions(-) create mode 100644 src/sugar3/config.py.in diff --git a/bin/sugar-activity b/bin/sugar-activity index b5c796bf..a8d4246f 100644 --- a/bin/sugar-activity +++ b/bin/sugar-activity @@ -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,11 +108,12 @@ 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) - gettext.textdomain(bundle.get_bundle_id()) + 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) module_name = splitted_module[0] diff --git a/configure.ac b/configure.ac index e5e3d79e..64650aed 100644 --- a/configure.ac +++ b/configure.ac @@ -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 ]) diff --git a/src/sugar3/Makefile.am b/src/sugar3/Makefile.am index e5e34367..9e13e8ce 100644 --- a/src/sugar3/Makefile.am +++ b/src/sugar3/Makefile.am @@ -17,6 +17,8 @@ sugar_PYTHON = \ profile.py \ util.py +nodist_sugar_PYTHON = config.py + XDG_MIME_SOURCES = \ xdgmime.c \ xdgmime.h \ diff --git a/src/sugar3/__init__.py b/src/sugar3/__init__.py index d24d665b..e69de29b 100644 --- a/src/sugar3/__init__.py +++ b/src/sugar3/__init__.py @@ -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) diff --git a/src/sugar3/activity/i18n.py b/src/sugar3/activity/i18n.py index d7c515b0..8a7a2b35 100644 --- a/src/sugar3/activity/i18n.py +++ b/src/sugar3/activity/i18n.py @@ -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 diff --git a/src/sugar3/config.py.in b/src/sugar3/config.py.in new file mode 100644 index 00000000..848c1ee6 --- /dev/null +++ b/src/sugar3/config.py.in @@ -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'