* Translate: Add GNU ngettext (plurals) support

This commit is contained in:
zzz committed 2010-05-27 00:38:32 +00:00
1 parent 9132e94143
commit dc3378d084
14 files changed
+377 -39

No files matched your search

+36
View File
@@ -8,6 +8,8 @@ import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import gnu.gettext.GettextResource;
import net.i2p.I2PAppContext;
import net.i2p.util.ConcurrentHashSet;
@@ -102,6 +104,40 @@ public abstract class Translate {
}
}
/**
* Use GNU ngettext
* For .po file format see http://www.gnu.org/software/gettext/manual/gettext.html.gz#Translating-plural-forms
*
* @param n how many
* @param s singluar string, optionally with {0} e.g. "one tunnel"
* @param s plural string optionally with {0} e.g. "{0} tunnels"
* @since 0.7.14
*/
public static String getString(int n, String s, String p, I2PAppContext ctx, String bun) {
String lang = getLanguage(ctx);
if (lang.equals(TEST_LANG))
return TEST_STRING + '(' + n + ')' + TEST_STRING;
ResourceBundle bundle = null;
if (!lang.equals("en"))
bundle = findBundle(bun, lang);
String x;
if (bundle == null)
x = n == 1 ? s : p;
else
x = GettextResource.ngettext(bundle, s, p, n);
Object[] oArray = new Object[1];
oArray[0] = Integer.valueOf(n);
try {
MessageFormat fmt = new MessageFormat(x, new Locale(lang));
return fmt.format(oArray, new StringBuffer(), null).toString();
} catch (IllegalArgumentException iae) {
System.err.println("Bad format: sing: \"" + s +
"\" plural: \"" + p +
"\" lang: " + lang);
return "FIXME: " + s + ' ' + p + ',' + n;
}
}
/** @return lang in routerconsole.lang property, else current locale */
public static String getLanguage(I2PAppContext ctx) {
String lang = ctx.getProperty(PROP_LANG);