Andriy Kornatskyy avatar Andriy Kornatskyy committed ed6e261

Added ability to view translations domains.

Comments (0)

Files changed (1)

src/wheezy/core/i18n.py

         self.fallbacks = {}
         self.translations = defaultdict(
             lambda: defaultdict(lambda: null_translations))
+        self.domains = defaultdict(
+            lambda: defaultdict(lambda: null_translations))
         if directories:
             for localedir in directories:
                 self.load(localedir)
             >>> tm.load(localedir)
             >>> sorted(tm.translations.keys())
             ['de', 'en']
+
+            Assess by language:
+
             >>> lang = tm['en']
             >>> m = lang['messages']
             >>> m.gettext('hello')
             >>> m.gettext('hello')
             'Hallo'
 
+            Assess by translation domain:
+
+            >>> messages = tm.domains['messages']
+            >>> m = messages['en']
+            >>> m.gettext('hello')
+            'Hello'
+
             Fallback to English:
 
             >>> m.gettext('world')
             if lang.startswith('.'):  # pragma: nocover
                 continue
             domaindir = os.path.join(localedir, lang, 'LC_MESSAGES')
-            if os.path.isdir(domaindir):
-                for domain in os.listdir(domaindir):
-                    if not domain.endswith('.mo'):
-                        continue
-                    domain = domain[:-3]
-                    if lang not in self.fallbacks:
-                        self.add_fallback((lang,))
-                    self.translations[lang][domain] = gettext.translation(
-                        domain,
-                        localedir,
-                        languages=self.fallbacks[lang]
-                    )
+            if not os.path.isdir(domaindir):  # pragma: nocover
+                continue
+            for domain in os.listdir(domaindir):
+                if not domain.endswith('.mo'):
+                    continue
+                domain = domain[:-3]
+                if lang not in self.fallbacks:
+                    self.add_fallback((lang,))
+                translation = gettext.translation(
+                    domain,
+                    localedir,
+                    languages=self.fallbacks[lang]
+                )
+                self.translations[lang][domain] = translation
+                self.domains[domain][lang] = translation
 
     def __getitem__(self, lang):
-        """ Returns ``attrdict`` with available translation domains.
+        """ Returns language ``defaultdict`` with translation domains.
         """
         return self.translations[lang]
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.