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

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

问题描述

我有一个 GWT DateBox 实现:

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 年火星 14 号

2014 Mars 14

如果浏览器语言环境是英语

If Browser locale is English

2014 年 3 月 14 日

2014 March 14

等等.

先谢谢你!

推荐答案

试试这个

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

或者你可以这样做:

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

使用JSNI

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

French(fr) 区域设置的屏幕截图

在上面的代码中,日期按照语言环境进行格式化,但月份仍以英语显示,例如对于法国来说,三月不会被火星取代.

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.

在此处阅读有关设置区域设置的信息语言最初是动态的.

似乎有 5 种方法可以提供语言环境:

Seems like there are 5 ways to provide the locale:

  • 1.) 使用名为locale"的查询参数.要使用此方法,您可以让您的Web 服务器将重定向从 app.example.com 发送到app.example.com/?locale= 确定您网络上的区域设置后如果可能的话,或者您从应用程序内部进行重定向,例如在你的 onModuleLoad() 你使用 Window.Location.assign( + ).您可以通过设置一个来更改查询参数的名称与locale.queryparam"不同的值.

  • 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.) 使用 cookie.要使用它,您必须通过以下方式定义 cookie 名称将locale.cookie"设置为任何值,如 I18N.gwt.xml 没有默认 cookie名称已定义.

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.) 使用元标记.如前所述,您可以包含一个 gwt:property动态主机页面中的元标记.

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

4.) 使用用户代理信息.要使用它,您必须激活它通过将locale.useragent"设置为Y"作为默认禁用I18N.gwt.xml.

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.) 创建您自己的属性提供程序并使用 JavaScript 来填充locale"属性值你自己.在这里你完全免费如何获取值.

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 的默认搜索顺序是查询参数、cookie、元、用户代理"但是如果您不配置/激活它们,cookie 和用户代理将被跳过.您还可以通过设置locale.searchorder"来修改搜索顺序你的 gwt.xml.

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天全站免登陆