【问题标题】:read key from LocalizedResource.properties programmatically以编程方式从 LocalizedResource.properties 读取密钥
【发布时间】:2011-02-05 07:35:55
【问题描述】:

我可以将 LocalizedResource.properties 与 uibinder 一起使用。假设我有在 java 文件中以编程方式创建的小部件。如何根据用户选择的语言“en,fr..etc”从 LocalizedResource.properties 中读取密钥?

【问题讨论】:

    标签: java gwt internationalization uibinder


    【解决方案1】:

    @Paŭlo Ebermann

    此方法不适用于 GWT,因为 GWT 无法翻译 LocaleResourceBUndle 到 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 员工不这样做?

    【讨论】:

      【解决方案2】:

      很难说清楚。

      1. 如果您的 LocalizedResource.properties 是按照“Internationalization - UiBinder”的描述生成的,那么我不明白您为什么要从中读取密钥

      2. 如果 .properties 用于 Message 或 Constant 接口,那么您可以通过 http://*.html?locale=fr_CA 阅读 *fr_CA.properties 等任何您想要的语言。

        有用的链接是Internationalizing GWT: Creating the translation for each language supported

      3. 或尝试 @UiTemplate 在为不同语言准备的模板之间切换。要了解当前的语言环境,您可以使用LocaleInfo.getLocaleName()

        有用的链接是Apply different XML templates to the same widget

      【讨论】:

      • 因为我确实提到过,如果我以编程方式创建小部件,我需要直接在代码中获取 .properties 而不是 uibinder.xml
      【解决方案3】:

      我对 GWT 和 UIBinder 一无所知,但在“标准版”Java 中,您将创建您选择的语言 (Locale) 的 ResourceBundle,然后使用其 getString 方法。

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

      然后你可以使用这个字符串来标记你的小部件。

      请尝试此操作并在 GWT 中报告成功。

      【讨论】:

        猜你喜欢
        • 2022-11-07
        • 2020-02-01
        • 1970-01-01
        • 2014-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-19
        相关资源
        最近更新 更多