forked from I2P_Developers/i2p.i2p
* Router Console translation infrastructure:
- Persistent lang setting with routerconsole.lang=xx
- Loading any page with ?lang=xx changes the persistent setting
- Add a custom Jetty handler to load foo_xx.jsp if it
exists for language xx. This is for jsp files with lots
of text in them. Otherwise use inline translate methods.
Not for included jsps.
- Add a script to create and update messages_xx.po translation
files, and create ResourceBundles from them
- Add class to translate strings from cached ResourceBundles
- Add translate wrappers to HelperBase, FormHandler, and *Renderer,
so calls can be made from both jsp and java files
- Add two example translations on configupdate.jsp - one in
the jsp itself and one in the helper.
- This is for strings in routerconsole only. Will be expanded
to other webapps and the router later.
This commit is contained in:
78
apps/routerconsole/java/src/net/i2p/router/web/Messages.java
Normal file
78
apps/routerconsole/java/src/net/i2p/router/web/Messages.java
Normal file
@@ -0,0 +1,78 @@
|
||||
package net.i2p.router.web;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.util.ConcurrentHashSet;
|
||||
|
||||
/**
|
||||
* Translate strings efficiently.
|
||||
* We don't include an English or default ResourceBundle, we simply check
|
||||
* for "en" and return the original string.
|
||||
* Support real-time language changing with the routerconsole.lang property.
|
||||
*
|
||||
* @author zzz, from a base generated by eclipse.
|
||||
*/
|
||||
public class Messages {
|
||||
public static final String PROP_LANG = "routerconsole.lang";
|
||||
private static final String BUNDLE_NAME = "net.i2p.router.web.messages";
|
||||
private static final String _localeLang = Locale.getDefault().getLanguage();
|
||||
private static final Map<String, ResourceBundle> _bundles = new ConcurrentHashMap(2);
|
||||
private static final Set<String> _missing = new ConcurrentHashSet(2);
|
||||
|
||||
/** current locale **/
|
||||
public static String getString(String key) {
|
||||
if (_localeLang.equals("en"))
|
||||
return key;
|
||||
ResourceBundle bundle = findBundle(_localeLang);
|
||||
if (bundle == null)
|
||||
return key;
|
||||
try {
|
||||
return bundle.getString(key);
|
||||
} catch (MissingResourceException e) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
/** lang in routerconsole.lang property, else current locale */
|
||||
public static String getString(String key, I2PAppContext ctx) {
|
||||
String lang = getLanguage(ctx);
|
||||
if (lang.equals("en"))
|
||||
return key;
|
||||
ResourceBundle bundle = findBundle(lang);
|
||||
if (bundle == null)
|
||||
return key;
|
||||
try {
|
||||
return bundle.getString(key);
|
||||
} catch (MissingResourceException e) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
private static String getLanguage(I2PAppContext ctx) {
|
||||
String lang = ctx.getProperty(PROP_LANG);
|
||||
if (lang == null || lang.length() <= 0)
|
||||
lang = _localeLang;
|
||||
return lang;
|
||||
}
|
||||
|
||||
/** cache both found and not found for speed */
|
||||
private static ResourceBundle findBundle(String lang) {
|
||||
ResourceBundle rv = _bundles.get(lang);
|
||||
if (rv == null && !_missing.contains(lang)) {
|
||||
try {
|
||||
// Would it be faster to specify a class loader?
|
||||
// No matter we only do this once per lang.
|
||||
rv = ResourceBundle.getBundle(BUNDLE_NAME, new Locale(lang));
|
||||
} catch (MissingResourceException e) {
|
||||
_missing.add(lang);
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user