简体   繁体   English

GWT I18N在服务器端

[英]GWT I18N on the server side

What is the best way to implement GWT Server Side Internationalization? 实施GWT服务器端国际化的最佳方法是什么?

  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) 使用本机Java属性文件(不确定如何读取以及如何找到正确的语言文件)(unicode字符串需要进行ASCII编码)

  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" 使用GWTI18N.java - GWT模块,它可以让您在客户端和服务器上无缝使用GWT I18N,并使用“java.lang.reflect.Proxy方法”

  3. Use Kotori I18N - ... 使用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? 在服务器端,我有一个仍然不使用任何GWT依赖源的Servlet,最好不要这样做吗?

I found this solution and it looks very good 我找到了这个解决方案,它看起来非常好

gwt-i18n-server - Provides a simple support of gwt i18n feature on the server side gwt-i18n-server - 在服务器端提供gwt i18n功能的简单支持

The aim is to permit to the GWT developer to use their Constants and Messages interfaces on the server side (See internationzation). 目的是允许GWT开发人员在服务器端使用他们的常量和消息接口(参见国际化)。 The implementation is based on java reflect api. 该实现基于java反映api。 It loads the properties files from the classpath (same folder than the interface). 它从类路径加载属性文件(与接口相同的文件夹)。 It supports Constants, ConstantsWithLookup, Messages (plural too). 它支持Constants,ConstantsWithLookup,Messages(复数)。 The licence is LGPL. 许可证是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"): 在SO中的其他线程之后,我想出了这个解决方案,它也考虑了用于属性文件的编码(这可能很麻烦,因为ResourceBundle默认使用“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: 使用它的方法与常规的ResourceBundle用法非常相似:

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: 或者您可以使用默认使用UTF-8的备用构造函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM