【发布时间】:2011-02-05 07:35:55
【问题描述】:
我可以将 LocalizedResource.properties 与 uibinder 一起使用。假设我有在 java 文件中以编程方式创建的小部件。如何根据用户选择的语言“en,fr..etc”从 LocalizedResource.properties 中读取密钥?
【问题讨论】:
标签: java gwt internationalization uibinder
我可以将 LocalizedResource.properties 与 uibinder 一起使用。假设我有在 java 文件中以编程方式创建的小部件。如何根据用户选择的语言“en,fr..etc”从 LocalizedResource.properties 中读取密钥?
【问题讨论】:
标签: java gwt internationalization uibinder
@Paŭlo Ebermann
此方法不适用于 GWT,因为 GWT 无法翻译 Locale 和
ResourceBUndle 到 JavaScript。
我只是试试。
Locale loc = new Locale(LocaleInfo.getCurrentLocale().getLocaleName());
String key = "AnotherWord";
ResourceBundle bundle = ResourceBundle.getBundle("msgs", loc);
GWT 编译失败
[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?
如果我尝试使用 java.util.* 源代码提供 GWT 编译器,它可能会起作用。但我认为这不是一个好主意。为什么 Google 员工不这样做?
【讨论】:
很难说清楚。
如果您的 LocalizedResource.properties 是按照“Internationalization - UiBinder”的描述生成的,那么我不明白您为什么要从中读取密钥
如果 .properties 用于 Message 或 Constant 接口,那么您可以通过 http://*.html?locale=fr_CA 阅读 *fr_CA.properties 等任何您想要的语言。
有用的链接是Internationalizing GWT: Creating the translation for each language supported
或尝试 @UiTemplate 在为不同语言准备的模板之间切换。要了解当前的语言环境,您可以使用LocaleInfo.getLocaleName()。
【讨论】:
我对 GWT 和 UIBinder 一无所知,但在“标准版”Java 中,您将创建您选择的语言 (Locale) 的 ResourceBundle,然后使用其 getString 方法。
Locale loc = ...;
String key = ...;
ResourceBundle bundle =
ResourceBundle.getBundle("LocalizedResource", loc);
String value = bundle.getString(key);
然后你可以使用这个字符串来标记你的小部件。
请尝试此操作并在 GWT 中报告成功。
【讨论】: