From: Bruno Haible Date: Sat, 1 Sep 2007 21:30:11 +0000 (+0000) Subject: Implement msgctxt for Java ResourceBundles. X-Git-Tag: v0.17~273 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b9661e07421f67e960d4ef5f5e5786c52c8b6b7b;p=thirdparty%2Fgettext.git Implement msgctxt for Java ResourceBundles. --- diff --git a/gettext-runtime/intl-java/ChangeLog b/gettext-runtime/intl-java/ChangeLog index f69b9951b..068c36c3f 100644 --- a/gettext-runtime/intl-java/ChangeLog +++ b/gettext-runtime/intl-java/ChangeLog @@ -1,3 +1,13 @@ +2007-09-01 Bruno Haible + + 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 * 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 b8ded37ee..727508f62 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 @@ -65,6 +65,18 @@ public abstract class GettextResource extends ResourceBundle { public static boolean verbose = false; + /** + * Like gettext(catalog,msgid), except that it returns null + * 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 msgid. * @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 n of the translation of - * msgid. - * @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 msgid depending on n, - * or msgid or msgid_plural if none is found + * Like ngettext(catalog,msgid,msgid_plural,n), except that it returns + * null 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 n of the translation of + * msgid. + * @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 msgid depending on n, + * or msgid or msgid_plural 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 msgid in the context of + * msgctxt. + * @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 msgid, or msgid 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 n of the translation of + * msgid in the context of msgctxt. + * @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 msgid depending on n, + * or msgid or msgid_plural 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); }