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

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

问题描述

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



目前我正在使用一个单独的小部件,它接收一个对象并动态显示其属性/域,因此无法使用UIBinder和上述国际化技术。

我是否需要更改我的设计并为每个想要显示的对象类型创建多个窗口小部件,或者是否有已建立的国际化技术,我尚未找到?

解决方案

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



此方法意味着您需要编码区域设置支持。我们使用完成了此操作词典类。提供区域设置支持的技巧是为每个区域设置一个字典。



第1步 -
确保您使用区域设置概念带cookie的GWT module.gwt.xml。

 < extend-property name =localevalues =en,ar ,de/> 
< set-property name =localevalue =en/>
< set-configuration-property name =locale.cookievalue =GWT_LOCALE/>
< set-configuration-property name =locale.useragentvalue =Y/>

第2步 -
使用html加载WidgetMessages.js脚本标签或使用RequestBuilder,如果你想按需延迟获取。 WidgetMessages.js的内容

  var widget_messages_en = {
today:今日,
现在:现在
};

var widget_messages_ar = {
today:7ڤدجچ,
now:چڤ


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

第3步 - 处理字典并将其加载到本地散列表中。

  private static Map< String,Dictionary> I18N_DICTIONARIES = new HashMap< String,Dictionary>(); 
$ b private static Dictionary createDictionary(String dictionaryName)
{
String moduleId = dictionaryName +_messages_+ LocaleInfo.getCurrentLocale()。getLocaleName();
Dictionary dictionary = Dictionary.getDictionary(moduleId);
I18N_DICTIONARIES.put(dictionaryName,dictionary);
返回字典;


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

注意 - 您可以尝试上面的方法来处理字符串到i18nstrings并使用它们的小部件....


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).

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.

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" />

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"
}

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;
    }

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

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

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