GWT Internationalization (i18n) in server side

二次信任 提交于 2019-12-13 00:57:24

问题


I have a GWT application with internationalization support (in client side).

I have a Messages class:

/**
 * Interface to represent the messages contained in resource bundle:
 */
public interface Messages extends com.google.gwt.i18n.client.Messages {

  /**
  * Translated "No".
  * 
  * @return translated "No"
  */
  @DefaultMessage("No")
  @Key("NO")
  String NO();
}

I have two properties Messages.properties and Messages_fr.properties.

I also have this configuration :

<inherits name="com.google.gwt.i18n.I18N" />
<extend-property name="locale" values="fr" />
<set-configuration-property name="locale.useragent" value="Y"/>

And in client side i do this:

private final Messages messages = GWT.create(Messages.class);
//...
messages.NO(); 

Client side with internationalization is working but if I add theses follwoing lines in server side:

private final Messages messages = GWT.create(Messages.class);

I have an error, because GWT.create it's only for client side.

Do you know how can I display internationalization messages in the server side?

Thanks!


回答1:


Keep in mind that GWT transforms client java code in javascript so that browsers can execute it. That's why your server can't do anything with your Messages class and throws errors. See the package client in com.google.gwt.i18n.client.Messages ? This is client-side code, so in the end, it will be js.

A good start for internationalization in server-side code could be http://docs.oracle.com/javase/tutorial/i18n/

You can still use your .properties you are using in client-side code, but you have to use plain java approach to access them. An example is :

ResourceBundle bundle = ResourceBundle.getBundle("com.example.client.i18n.myresource");
bundle.getString("stringToRetrieve");



回答2:


Not a specific answer, but unfortunately GWT currently doesn't have support for fully reusing the client messages file on the server, although there is work-in-progress to add it to GWT. John Tamplin gave a talk at the GWT Meet-up 2013 about the progress and the problems with re-using the client side messages on the server side. You can find the presentation at the meetup youtube channel.



来源:https://stackoverflow.com/questions/18075453/gwt-internationalization-i18n-in-server-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!