[Checkins] SVN: zope.i18n/trunk/ 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 variable is set, the ZCML registration will only process those folders which are in the allowed languages list.

Hanno Schlichting plone at hannosch.info
Sun Jul 6 12:41:51 EDT 2008


Log message for revision 88072:
  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 variable is set, the ZCML registration will only process those folders which are in the allowed languages list.
  

Changed:
  U   zope.i18n/trunk/CHANGES.txt
  A   zope.i18n/trunk/src/zope/i18n/tests/locale/de/
  A   zope.i18n/trunk/src/zope/i18n/tests/locale/de/LC_MESSAGES/
  A   zope.i18n/trunk/src/zope/i18n/tests/locale/de/LC_MESSAGES/zope-i18n.mo
  A   zope.i18n/trunk/src/zope/i18n/tests/locale/de/LC_MESSAGES/zope-i18n.po
  U   zope.i18n/trunk/src/zope/i18n/tests/test_zcml.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:31:31 UTC (rev 88071)
+++ zope.i18n/trunk/CHANGES.txt	2008-07-06 16:41:50 UTC (rev 88072)
@@ -5,6 +5,12 @@
 3.5 (unreleased)
 ------------------
 
+- 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
+  variable is set, the ZCML registration will only process those folders
+  which are in the allowed languages list.
+
 - Feature: Added optional automatic compilation of mo files from po files.
   You need to depend on the `zope.i18n [compile]` extra and set an environment
   variable called `zope_i18n_compile_mo_files` to any True value to enable

Added: zope.i18n/trunk/src/zope/i18n/tests/locale/de/LC_MESSAGES/zope-i18n.mo
===================================================================
(Binary files differ)


Property changes on: zope.i18n/trunk/src/zope/i18n/tests/locale/de/LC_MESSAGES/zope-i18n.mo
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: zope.i18n/trunk/src/zope/i18n/tests/locale/de/LC_MESSAGES/zope-i18n.po
===================================================================
--- zope.i18n/trunk/src/zope/i18n/tests/locale/de/LC_MESSAGES/zope-i18n.po	                        (rev 0)
+++ zope.i18n/trunk/src/zope/i18n/tests/locale/de/LC_MESSAGES/zope-i18n.po	2008-07-06 16:41:50 UTC (rev 88072)
@@ -0,0 +1,11 @@
+msgid ""
+msgstr ""
+"Project-Id-Version: Zope 3\n"
+"PO-Revision-Date: 2002/06/13\n"
+"Last-Translator: Zope 3 contributors\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=ISO-8859-1\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+msgid "Message"
+msgstr "Message translated"


Property changes on: zope.i18n/trunk/src/zope/i18n/tests/locale/de/LC_MESSAGES/zope-i18n.po
___________________________________________________________________
Name: svn:eol-style
   + native

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:31:31 UTC (rev 88071)
+++ zope.i18n/trunk/src/zope/i18n/tests/test_zcml.py	2008-07-06 16:41:50 UTC (rev 88072)
@@ -41,7 +41,13 @@
     def setUp(self):
         super(DirectivesTest, self).setUp()
         self.context = xmlconfig.file('meta.zcml', zope.i18n)
+        self.allowed = zcml.ALLOWED_LANGUAGES
+        zcml.ALLOWED_LANGUAGES = None
 
+    def tearDown(self):
+        super(DirectivesTest, self).tearDown()
+        zcml.ALLOWED_LANGUAGES = self.allowed
+
     def testRegisterTranslations(self):
         self.assert_(queryUtility(ITranslationDomain) is None)
         xmlconfig.string(
@@ -53,8 +59,23 @@
         path = os.path.join(os.path.dirname(zope.i18n.tests.__file__),
                             'locale', 'en', 'LC_MESSAGES', 'zope-i18n.mo')
         util = getUtility(ITranslationDomain, 'zope-i18n')
+        self.assertEquals(util._catalogs.get('test'), ['test'])
+        self.assertEquals(util._catalogs.get('en'), [unicode(path)])
+
+    def testAllowedTranslations(self):
+        self.assert_(queryUtility(ITranslationDomain) is None)
+        zcml.ALLOWED_LANGUAGES = ('de', 'fr')
+        xmlconfig.string(
+            template % '''
+            <configure package="zope.i18n.tests">
+            <i18n:registerTranslations directory="locale" />
+            </configure>
+            ''', self.context)
+        path = os.path.join(os.path.dirname(zope.i18n.tests.__file__),
+                            'locale', 'de', 'LC_MESSAGES', 'zope-i18n.mo')
+        util = getUtility(ITranslationDomain, 'zope-i18n')
         self.assertEquals(util._catalogs,
-                          {'test': ['test'], 'en': [unicode(path)]})
+                          {'test': ['test'], 'de': [unicode(path)]})
 
     def testRegisterDistributedTranslations(self):
         self.assert_(queryUtility(ITranslationDomain) is None)
@@ -75,9 +96,9 @@
         path2 = os.path.join(os.path.dirname(zope.i18n.tests.__file__),
                              'locale2', 'en', 'LC_MESSAGES', 'zope-i18n.mo')
         util = getUtility(ITranslationDomain, 'zope-i18n')
-        self.assertEquals(util._catalogs,
-                          {'test': ['test', 'test'],
-                                   'en': [unicode(path1), unicode(path2)]})
+        self.assertEquals(util._catalogs.get('test'), ['test', 'test'])
+        self.assertEquals(util._catalogs.get('en'),
+                          [unicode(path1), unicode(path2)])
 
         msg = util.translate(u'Additional message', target_language='en')
         self.assertEquals(msg, u'Additional message translated')

Modified: zope.i18n/trunk/src/zope/i18n/zcml.py
===================================================================
--- zope.i18n/trunk/src/zope/i18n/zcml.py	2008-07-06 16:31:31 UTC (rev 88071)
+++ zope.i18n/trunk/src/zope/i18n/zcml.py	2008-07-06 16:41:50 UTC (rev 88072)
@@ -33,7 +33,14 @@
 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."""
 
@@ -43,6 +50,11 @@
         required=True
         )
 
+def allow_language(lang):
+    if ALLOWED_LANGUAGES is None:
+        return True
+    return lang in ALLOWED_LANGUAGES
+
 def registerTranslations(_context, directory):
     path = os.path.normpath(directory)
     domains = {}
@@ -51,6 +63,8 @@
     # which is exactly the opposite as we need it. So create a dictionary that
     # reverses the nesting.
     for language in os.listdir(path):
+        if not allow_language(language):
+            continue
         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



More information about the Checkins mailing list