[PATCH 1/3] Add support for context-based translations

David Shea dshea at redhat.com
Fri Oct 18 15:50:52 UTC 2013


This change adds three new functions to pyanaconda.i18n:

  C_ takes a context and a message and returns a translated message.
  This is analagous to _.

  CN_ takes a context and a message, marks them as translatable, and
  returns the original message. The message must be translated elsewhere
  using C_ and the context string that was passsed to CN_. This is
  analagous to N_.

  CP_ takes a context, a message, the plural form of the message, and a
  number and returns a translated message according to the pluralization
  rules of the target language. This is analagous to P_.

The reason for this is that strings in a user interface often involve
abbreviations that are sensitive to the other strings used within a
context. For example, consider the GUI string "_Add". The underscore
indicates that the 'A' can be used as a keyboard shortcut. This might be
translated to Spanish as "_Agregar". However, one of the windows where
"_Add" is used may also contain a stock "_OK" button, which is
translated to Spanish as "_Aceptar". By using a separate message context
for different instances of "_Add", the translator is able to set
different translations for different contexts and thus choose different
keyboard shortcuts where appropriate.
---
 po/Makevars        |  4 ++--
 pyanaconda/i18n.py | 32 +++++++++++++++++++++++++++++++-
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/po/Makevars b/po/Makevars
index d0775e8..fe913f2 100644
--- a/po/Makevars
+++ b/po/Makevars
@@ -8,7 +8,7 @@ subdir = po
 top_builddir = ..
 
 # These options get passed to xgettext.
-XGETTEXT_OPTIONS = --keyword=_ --keyword=N_ --keyword=P_:1,2 --from-code=UTF-8
+XGETTEXT_OPTIONS = --keyword=_ --keyword=N_ --keyword=P_:1,2 --keyword=C_:1c,2 --keyword=CN_:1c,2 --keyword=CP_:1c,2,3 --from-code=UTF-8
 
 # This is the copyright holder that gets inserted into the header of the
 # $(DOMAIN).pot file.  Set this to the copyright holder of the surrounding
@@ -44,7 +44,7 @@ EXTRA_LOCALE_CATEGORIES =
 # context.  Possible values are "yes" and "no".  Set this to yes if the
 # package uses functions taking also a message context, like pgettext(), or
 # if in $(XGETTEXT_OPTIONS) you define keywords with a context argument.
-USE_MSGCTXT = no
+USE_MSGCTXT = yes
 
 # These options get passed to msgmerge.
 # Useful options are in particular:
diff --git a/pyanaconda/i18n.py b/pyanaconda/i18n.py
index 96cfef6..12ace35 100644
--- a/pyanaconda/i18n.py
+++ b/pyanaconda/i18n.py
@@ -19,10 +19,40 @@
 # Red Hat Author(s): Chris Lumens <clumens at redhat.com>
 #
 
-__all__ = ["_", "N_", "P_"]
+__all__ = ["_", "N_", "P_", "C_", "CN_", "CP_"]
 
 import gettext
 
 _ = lambda x: gettext.ldgettext("anaconda", x)
 N_ = lambda x: x
 P_ = lambda x, y, z: gettext.ldngettext("anaconda", x, y, z)
+
+# This is equivalent to "pgettext" in GNU gettext. The pgettext functions
+# are not exported by Python, but all they really do is a stick a EOT
+# character between msgctxt and msgid and check that msgctxt isn't part
+# of the return value.
+def C_(msgctxt, msgid):
+    ctxid = "%s\x04%s" % (msgctxt, msgid)
+    translation = _(ctxid)
+
+    # If there is no translation for msgctxt<EOT>msgid, return only msgid
+    if translation == ctxid:
+        return msgid
+    else:
+        return translation
+
+# Mark as translatable with context
+CN_ = lambda c, x: x
+
+# npgettext; i.e., gettext with plural form and context
+def CP_(msgctxt, msgid, msgid_plural, n):
+    ctxid = "%s\x04%s" % (msgctxt, msgid)
+    translation = P_(ctxid, msgid_plural, n)
+
+    # If the returned value is msgctxt<EOT>msgid, ngettext was trying to
+    # fallback to msgid. We don't add msgctxt to msgid_plural, so any other
+    # return value is correct.
+    if translation == ctxid:
+        return msgid
+    else:
+        return translation
-- 
1.8.3.1



More information about the anaconda-patches mailing list