Wagner Bruna  committed b578229

i18n: support for message contexts

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

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 ), the messages are
explicitely concatenated with the '\004' context separator
before lookup.

  • Participants
  • Parent commits 8db3637
  • Branches hgtk

Comments (0)

Files changed (1)

File tortoisehg/util/

     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.
         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)}