Bertrand Croq avatar Bertrand Croq committed a179d82

fix i18n:Locale context manager methods

Comments (0)

Files changed (2)

             default_timezone = pytz.timezone(default_timezone)
         self.default_timezone = default_timezone
 
-        self._previous_locale = None
+        self._previous_locales = []
 
         # By default, load the translation files for the 'nagare' domain
         # from the nagare directories
         if not self.translation_directories:
             self.translation_directories.update(previous_locale.translation_directories)
 
-        self._previous_locale = previous_locale
+        self._previous_locales.append(previous_locale)
         set_locale(self)
 
     def __exit__(self, *args, **kw):
         """Pop this locale from the stack
         """
-        set_locale(self._previous_locale)
+        set_locale(self._previous_locales.pop())
 
 # -----------------------------------------------------------------------------
 

nagare/test/test_i18n.py

     d = i18n.parse_date('4/1/2004')
     assert isinstance(d, datetime.date)
     assert (d.year == 2004) and (d.month == 4) and (d.day == 1)
+
+
+def test_context_manager():
+    locale1 = i18n.Locale('fr', 'FR', domain='domain1')
+    locale2 = i18n.Locale('fr', 'FR', domain='domain2')
+    i18n.set_locale(locale1)
+    assert i18n.get_locale().domain == 'domain1', i18n.get_locale().domain
+    with locale2:
+        assert i18n.get_locale().domain == 'domain2', i18n.get_locale().domain
+        with locale2:
+            assert i18n.get_locale().domain == 'domain2', i18n.get_locale().domain
+
+    assert i18n.get_locale().domain == 'domain1', i18n.get_locale().domain
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.