← Back to team overview

anewt-developers team mailing list archive

[Branch ~uws/anewt/anewt.uws] Rev 1764: [i18n] Implement contextual translation support

 

------------------------------------------------------------
revno: 1764
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt.uws
timestamp: Mon 2010-02-22 23:56:16 +0100
message:
  [i18n] Implement contextual translation support
  
  Implemented the pgettext() function in PHP since it is not
  provided by the gettext bindings in PHP itself. The
  pgettext() function also has a C_() alias (inspired by the
  i18n support in GLib).
  
  The i18n.make script also correctly detects C_() when
  extracting translatable strings from source files.
  
  Part of bug #522405.
modified:
  i18n/gettext.lib.php
  scripts/i18n.make


--
lp:anewt
https://code.launchpad.net/~uws/anewt/anewt.uws

Your team Anewt developers is subscribed to branch lp:anewt.
To unsubscribe from this branch go to https://code.launchpad.net/~uws/anewt/anewt.uws/+edit-subscription.
=== modified file 'i18n/gettext.lib.php'
--- i18n/gettext.lib.php	2006-05-16 07:08:18 +0000
+++ i18n/gettext.lib.php	2010-02-22 22:56:16 +0000
@@ -63,9 +63,13 @@
 }
 
 /**
- * Just like _(), but strips the context (the part before the first | character)
- * if the translation is the same as the original string. This function can be
- * used to provide context to the translator.
+ * Just like gettext(), but strips the context (the part before the first
+ * <code>|</code> character) if the translation is the same as the original
+ * string. This is like pgettext, but with the context specified in the string,
+ * not as a separate argument.
+ *
+ * \deprecated
+ *   This method is deprecated; use pgettext() instead.
  *
  * \param $str
  *   The string to translate.
@@ -74,8 +78,8 @@
  *   The translated string, or the original string if no translation was
  *   found (with any prefix stripped)
  *
- * \see
- *   _
+ * \see pgettext
+ * \see gettext
  */
 function Q_($str) {
 	assert('is_string($str)');
@@ -97,6 +101,46 @@
 	return $str;
 }
 
+/**
+ * Translate a message with disambiguating context.
+ *
+ * This function can be used to provide context to the translator.
+ *
+ * \param $ctx
+ *   The context of the message.
+ * \param $str
+ *   The string to translate.
+ *
+ * \return
+ *   The translated string, or the original string if no translation was
+ *   found.
+ */
+function pgettext($ctx, $str)
+{
+	/* The .mo files generated by gettext > 0.15 have context separated from the
+	 * message itself using 0x04 as a separator. */
+
+	$str_with_prefix = sprintf("%s\004%s", $ctx, $str);
+	$translated = gettext($str_with_prefix);
+
+	/* Translation succeeded? */
+	if ($str_with_prefix != $translated)
+		return $translated;
+
+	return $str;
+}
+
+/**
+ * Alias for pgettext.
+ *
+ * \param $ctx
+ * \param $str
+ * \see pgettext
+ */
+function C_($ctx, $str) {
+	return pgettext($ctx, $str);
+}
+
 
 /* Check for the gettext() function. If it was not found, we include the
  * fallback functions. */

=== modified file 'scripts/i18n.make'
--- scripts/i18n.make	2008-02-21 14:25:10 +0000
+++ scripts/i18n.make	2010-02-22 22:56:16 +0000
@@ -17,7 +17,7 @@
 POFILES = $(wildcard *.po)
 GMOFILES = $(POFILES:%.po=%.gmo)
 
-XGETTEXT_ARGS="--keyword=Q_ --keyword=N_"
+XGETTEXT_ARGS="--keyword=gettext_noop --keyword=pgettext:1c,2 --keyword=N_ --keyword=C_:1c,2 --keyword=Q_"
 
 %.gmo: %.po
 	@echo -n "$<: "