[Checkins] SVN: zope.i18n/trunk/ Feature: Added new top-level negotiate function, which can be used to negotiate the language when th

Hanno Schlichting plone at hannosch.info
Sun Jul 6 13:28:54 EDT 2008


Log message for revision 88073:
  Feature: Added new top-level negotiate function, which can be used to negotiate the language when th
  e available languages are set globally via `zope_i18n_allowed_languages`. Refactored env variables into a config.py file.
  

Changed:
  U   zope.i18n/trunk/CHANGES.txt
  U   zope.i18n/trunk/src/zope/i18n/__init__.py
  A   zope.i18n/trunk/src/zope/i18n/config.py
  U   zope.i18n/trunk/src/zope/i18n/negotiator.py
  U   zope.i18n/trunk/src/zope/i18n/tests/test_zcml.py
  U   zope.i18n/trunk/src/zope/i18n/translationdomain.py
  U   zope.i18n/trunk/src/zope/i18n/zcml.py

-=-
Modified: zope.i18n/trunk/CHANGES.txt
===================================================================
--- zope.i18n/trunk/CHANGES.txt	2008-07-06 16:41:50 UTC (rev 88072)
+++ zope.i18n/trunk/CHANGES.txt	2008-07-06 17:28:53 UTC (rev 88073)
@@ -5,6 +5,10 @@
 3.5 (unreleased)
 ------------------
 
+- Feature: Added new top-level negotiate function, which can be used to
+  negotiate the language when the available languages are set globally via
+  `zope_i18n_allowed_languages`.
+
 - Feature: Added support for restricting the available languages. We support
   an environment variable called `zope_i18n_allowed_languages` now, which is
   a list of comma or space separated language codes. If the environment

Modified: zope.i18n/trunk/src/zope/i18n/__init__.py
===================================================================
--- zope.i18n/trunk/src/zope/i18n/__init__.py	2008-07-06 16:41:50 UTC (rev 88072)
+++ zope.i18n/trunk/src/zope/i18n/__init__.py	2008-07-06 17:28:53 UTC (rev 88073)
@@ -17,12 +17,14 @@
 """
 import re
 
+from zope.component import queryUtility
 from zope.i18nmessageid import MessageFactory, Message
+
+from zope.i18n.config import ALLOWED_LANGUAGES
+from zope.i18n.interfaces import INegotiator
 from zope.i18n.interfaces import ITranslationDomain
 from zope.i18n.interfaces import IFallbackTranslationDomainFactory
-from zope.component import queryUtility
 
-
 # Set up regular expressions for finding interpolation variables in text.
 # NAME_RE must exactly match the expression of the same name in the
 # zope.tal.taldefs module:
@@ -31,6 +33,19 @@
 _interp_regex = re.compile(r'(?<!\$)(\$(?:(%(n)s)|{(%(n)s)}))'
     % ({'n': NAME_RE}))
 
+
+def negotiate(context):
+    """Negotiate language.
+
+    This only works if the languages are set globally, otherwise each message
+    catalog needs to do the language negotiation.
+    """
+    if ALLOWED_LANGUAGES is not None:
+        negotiator = queryUtility(INegotiator)
+        if negotiator is not None:
+            return negotiator.getLanguage(ALLOWED_LANGUAGES, context)
+    return None
+
 def translate(msgid, domain=None, mapping=None, context=None,
                target_language=None, default=None):
     """Translate text.
@@ -107,6 +122,9 @@
     if util is None:
         return interpolate(default, mapping)
 
+    if target_language is None and context is not None:
+        target_language = negotiate(context)
+
     return util.translate(msgid, mapping, context, target_language, default)
 
 def interpolate(text, mapping=None):

Added: zope.i18n/trunk/src/zope/i18n/config.py
===================================================================
--- zope.i18n/trunk/src/zope/i18n/config.py	                        (rev 0)
+++ zope.i18n/trunk/src/zope/i18n/config.py	2008-07-06 17:28:53 UTC (rev 88073)
@@ -0,0 +1,11 @@
+import os
+
+COMPILE_MO_FILES_KEY = 'zope_i18n_compile_mo_files'
+COMPILE_MO_FILES = os.environ.get(COMPILE_MO_FILES_KEY, False)
+
+ALLOWED_LANGUAGES_KEY = 'zope_i18n_allowed_languages'
+ALLOWED_LANGUAGES = os.environ.get(ALLOWED_LANGUAGES_KEY, None)
+
+if ALLOWED_LANGUAGES is not None:
+    ALLOWED_LANGUAGES = ALLOWED_LANGUAGES.strip().replace(',', ' ')
+    ALLOWED_LANGUAGES = frozenset(ALLOWED_LANGUAGES.split())


Property changes on: zope.i18n/trunk/src/zope/i18n/config.py
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: zope.i18n/trunk/src/zope/i18n/negotiator.py
===================================================================
--- zope.i18n/trunk/src/zope/i18n/negotiator.py	2008-07-06 16:41:50 UTC (rev 88072)
+++ zope.i18n/trunk/src/zope/i18n/negotiator.py	2008-07-06 17:28:53 UTC (rev 88073)
@@ -15,9 +15,10 @@
 
 $Id$
 """
+from zope.interface import implements
+
 from zope.i18n.interfaces import INegotiator
 from zope.i18n.interfaces import IUserPreferredLanguages
-from zope.interface import implements
 
 def normalize_lang(lang):
     lang = lang.strip().lower()

Modified: zope.i18n/trunk/src/zope/i18n/tests/test_zcml.py
===================================================================
--- zope.i18n/trunk/src/zope/i18n/tests/test_zcml.py	2008-07-06 16:41:50 UTC (rev 88072)
+++ zope.i18n/trunk/src/zope/i18n/tests/test_zcml.py	2008-07-06 17:28:53 UTC (rev 88073)
@@ -27,6 +27,7 @@
 import zope.i18n.tests
 from zope.i18n.interfaces import ITranslationDomain
 from zope.i18n.compile import HAS_PYTHON_GETTEXT
+from zope.i18n import config
 from zope.i18n import zcml
 
 template = """\
@@ -41,12 +42,12 @@
     def setUp(self):
         super(DirectivesTest, self).setUp()
         self.context = xmlconfig.file('meta.zcml', zope.i18n)
-        self.allowed = zcml.ALLOWED_LANGUAGES
-        zcml.ALLOWED_LANGUAGES = None
+        self.allowed = config.ALLOWED_LANGUAGES
+        config.ALLOWED_LANGUAGES = None
 
     def tearDown(self):
         super(DirectivesTest, self).tearDown()
-        zcml.ALLOWED_LANGUAGES = self.allowed
+        config.ALLOWED_LANGUAGES = self.allowed
 
     def testRegisterTranslations(self):
         self.assert_(queryUtility(ITranslationDomain) is None)
@@ -64,7 +65,7 @@
 
     def testAllowedTranslations(self):
         self.assert_(queryUtility(ITranslationDomain) is None)
-        zcml.ALLOWED_LANGUAGES = ('de', 'fr')
+        config.ALLOWED_LANGUAGES = ('de', 'fr')
         xmlconfig.string(
             template % '''
             <configure package="zope.i18n.tests">
@@ -111,7 +112,7 @@
 
     if HAS_PYTHON_GETTEXT:
         def testRegisterAndCompileTranslations(self):
-            zcml.COMPILE_MO_FILES = True
+            config.COMPILE_MO_FILES = True
             self.assert_(queryUtility(ITranslationDomain) is None)
 
             # Copy an old and outdated file over, so we can test if the

Modified: zope.i18n/trunk/src/zope/i18n/translationdomain.py
===================================================================
--- zope.i18n/trunk/src/zope/i18n/translationdomain.py	2008-07-06 16:41:50 UTC (rev 88072)
+++ zope.i18n/trunk/src/zope/i18n/translationdomain.py	2008-07-06 17:28:53 UTC (rev 88073)
@@ -81,7 +81,7 @@
 
     def _recursive_translate(self, msgid, mapping, target_language, default,
                              seen=None):
-        """Recurivly translate msg."""
+        """Recursively translate msg."""
         # MessageID attributes override arguments
         if isinstance(msgid, Message):
             if msgid.domain != self.domain:

Modified: zope.i18n/trunk/src/zope/i18n/zcml.py
===================================================================
--- zope.i18n/trunk/src/zope/i18n/zcml.py	2008-07-06 16:41:50 UTC (rev 88072)
+++ zope.i18n/trunk/src/zope/i18n/zcml.py	2008-07-06 17:28:53 UTC (rev 88073)
@@ -20,27 +20,19 @@
 
 import os
 
-from zope.interface import Interface
+from zope.component import queryUtility
+from zope.component.zcml import utility
 from zope.configuration.fields import Path
+from zope.interface import Interface
+
+from zope.i18n import config
 from zope.i18n.compile import compile_mo_file
 from zope.i18n.gettextmessagecatalog import GettextMessageCatalog
 from zope.i18n.testmessagecatalog import TestMessageCatalog
 from zope.i18n.translationdomain import TranslationDomain
 from zope.i18n.interfaces import ITranslationDomain
-from zope.component import queryUtility
-from zope.component.zcml import utility
 
-COMPILE_MO_FILES_KEY = 'zope_i18n_compile_mo_files'
-COMPILE_MO_FILES = os.environ.get(COMPILE_MO_FILES_KEY, False)
 
-ALLOWED_LANGUAGES_KEY = 'zope_i18n_allowed_languages'
-ALLOWED_LANGUAGES = os.environ.get(ALLOWED_LANGUAGES_KEY, None)
-
-if ALLOWED_LANGUAGES is not None:
-    ALLOWED_LANGUAGES = ALLOWED_LANGUAGES.strip().replace(',', ' ')
-    ALLOWED_LANGUAGES = frozenset(ALLOWED_LANGUAGES.split())
-
-
 class IRegisterTranslationsDirective(Interface):
     """Register translations with the global site manager."""
 
@@ -51,9 +43,9 @@
         )
 
 def allow_language(lang):
-    if ALLOWED_LANGUAGES is None:
+    if config.ALLOWED_LANGUAGES is None:
         return True
-    return lang in ALLOWED_LANGUAGES
+    return lang in config.ALLOWED_LANGUAGES
 
 def registerTranslations(_context, directory):
     path = os.path.normpath(directory)
@@ -68,7 +60,7 @@
         lc_messages_path = os.path.join(path, language, 'LC_MESSAGES')
         if os.path.isdir(lc_messages_path):
             # Preprocess files and update or compile the mo files
-            if COMPILE_MO_FILES:
+            if config.COMPILE_MO_FILES:
                 for domain_file in os.listdir(lc_messages_path):
                     if domain_file.endswith('.po'):
                         domain = domain_file[:-3]



More information about the Checkins mailing list