summaryrefslogtreecommitdiffstats
path: root/gettext-runtime/intl-java
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2007-09-01 21:30:11 +0000
committerBruno Haible <bruno@clisp.org>2009-06-23 12:15:02 +0200
commitb9661e07421f67e960d4ef5f5e5786c52c8b6b7b (patch)
tree09ec836a4a0f2f6050bd55952bbc19ea24ff78b6 /gettext-runtime/intl-java
parent262cdd315aa1535e059feaedd348b4c3784a374b (diff)
downloadexternal_gettext-b9661e07421f67e960d4ef5f5e5786c52c8b6b7b.zip
external_gettext-b9661e07421f67e960d4ef5f5e5786c52c8b6b7b.tar.gz
external_gettext-b9661e07421f67e960d4ef5f5e5786c52c8b6b7b.tar.bz2
Implement msgctxt for Java ResourceBundles.
Diffstat (limited to 'gettext-runtime/intl-java')
-rw-r--r--gettext-runtime/intl-java/ChangeLog10
-rw-r--r--gettext-runtime/intl-java/gnu/gettext/GettextResource.java88
2 files changed, 83 insertions, 15 deletions
diff --git a/gettext-runtime/intl-java/ChangeLog b/gettext-runtime/intl-java/ChangeLog
index f69b995..068c36c 100644
--- a/gettext-runtime/intl-java/ChangeLog
+++ b/gettext-runtime/intl-java/ChangeLog
@@ -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.
diff --git a/gettext-runtime/intl-java/gnu/gettext/GettextResource.java b/gettext-runtime/intl-java/gnu/gettext/GettextResource.java
index b8ded37..727508f 100644
--- a/gettext-runtime/intl-java/gnu/gettext/GettextResource.java
+++ b/gettext-runtime/intl-java/gnu/gettext/GettextResource.java
@@ -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
@@ -66,6 +66,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
* @param msgid the key string to be translated, an ASCII string
@@ -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);
}