Bug 536996 – Missing noop i18n macro equivalent to C_
authorMatthias Clasen <matthiasc@src.gnome.org>
Fri, 18 Jul 2008 18:14:04 +0000 (18:14 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Fri, 18 Jul 2008 18:14:04 +0000 (18:14 +0000)
        * glib/glib.symbols:
        * glib/gstrfuncs.[hc]: Add g_dpgettext2() which is a
        variant of g_dpgettext() taking context and id as separate
        arguments.

        * glib/gi18n-lib.h:
        * glib/gi18n.h: Add an NC_() macro that is to C_() as N_()
        is to _().

svn path=/trunk/; revision=7202

ChangeLog
docs/reference/ChangeLog
docs/reference/glib/glib-sections.txt
docs/reference/glib/tmpl/i18n.sgml
glib/gi18n-lib.h
glib/gi18n.h
glib/glib.symbols
glib/gstrfuncs.c
glib/gstrfuncs.h

index 1b8bd6305ae758d3cdc05905c260899e39548ff7..571e21f775718f0e3e7981c35afed53ebb2e8107 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2008-07-18  Matthias Clasen  <mclasen@redhat.com>
+
+       Bug 536996 – Missing noop i18n macro equivalent to C_
+
+       * glib/glib.symbols:
+       * glib/gstrfuncs.[hc]: Add g_dpgettext2() which is a 
+       variant of g_dpgettext() taking context and id as separate
+       arguments.
+
+       * glib/gi18n-lib.h:
+       * glib/gi18n.h: Add an NC_() macro that is to C_() as N_()
+       is to _().
+
 2008-07-18  Matthias Clasen  <mclasen@redhat.com>
 
        * tests/Makefile.am:
index 00690cf054c4b663365663666465604295eeab44..cc8f1afc78c91f92fa5dcf5a46206936b4b662c7 100644 (file)
@@ -1,3 +1,8 @@
+2008-07-18  Matthias Clasen  <mclasen@redhat.com>
+
+       * glib/glib-sections.txt:
+       * glib/tmpl/i18n.sgml: Add new gettext stuff
+
 2008-07-18  Matthias Clasen  <mclasen@redhat.com>
 
        Bug 530759 – update the gobject tutorial to the XXI century
index 575e3f7a740b1636b63dfeaea8ae405b0b9a541e..90f9899e5a7c7c892c94f4bb138cc3fda91586fb 100644 (file)
@@ -2503,9 +2503,11 @@ g_unichar_to_utf8
 Q_
 C_
 N_
+NC_
 g_dgettext
 g_dngettext
 g_dpgettext
+g_dpgettext2
 g_strip_context
 <SUBSECTION>
 g_get_language_names
index 4c1fe7980b67167eddad737df66573ebf9edf4ae..b9ce778e1b1cb700e6b2f1bb93abf9bde44610a4 100644 (file)
@@ -148,6 +148,41 @@ To get the translated string, call gettext() at runtime.
 @Returns: 
 
 
+<!-- ##### MACRO NC_ ##### -->
+<para>
+Only marks a string for translation, with context.
+This is useful in situations where the translated strings can't
+be directly used, e.g. in string array initializers. 
+To get the translated string, you should call g_dpgettext2() at runtime.
+</para>
+|[
+     {
+       static const char *messages[] = {
+         NC_("some context", "some very meaningful message"),
+         NC_("some context", "and another one")
+       };
+       const char *string;
+       ...
+       string
+         = index &gt; 1 ? g_dpgettext2 (NULL, "some context", "a default message") : g_dpgettext2 (NULL, "some context", messages[index]);
+<!-- -->     
+       fputs (string);
+       ...
+     }
+]|
+
+<note><para>
+If you are using the NC_() macro, you need to make sure that you
+pass <option>--keyword=NC_:1c,2</option> to xgettext when extracting
+messages. Note that this only works with GNU gettext >= 0.15.
+</para></note>
+
+
+@Context: a message context, must be a string literal
+@String: a message id, must be a string literal
+@Since: 2.18
+
+
 <!-- ##### FUNCTION g_strip_context ##### -->
 <para>
 
index f5bcc180d6915651c230bc8fff7ad9ffdea295b8..b8be9aed05c87a8c1ba1e7772c8e8f7ea30836ba 100644 (file)
@@ -32,5 +32,6 @@
 #define Q_(String) g_dpgettext (GETTEXT_PACKAGE, String, 0)
 #define N_(String) (String)
 #define C_(Context,String) g_dpgettext (GETTEXT_PACKAGE, Context "\004" String, strlen (Context) + 1)
+#define NC_(Context, String) (String)
 
 #endif  /* __G_I18N_LIB_H__ */
index 011b52f89e4c7ebd3158d850a3410ac15c194ac5..b835546446c160e7128d373c92c9db86f614054d 100644 (file)
@@ -28,5 +28,6 @@
 #define Q_(String) g_dpgettext (NULL, String, 0)
 #define N_(String) (String)
 #define C_(Context,String) g_dpgettext (NULL, Context "\004" String, strlen (Context) + 1)
+#define NC_(Context, String) (String)
 
 #endif  /* __G_I18N_H__ */
index 76267f6614efcb615f80682eadcbd54a4c6ac319..d5cb4e8448ea980d748800f90fe192c618e766d2 100644 (file)
@@ -1154,6 +1154,7 @@ g_strip_context G_GNUC_FORMAT(1)
 g_dgettext G_GNUC_FORMAT(2)
 g_dngettext G_GNUC_FORMAT(3)
 g_dpgettext G_GNUC_FORMAT(2)
+g_dpgettext2 G_GNUC_FORMAT(3)
 #endif
 #endif
 
index fcb09374013433c686c1c8306d334862744433cb..ee07cc356a1e66d7f7bc7c2d2f8eb7b00cf9c1df 100644 (file)
@@ -2909,6 +2909,62 @@ g_dpgettext (const gchar *domain,
   return translation;
 }
 
+/* This function is taken from gettext.h 
+ * GNU gettext uses '\004' to separate context and msgid in .mo files.
+ */
+/**
+ * g_dpgettext2:
+ * @domain: the translation domain to use, or %NULL to use
+ *   the domain set with textdomain()
+ * @context: the message context
+ * @msgid: the message
+ *
+ * This function is a variant of g_dgettext() which supports
+ * a disambiguating message context. GNU gettext uses the
+ * '\004' character to separate the message context and
+ * message id in @msgctxtid.
+ *
+ * This uses g_dgettext() internally.  See that functions for differences
+ * with dgettext() proper.
+ *
+ * This function differs from C_() in that it is not a macro and 
+ * thus you may use non-string-literals as context and msgid arguments.
+ *
+ * Returns: The translated string
+ *
+ * Since: 2.18
+ */
+G_CONST_RETURN char *
+g_dpgettext2 (const char *domain,
+              const char *msgctxt,
+              const char *msgid)
+{
+  size_t msgctxt_len = strlen (msgctxt) + 1;
+  size_t msgid_len = strlen (msgid) + 1;
+  const char *translation;
+  char* msg_ctxt_id;
+
+  msg_ctxt_id = g_alloca (msgctxt_len + msgid_len);
+
+  memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
+  msg_ctxt_id[msgctxt_len - 1] = '\004';
+  memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
+
+  translation = g_dgettext (domain, msg_ctxt_id);
+
+  if (translation == msg_ctxt_id) 
+    {
+      /* try the old way of doing message contexts, too */
+      msg_ctxt_id[msgctxt_len - 1] = '|';
+      translation = g_dgettext (domain, msg_ctxt_id);
+
+      if (translation == msg_ctxt_id)
+        return msgid;
+    }
+
+  return translation;
+}
+
 static gboolean
 _g_dgettext_should_translate (void)
 {
index f58a2856860fc440e8bb7729f1bf10653a712afb..ee291f80fc88a4624c5347bda1cf9b9cab3de058 100644 (file)
@@ -257,6 +257,9 @@ G_CONST_RETURN gchar *g_dngettext      (const gchar *domain,
 G_CONST_RETURN gchar *g_dpgettext      (const gchar *domain,
                                         const gchar *msgctxtid,
                                         gsize        msgidoffset) G_GNUC_FORMAT(2);
+G_CONST_RETURN gchar *g_dpgettext2     (const gchar *domain,
+                                        const gchar *context,
+                                        const gchar *msgid) G_GNUC_FORMAT(3);
 
 G_END_DECLS