[Mpuls-commits] r2932 - base/trunk/mpulsweb/lib
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Wed Jun 9 12:02:14 CEST 2010
Author: torsten
Date: 2010-06-09 12:02:13 +0200 (Wed, 09 Jun 2010)
New Revision: 2932
Added:
base/trunk/mpulsweb/lib/translation.py
Log:
New. Copied from pylons.i18n.translations to allow configurable location of language files
Added: base/trunk/mpulsweb/lib/translation.py
===================================================================
--- base/trunk/mpulsweb/lib/translation.py 2010-06-09 09:03:20 UTC (rev 2931)
+++ base/trunk/mpulsweb/lib/translation.py 2010-06-09 10:02:13 UTC (rev 2932)
@@ -0,0 +1,49 @@
+import os
+from gettext import NullTranslations, translation
+
+import pylons
+
+# Copied from pylons.i18n.translation. Adapted code to allow otional locations
+# of language files
+
+def _get_translator(lang, **kwargs):
+ """Utility method to get a valid translator object from a language
+ name"""
+ if not lang:
+ return NullTranslations()
+ if 'pylons_config' in kwargs:
+ conf = kwargs.pop('pylons_config')
+ else:
+ conf = pylons.config.current_conf()
+ # XXX: root_path is deprecated
+ try:
+ rootdir = conf['pylons.paths']['root']
+ except KeyError:
+ rootdir = conf['pylons.paths'].get('root_path')
+ if conf['pylons.paths'].has_key('i18n'):
+ localedir = conf['pylons.paths'].get('i18n')
+ else:
+ localedir = os.path.join(rootdir, 'i18n')
+ if not isinstance(lang, list):
+ lang = [lang]
+ try:
+ translator = translation(conf['pylons.package'], localedir,
+ languages=lang, **kwargs)
+ except IOError, ioe:
+ raise LanguageError('IOError: %s' % ioe)
+ translator.pylons_lang = lang
+ return translator
+
+
+def set_lang(lang, **kwargs):
+ """Set the current language used for translations.
+
+ ``lang`` should be a string or a list of strings. If a list of
+ strings, the first language is set as the main and the subsequent
+ languages are added as fallbacks.
+ """
+ translator = _get_translator(lang, **kwargs)
+ environ = pylons.request.environ
+ environ['pylons.pylons'].translator = translator
+ if 'paste.registry' in environ:
+ environ['paste.registry'].replace(pylons.translator, translator)
More information about the Mpuls-commits
mailing list