read key from LocalizedResource.properties programmatically

这一生的挚爱 提交于 2019-12-24 09:39:06

问题


I able to use LocalizedResource.properties with uibinder. let say i have widget that created programmatically in java file. how to read in key from LocalizedResource.properties depending on language "en,fr..etc" selected by users?


回答1:


It's difficult to say something definitely.

  1. If your LocalizedResource.properties is generated as described by "Internationalization - UiBinder" so I don't understand why you want to read in key from it

  2. If .properties made for Message or Constant interface so you could read *fr_CA.properties by http://*.html?locale=fr_CA and so for any languge you want.

    Helpful link is Internationalizing GWT: Creating the translation for each language supported

  3. or try @UiTemplate to switch between tamplates prepared for different languages. To find out current locale you can use LocaleInfo.getLocaleName().

    Helpful link is Apply different XML templates to the same widget




回答2:


@Paŭlo Ebermann

This method doesn't work for GWT because GWT can't translate java classes like Locale and ResourceBUndle to JavaScript.

I just try it.

Locale loc = new Locale(LocaleInfo.getCurrentLocale().getLocaleName());
String key = "AnotherWord";
ResourceBundle bundle = ResourceBundle.getBundle("msgs", loc);

GWT Compilation fail with

[ERROR] Errors in 'file:/K:/programming/eclipse-workspace/polyglotte/src/com/mw/uibinder/client/Polyglotte.java'
  [ERROR] Line 64: No source code is available for type java.util.Locale; did you forget to inherit a required module?
  [ERROR] Line 67: No source code is available for type java.util.ResourceBundle; did you forget to inherit a required module?

May be it will work if I try feed GWT Compiler with java.util.* source codes. But I think it isn't good idea. Why Googlers don't follow this way?




回答3:


I don't know anything about GWT and UIBinder, but in "Standard Edition" Java, you would create a ResourceBundle of your selected language (Locale), and then use its getString method.

Locale loc = ...;
String key = ...;
ResourceBundle bundle =
     ResourceBundle.getBundle("LocalizedResource", loc);

String value = bundle.getString(key);

Then you can use this string to label your widget.

Please try this and report the success in GWT.



来源:https://stackoverflow.com/questions/4905699/read-key-from-localizedresource-properties-programmatically

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