java - 如何将区域设置设置为 GWT DateBox

标签 java gwt

我有一个 GWT DateBox 实现:

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

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

2014 Mars 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;
}-*/;

法语(fr) 语言环境的屏幕截图

enter image description here

<小时/>

在上面的代码中,日期按照区域设置进行格式化,但月份仍然以英语显示,例如对于法国来说,三月并没有被火星取代。

为了解决这个问题,我们必须定义语言环境。

在此处阅读有关 setting locale language dynamically initially 的信息.

似乎有 5 种提供区域设置的方法:

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

  • 2.) 使用 cookie。要使用它,您必须通过以下方式定义 cookie 名称 将“locale.cookie”设置为 I18N.gwt.xml 中的任意值 无默认 cookie 名称已定义。

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

  • 4.) 使用用户代理信息。要使用它,您必须激活它 通过将“locale.useragent”设置为“Y”,默认情况下禁用 I18N.gwt.xml。

  • 5.) 创建您自己的属性提供程序并使用 JavaScript 来填充 “locale”属性值你自己。在这里您可以完全自由地了解如何 获取值。

GWT 的默认搜索顺序是“query param、cookie、meta、useragent”,但是 如果您不配置/激活 cookie 和 useragent 将被跳过。 您还可以通过设置“locale.searchorder”来修改搜索顺序 您的 gwt.xml。

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

关于java - 如何将区域设置设置为 GWT DateBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22408685/

相关文章:

linux - 为什么从同一个 RPM 安装多个 Oracle JDK 二进制文件在不同的机器上不同?

gwt - 使 gwt celltable 行被选中

javascript - 从 iFrame 禁用 javascript 警报

css - 在 GWT 中拆分图像会导致不需要的空白

java - 为什么添加 throws InterruptedException 会为 Runnable 的实现创建编译错误

java - 转换对象的模式

java - Angular 6 Spring Boot POST 问题

java - 无法向覆盖的方法添加 throws 子句导致我必须返回 null

java - GWT。删除 anchor 部分 url

java - GWT 错误 : java. lang.NoClassDefFoundError:com/google/gwt/core/client/GWTBridge