]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Implement msgctxt for Java ResourceBundles.
authorBruno Haible <bruno@clisp.org>
Sat, 1 Sep 2007 21:30:11 +0000 (21:30 +0000)
committerBruno Haible <bruno@clisp.org>
Tue, 23 Jun 2009 10:15:02 +0000 (12:15 +0200)
gettext-runtime/intl-java/ChangeLog
gettext-runtime/intl-java/gnu/gettext/GettextResource.java

index f69b9951b1ff03df01535edaf06c9e25e4a5c292..068c36c3fcf8fd8957dcbb86105687ae26a34985 100644 (file)
@@ -1,3 +1,13 @@
+2007-09-01  Bruno Haible  <bruno@clisp.org>
+
+       Implement msgctxt for Java ResourceBundles.
+       * gnu/gettext/GettextResource.java (gettextnull): New method.
+       (gettext): Use it.
+       (ngettextnull): New method, extracted from ngettext.
+       (ngettext): Use it.
+       (pgettext, npgettext): New methods.
+       Suggested by Felix Berger.
+
 2007-03-27  Bruno Haible  <bruno@clisp.org>
 
        * Makefile.am (javadoc2/index.html): Pass the package name to javadoc.
index b8ded37eea2b86d575cd87a6bfeeca561f7fcaee..727508f62eb069dff9bdf6995ab043461bf2b1fc 100644 (file)
@@ -1,5 +1,5 @@
 /* GNU gettext for Java
- * Copyright (C) 2001 Free Software Foundation, Inc.
+ * Copyright (C) 2001, 2007 Free Software Foundation, Inc.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Library General Public License as published
@@ -65,6 +65,18 @@ public abstract class GettextResource extends ResourceBundle {
 
   public static boolean verbose = false;
 
+  /**
+   * Like gettext(catalog,msgid), except that it returns <CODE>null</CODE>
+   * when no translation was found.
+   */
+  private static String gettextnull (ResourceBundle catalog, String msgid) {
+    try {
+      return (String)catalog.getObject(msgid);
+    } catch (MissingResourceException e) {
+      return null;
+    }
+  }
+
   /**
    * Returns the translation of <VAR>msgid</VAR>.
    * @param catalog a ResourceBundle
@@ -73,25 +85,17 @@ public abstract class GettextResource extends ResourceBundle {
    *         none is found
    */
   public static String gettext (ResourceBundle catalog, String msgid) {
-    try {
-      String result = (String)catalog.getObject(msgid);
-      if (result != null)
-        return result;
-    } catch (MissingResourceException e) {
-    }
+    String result = gettextnull(catalog,msgid);
+    if (result != null)
+      return result;
     return msgid;
   }
 
   /**
-   * Returns the plural form for <VAR>n</VAR> of the translation of
-   * <VAR>msgid</VAR>.
-   * @param catalog a ResourceBundle
-   * @param msgid the key string to be translated, an ASCII string
-   * @param msgid_plural its English plural form
-   * @return the translation of <VAR>msgid</VAR> depending on <VAR>n</VAR>,
-   *         or <VAR>msgid</VAR> or <VAR>msgid_plural</VAR> if none is found
+   * Like ngettext(catalog,msgid,msgid_plural,n), except that it returns
+   * <CODE>null</CODE> when no translation was found.
    */
-  public static String ngettext (ResourceBundle catalog, String msgid, String msgid_plural, long n) {
+  private static String ngettextnull (ResourceBundle catalog, String msgid, long n) {
     // The reason why we use so many reflective API calls instead of letting
     // the GNU gettext generated ResourceBundles implement some interface,
     // is that we want the generated ResourceBundles to be completely
@@ -205,6 +209,60 @@ public abstract class GettextResource extends ResourceBundle {
         // Found the value. It doesn't depend on n in this case.
         return (String)value;
     }
+    // Default: null.
+    return null;
+  }
+
+  /**
+   * Returns the plural form for <VAR>n</VAR> of the translation of
+   * <VAR>msgid</VAR>.
+   * @param catalog a ResourceBundle
+   * @param msgid the key string to be translated, an ASCII string
+   * @param msgid_plural its English plural form
+   * @return the translation of <VAR>msgid</VAR> depending on <VAR>n</VAR>,
+   *         or <VAR>msgid</VAR> or <VAR>msgid_plural</VAR> if none is found
+   */
+  public static String ngettext (ResourceBundle catalog, String msgid, String msgid_plural, long n) {
+    String result = ngettextnull(catalog,msgid,n);
+    if (result != null)
+      return result;
+    // Default: English strings and Germanic plural rule.
+    return (n != 1 ? msgid_plural : msgid);
+  }
+
+  /* The separator between msgctxt and msgid.  */
+  private static final String CONTEXT_GLUE = "\u0004";
+
+  /**
+   * Returns the translation of <VAR>msgid</VAR> in the context of
+   * <VAR>msgctxt</VAR>.
+   * @param catalog a ResourceBundle
+   * @param msgctxt the context for the key string, an ASCII string
+   * @param msgid the key string to be translated, an ASCII string
+   * @return the translation of <VAR>msgid</VAR>, or <VAR>msgid</VAR> if
+   *         none is found
+   */
+  public static String pgettext (ResourceBundle catalog, String msgctxt, String msgid) {
+    String result = gettextnull(catalog,msgctxt+CONTEXT_GLUE+msgid);
+    if (result != null)
+      return result;
+    return msgid;
+  }
+
+  /**
+   * Returns the plural form for <VAR>n</VAR> of the translation of
+   * <VAR>msgid</VAR> in the context of <VAR>msgctxt</VAR>.
+   * @param catalog a ResourceBundle
+   * @param msgctxt the context for the key string, an ASCII string
+   * @param msgid the key string to be translated, an ASCII string
+   * @param msgid_plural its English plural form
+   * @return the translation of <VAR>msgid</VAR> depending on <VAR>n</VAR>,
+   *         or <VAR>msgid</VAR> or <VAR>msgid_plural</VAR> if none is found
+   */
+  public static String npgettext (ResourceBundle catalog, String msgctxt, String msgid, String msgid_plural, long n) {
+    String result = ngettextnull(catalog,msgctxt+CONTEXT_GLUE+msgid,n);
+    if (result != null)
+      return result;
     // Default: English strings and Germanic plural rule.
     return (n != 1 ? msgid_plural : msgid);
   }