[Mpuls-commits] r4085 - base/trunk/mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Nov 2 16:09:10 CET 2010


Author: torsten
Date: 2010-11-02 16:09:10 +0100 (Tue, 02 Nov 2010)
New Revision: 4085

Modified:
   base/trunk/mpulsweb/lib/translation.py
Log:
* mpulsweb/lib/translation.py: Added support for multiple domains in
different locatations. This should make internationalization much more
flexible. Some optimization is still needed. E.g do not build the
translator object every time This was already implemented in this way.
in the former version of lib/translation.py.


Modified: base/trunk/mpulsweb/lib/translation.py
===================================================================
--- base/trunk/mpulsweb/lib/translation.py	2010-11-01 09:27:06 UTC (rev 4084)
+++ base/trunk/mpulsweb/lib/translation.py	2010-11-02 15:09:10 UTC (rev 4085)
@@ -19,14 +19,7 @@
         _('This should be in lots of languages')
     
     """
-    # TODO: Do not build a new newbranslator every time. Save it in app_globals
-    # e.g
-    base_translated = _get_translator(get_lang())
-    translated = pylons.translator.ugettext(value)
-    if translated == value:
-        # Seems not to be translated look in baseapplication
-        translated = base_translated.ugettext(value)
-    return translated
+    return _ugettext(get_lang(), value)
 
 _ = ugettext
 
@@ -45,53 +38,84 @@
                   n) % {'num': n}
 
     """
-    # TODO: Do not build a new newbranslator every time. Save it in app_globals
-    # e.g
-    base_translated = _get_translator(get_lang())
-    translated = pylons.translator.ungettext(singular, plural, n)
-    if translated == singular:
-        # Seems not to be translated look in baseapplication
-        translated = base_translated.ungettext(singular, plural, n)
-    return translated
+    return _ungettext(get_lang(), singular, plural, n) 
 
-def _get_translator(lang, **kwargs):
+def _ugettext(lang, value):
+    dirs = []
+    domains = []
+    if pylons.config.get('mpuls.app.path.i18n'):
+        domains.append(pylons.config.get('mpuls.app.instance'))
+        dirs.append(pylons.config.get('mpuls.app.path.i18n'))
+
+    # Adding defaults
+    conf = pylons.config.current_conf()
+    dirs.append(os.path.join(conf['pylons.paths']['root'], 'i18n'))
+    domains.append('mpulsweb')
+
+    for do in domains:
+        for di in dirs:
+            translator = _get_translator(lang, do, di)
+            if translator is not None: 
+                translated = translator.ugettext(value)
+                if translated != value:
+                    # Seems to be translated
+                    return translated
+    return value
+
+def _ungettext(lang, singular, plural, n):
+    dirs = []
+    domains = []
+    if pylons.config.get('mpuls.app.path.i18n'):
+        domains.append(pylons.config.get('mpuls.app.instance'))
+        dirs.append(pylons.config.get('mpuls.app.path.i18n'))
+
+    # Adding defaults
+    conf = pylons.config.current_conf()
+    dirs.append(os.path.join(conf['pylons.paths']['root'], 'i18n'))
+    domains.append('mpulsweb')
+
+    for do in domains:
+        for di in dirs:
+            translator = _get_translator(lang, do, di)
+            if translator is not None: 
+                translated = translator.ungettext(singular, plural, n)
+                if translated != singular:
+                    # Seems to be translated
+                    return translated
+    return value
+
+def _get_translator(lang, domain='mpulsweb', dir=None, **kwargs):
     """Utility method to get a valid translator object from a language
     name"""
+    #log.debug('getting translator for %s' % domain)
     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')
-    localedir = os.path.join(rootdir, 'i18n')
-    if 'i18n_dir' in kwargs:
-        dir = kwargs.pop('i18n_dir')
-        if dir is not None:
-            localedir = dir
+
     if not isinstance(lang, list):
         lang = [lang]
+
+    if not dir:
+        conf = pylons.config.current_conf()
+        dir = os.path.join(conf['pylons.paths']['root'], 'i18n')
+
     try:
-        translator = translation(conf['pylons.package'], localedir,
-                                 languages=lang, **kwargs)
+        # TODO: Do not build a new newbranslator every time. Save it in app_globals
+        # e.g
+        translator = translation(domain,dir,languages=lang, **kwargs)
+        translator.pylons_lang = lang
+        return translator
     except IOError, ioe:
-        raise LanguageError('IOError: %s' % ioe)
-    translator.pylons_lang = lang
-    return translator
+        #log.debug('Can not load %s in %s' % (domain, dir))
+        return None
 
-
-def set_lang(lang, **kwargs):
+def set_lang(lang):
     """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)
+    translator = _get_translator(lang)
     environ = pylons.request.environ
     environ['pylons.pylons'].translator = translator
     if 'paste.registry' in environ:



More information about the Mpuls-commits mailing list