Wagner Bruna avatar Wagner Bruna committed b578229

i18n: support for message contexts

Adds support for contextualizing messages to avoid ambiguities,
as described in

http://www.gnu.org/software/gettext/manual/gettext.html#Contexts

The "_" functions are enhanced to get an additional context parameter;
for instance, "_('All', 'changesets')" and "_('All', 'files')"
would appear as two separate strings for translation, together
with the context marker. If no contextualized message is found,
we fall back to the old behavior, to reuse existing translations.

Since Python gettext currently doesn't support pgettext
(see http://bugs.python.org/issue2504 ), the messages are
explicitely concatenated with the '\004' context separator
before lookup.

Comments (0)

Files changed (1)

tortoisehg/util/i18n.py

     langs.append('en')  # means null translation
     return sorted(langs)
 
-def _(message):
+def _(message, context=''):
+    if context:
+        tmsg = t.gettext(context + '\004' + message)
+        if '\004' not in tmsg:
+            return tmsg
     return t.gettext(message)
 
-def agettext(message):
+def agettext(message, context=''):
     """Translate message and convert to local encoding
     such as 'ascii' before being returned.
 
     """
     try:
         from tortoisehg.util import hglib
-        u = _(message)
+        u = _(message, context)
         return hglib.fromutf(u)
     except (LookupError, UnicodeEncodeError):
         return message
 
 class keepgettext(object):
-    def _(self, message):
-        return {'id': message, 'str': _(message)}
+    def _(self, message, context=''):
+        return {'id': message, 'str': _(message, context)}
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.