动态生成内容的 GWT 国际化 [英] GWT Internationalization for dynamically generated content

查看:16
本文介绍了动态生成内容的 GWT 国际化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的大部分应用程序,我能够使用 https://developers.google.com/web-toolkit/doc/latest/DevGuideI18n(主要是 UIBinder 方法).

For a majority of my app I am able to use the recommended internationalization techniques as laid out in https://developers.google.com/web-toolkit/doc/latest/DevGuideI18n (mainly the UIBinder approach).

我目前使用单个小部件接收对象并动态显示其属性/字段,因此无法使用 UIBinder 和上述国际化技术.

I am currently using a single widget that takes in an object and displays its attributes/fields dynamically and therefore am unable to use UIBinder and the aforementioned internationalization technique.

我是否只需要更改我的设计并为我希望显示的每种类型的对象创建多个小部件,或者是否有我尚未找到的既定国际化技术??

Will I just need to change my design and create multiple widgets for each type of object I wish to display or is there an established internationalization technique I have yet to find??

推荐答案

您需要重新阅读标题为 动态字符串国际化.

You need to re-read the the Dev Guide from the section titled Dynamic String Internationalization.

这种方法意味着您需要为语言环境支持编写代码.我们已经使用 字典类.提供语言环境支持的技巧是为每个语言环境准备一个字典.

The approach means you need to code for locale support. We have done this using Dictionary class. The trick to provide locale support is to have a dictionary for each locale.

步骤 1-确保您使用带有 cookie 的 GWT module.gwt.xml 的语言环境概念.确保在加载 gwt 应用程序之前设置 cookie GWT_LOCALE.

Step 1- Ensure you use locale concepts of GWT module.gwt.xml with cookie. Ensure cookie GWT_LOCALE is set before gwt application loads.

<extend-property name="locale" values="en,ar,de" />
<set-property name="locale" value="en" />
<set-property-fallback name="locale" value="en" />
<set-configuration-property name="locale.cookie" value="GWT_LOCALE" />
<set-configuration-property name="locale.useragent" value="Y" />

步骤 2-使用 html 脚本标签预先加载 WidgetMessages.js 或使用 RequestBuilder 如果您希望按需延迟获取它.WidgetMessages.js 的内容

Step 2- Load WidgetMessages.js upfront using html script tags or Use RequestBuilder if you wish to fetch this lazily on demand. Contents of WidgetMessages.js

var widget_messages_en = {
    "today" : "Today",
    "now" : "Now"
};

var widget_messages_ar= {
    "today"  : "۷ڤدجچ",
    "now"  : "چڤت"
}

var widget_messages_de= { 
    "today"  : "Today",
    "now"  : "Now"
}

步骤 3- 处理字典并将其加载到本地哈希图中.

Step 3- Process and load the dictionaries into a local hashmap.

    private static Map<String, Dictionary> I18N_DICTIONARIES = new HashMap<String, Dictionary>();

    private static Dictionary createDictionary( String dictionaryName)
    {
            String moduleId = dictionaryName + "_messages_" + LocaleInfo.getCurrentLocale().getLocaleName();
            Dictionary dictionary = Dictionary.getDictionary( moduleId );
            I18N_DICTIONARIES.put( dictionaryName, dictionary );
            return dictionary;
    }

    public static String getI18NString(String dictionaryName, String stringToInternationalize )
    {
        Dictionary dictionary = I18N_DICTIONARIES.get( dictionaryName);
        if ( dictionary == null )
        {
            dictionary = createDictionary( dictionaryName);
        }
        String i18string = null;
        if ( dictionary == null )
            return stringToInternationalize;
        try
        {
            i18string = dictionary.get( stringToInternationalize );
        }
        catch ( Exception e )
        {
        }
        return i18string;
    }

注意 - 您可以尝试上述方法的几种变体来将字符串处理为 i18nstrings 并在小部件上使用它们....

Note - YOU CAN TRY Several variations of the above approach to process strings to i18nstrings and use them on widgets....

这篇关于动态生成内容的 GWT 国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆