如何将语言环境设置为GWT DateBox [英] How do I set locale to GWT DateBox

查看:106
本文介绍了如何将语言环境设置为GWT DateBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GWT DateBox实现:

  DateTimeFormat dateFormat = DateTimeFormat.getLongDateTimeFormat(); 
dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));

我想为日期设置不同的区域设置。例如
如果浏览器语言是法国,日期应该是:

lockquote
2014 Mars 14

如果浏览器区域设置为英语


2014年3月14日


等等。



预先感谢! <请尝试这一个

  DateTimeFormat dateFormat = DateTimeFormat.getFormat( 。LocaleInfo.getCurrentLocale()getDateTimeFormatInfo()dateFormatLong()); 

或者您可以这样做:

 映射< String,DefaultDateTimeFormatInfo> formats = new HashMap< String,DefaultDateTimeFormatInfo>(); 

DefaultDateTimeFormatInfo formatDE = new DateTimeFormatInfoImpl_de();
DefaultDateTimeFormatInfo formatEN = new DateTimeFormatInfoImpl_en();
DefaultDateTimeFormatInfo formatFR = new DateTimeFormatInfoImpl_fr();
DefaultDateTimeFormatInfo formatES = new DateTimeFormatInfoImpl_es();
DefaultDateTimeFormatInfo formatZH = new DateTimeFormatInfoImpl_zh();
DefaultDateTimeFormatInfo formatRU = new DateTimeFormatInfoImpl_ru();

formats.put(de,formatDE);
formats.put(en,formatEN);
formats.put(fr,formatFR);
formats.put(es,formatES);
formats.put(zh,formatZH);
formats.put(ru,formatRU);

String language = getLanguage();

DefaultDateTimeFormatInfo format = formats.get(language);
DateTimeFormat dateFormat = null;
if(format == null){
dateFormat = DateTimeFormat.getFormat(LocaleInfo.getCurrentLocale()
.getDateTimeFormatInfo()。dateFormatLong());
} else {
dateFormat = DateTimeFormat.getFormat(format.dateFormatFull());
}

System.out.println(dateFormat.format(new Date()));

DateBox dateBox = new DateBox();
dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));
RootPanel.get()。add(dateBox);

使用JSNI

  public static final String getLanguage()/ *  -  {
return navigator.language;
} - * /;

法语(fr) locale








在上面的代码中,日期格式为每个语言环境,但仍然以英语显示月份,例如:法国不会用火星取代3月。



为了解决这个问题,我们必须定义locale。



在这里阅读最初动态设置区域设置语言



好像有5种方法可以提供语言环境:使用 b
$ b


  • 一个名为locale的查询参数。要使用此方法,可以在确定Web
    服务器上的语言环境后,让
    web服务器将app.example.com的重定向发送到
    app.example.com/?locale= if可能或者你从你的应用程序中重定向,例如在
    你的onModuleLoad()你使用Window.Location.assign(+)。您可以通过使用cookie将
    不同的值设置为locale.queryparam。


  • 2。)来更改查询参数的名称。要使用它,必须将
    的cookie设置为locale.cookie,并将其定义为任何值,如I18N.gwt.xml中所示。不定义默认cookie
    名称。


  • 3。)使用元标记。如前所述,您可以在动态主机页面中包含gwt:property
    元标记。


  • 4)使用用户代理信息。要使用此功能,您必须通过将locale.useragent设置为Y作为其在
    I18N.gwt.xml中的默认禁用来激活它


  • 。创建你自己的属性提供者并使用JavaScript来自己填充
    locale属性值。在这里你完全免费如何
    获得价值。


    GWT的默认搜索顺序是query param ,cookie,meta,useragent,但如果您不配置/激活它们,则会跳过
    cookie和useragent。
    您还可以通过在
    your gwt.xml中设置locale.searchorder来修改搜索顺序。



    现在选择一个解决方案...

    I have a GWT DateBox implementation:

    DateTimeFormat dateFormat = DateTimeFormat.getLongDateTimeFormat();
    dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));
    

    I would like to set different locales for the date. For example If Browser language is France the date should be:

    2014 Mars 14

    If Browser locale is English

    2014 March 14

    and so on.

    Thank you in advance!

    解决方案

    Try this one

    DateTimeFormat dateFormat = DateTimeFormat.getFormat(LocaleInfo.getCurrentLocale().getDateTimeFormatInfo().dateFormatLong());
    

    Or you can do in this way:

        Map<String, DefaultDateTimeFormatInfo> formats = new HashMap<String, DefaultDateTimeFormatInfo>();
    
        DefaultDateTimeFormatInfo formatDE = new DateTimeFormatInfoImpl_de();
        DefaultDateTimeFormatInfo formatEN = new DateTimeFormatInfoImpl_en();
        DefaultDateTimeFormatInfo formatFR = new DateTimeFormatInfoImpl_fr();
        DefaultDateTimeFormatInfo formatES = new DateTimeFormatInfoImpl_es();
        DefaultDateTimeFormatInfo formatZH = new DateTimeFormatInfoImpl_zh();
        DefaultDateTimeFormatInfo formatRU = new DateTimeFormatInfoImpl_ru();
    
        formats.put("de", formatDE);
        formats.put("en", formatEN);
        formats.put("fr", formatFR);
        formats.put("es", formatES);
        formats.put("zh", formatZH);
        formats.put("ru", formatRU);
    
        String language = getLanguage();
    
        DefaultDateTimeFormatInfo format = formats.get(language);
        DateTimeFormat dateFormat = null;
        if (format == null) {
            dateFormat = DateTimeFormat.getFormat(LocaleInfo.getCurrentLocale()
                    .getDateTimeFormatInfo().dateFormatLong());
        } else {
            dateFormat = DateTimeFormat.getFormat(format.dateFormatFull());
        }
    
        System.out.println(dateFormat.format(new Date()));
    
        DateBox dateBox = new DateBox();
        dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));
        RootPanel.get().add(dateBox);
    

    using JSNI

    public static final native String getLanguage() /*-{
        return navigator.language;
    }-*/;
    

    Screenshot for French(fr) locale


    In above code date is formatted as per locale but still month is displayed in English language for e.g. March is not replaced with Mars for France.

    To solve this problem we have to define locale.

    Read here about setting locale language dynamically initially.

    Seems like there are 5 ways to provide the locale:

    • 1.) using a query param named "locale". To use this method you can let your web server send a redirect from app.example.com to app.example.com/?locale= after determining the locale on your web server if possible or you do the redirect from within your app, e.g. in your onModuleLoad() you use Window.Location.assign( + ). You can change the name of the query param by setting a different value to "locale.queryparam".

    • 2.) using a cookie. To use this you have to define the cookie name by setting "locale.cookie" to any value as in I18N.gwt.xml no default cookie name is defined.

    • 3.) using meta tags. As already described you can include a gwt:property meta tag in a dynamic host page.

    • 4.) using the user agent information. To use this you have to activate it by setting "locale.useragent" to "Y" as its disabled by default in I18N.gwt.xml.

    • 5.) create your own property provider and use JavaScript to fill the "locale" property value yourself. Here you are completely free how to obtain the value.

    GWT's default search order is "query param, cookie, meta, useragent" but cookie and useragent will be skipped if you don't configure/activate them. You could also modify the search order by setting "locale.searchorder" in your gwt.xml.

    Now choose one solution ...

    这篇关于如何将语言环境设置为GWT DateBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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