开发者

GWT I18N on the server side

开发者 https://www.devze.com 2023-01-12 11:08 出处:网络
What is the best way to implement GW开发者_如何学编程T Server Side Internationalization? Use native Java properties files (not sure how to read and how to locate the right language file) (unicode st

What is the best way to implement GW开发者_如何学编程T Server Side Internationalization?

  1. Use native Java properties files (not sure how to read and how to locate the right language file) (unicode string need to be ASCII encoded)

  2. Use GWTI18N.java - GWT module which gives you seamless use of GWT I18N on both the client and the server and uses "java.lang.reflect.Proxy method"

  3. Use Kotori I18N - ...

  4. Other ideas?

How can I find and pass localization from client to sever?

On the server side I have an Servlet which still doesn't use any GWT dependant source, is it better not to do so?


I found this solution and it looks very good

gwt-i18n-server - Provides a simple support of gwt i18n feature on the server side

The aim is to permit to the GWT developer to use their Constants and Messages interfaces on the server side (See internationzation). The implementation is based on java reflect api. It loads the properties files from the classpath (same folder than the interface). It supports Constants, ConstantsWithLookup, Messages (plural too). The licence is LGPL.

Client current locale can be found this way:

LocaleInfo.getCurrentLocale().getLocaleName()


Following other threads here in SO, I came up with this solution that also considers the encoding used for the properties files (which can be troublesome as ResourceBundle uses by default "ISO-8859-1"):

import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;

public class MyResourceBundle {

    // feature variables
    private ResourceBundle bundle;
    private String fileEncoding;

    public MyResourceBundle(Locale locale, String fileEncoding){
        this.bundle = ResourceBundle.getBundle("com.app.Bundle", locale);
        this.fileEncoding = fileEncoding;
    }

    public MyResourceBundle(Locale locale){
        this(locale, "UTF-8");
    }

    public String getString(String key){
        String value = bundle.getString(key); 
        try {
            return new String(value.getBytes("ISO-8859-1"), fileEncoding);
        } catch (UnsupportedEncodingException e) {
            return value;
        }
    }
}

The way to use this would be very similar than the regular ResourceBundle usage:

private MyResourceBundle labels = new MyResourceBundle("es", "UTF-8");
String label = labels.getString(key)

Or you can use the alternate constructor which uses UTF-8 by default:

private MyResourceBundle labels = new MyResourceBundle("es");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号