Files
i2p.i2p/apps/routerconsole/java/src/net/i2p/router/web/LocaleWebAppHandler.java
zzz 4497463778 * 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.
2009-10-18 14:06:07 +00:00

70 lines
2.5 KiB
Java

package net.i2p.router.web;
import java.io.IOException;
import java.util.Locale;
import java.util.Map;
import net.i2p.I2PAppContext;
import org.mortbay.http.HttpRequest;
import org.mortbay.http.HttpResponse;
import org.mortbay.jetty.servlet.WebApplicationHandler;
/**
* Convert foo.jsp to foo_xx.jsp for language xx.
* This is appropriate for jsps with large amounts of text.
* This does not work for included jsps (e.g. summary*)
*
* @author zzz
*/
public class LocaleWebAppHandler extends WebApplicationHandler
{
private I2PAppContext _context;
public LocaleWebAppHandler(I2PAppContext ctx) {
super();
_context = ctx;
}
/**
* Handle foo.jsp by converting to foo_xx.jsp
* for language xx, where xx is the language for the default locale,
* or as specified in the routerconsole.lang property.
* Unless language==="en".
*/
public void handle(String pathInContext,
String pathParams,
HttpRequest httpRequest,
HttpResponse httpResponse)
throws IOException
{
//System.err.println("Path: " + pathInContext);
String newPath = pathInContext;
if (pathInContext.endsWith(".jsp")) {
int len = pathInContext.length();
// ...but leave foo_xx.jsp alone
if (len < 8 || pathInContext.charAt(len - 7) != '_') {
String lang = _context.getProperty(Messages.PROP_LANG);
if (lang == null || lang.length() <= 0)
lang = Locale.getDefault().getLanguage();
if (lang != null && lang.length() > 0 && !lang.equals("en")) {
String testPath = pathInContext.substring(0, len - 4) + '_' + lang + ".jsp";
// Do we have a servlet for the new path that isn't the catchall *.jsp?
Map.Entry servlet = getHolderEntry(testPath);
if (servlet != null) {
String servletPath = (String) servlet.getKey();
if (servletPath != null && !servletPath.startsWith("*")) {
// success!!
//System.err.println("Servlet is: " + servletPath);
newPath = testPath;
}
}
}
}
}
//System.err.println("New path: " + newPath);
super.handle(newPath, pathParams, httpRequest, httpResponse);
//System.err.println("Was handled? " + httpRequest.isHandled());
}
}