我有一个 GWT DateBox 实现:
DateTimeFormat dateFormat = DateTimeFormat.getLongDateTimeFormat();
dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));
Run Code Online (Sandbox Code Playgroud)
我想为日期设置不同的语言环境。例如,如果浏览器语言是法国,则日期应为:
2014 年 14 月
如果浏览器区域设置为英语
2014 年 3 月 14 日
等等。
先感谢您!
试试这个
DateTimeFormat dateFormat = DateTimeFormat.getFormat(LocaleInfo.getCurrentLocale().getDateTimeFormatInfo().dateFormatLong());
Run Code Online (Sandbox Code Playgroud)
或者你可以这样做:
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);
Run Code Online (Sandbox Code Playgroud)
使用JSNI
public static final native String getLanguage() /*-{
return navigator.language;
}-*/;
Run Code Online (Sandbox Code Playgroud)
French(fr)区域设置的屏幕截图

在上面的代码中,日期按照语言环境进行格式化,但仍然以英语显示月份,例如,对于法国,三月不会替换为火星。
为了解决这个问题,我们必须定义语言环境。
在此处阅读有关最初动态设置区域设置语言的信息。
似乎有 5 种方法可以提供语言环境:
1.) 使用名为“locale”的查询参数。要使用此方法,您可以让您的 Web 服务器在确定您的 Web 服务器上的区域设置后(如果可能)发送从 app.example.com 到 app.example.com/?locale= 的重定向,或者您从您的应用程序内进行重定向,例如在您的 onModuleLoad() 中,您使用 Window.Location.assign( + )。您可以通过为“locale.queryparam”设置不同的值来更改查询参数的名称。
2.) 使用cookie。要使用它,您必须通过将“locale.cookie”设置为任何值来定义 cookie 名称,如 I18N.gwt.xml 中未定义默认 cookie 名称。
3.) 使用元标记。如前所述,您可以在动态主机页面中包含 gwt:property 元标记。
4.) 使用用户代理信息。要使用它,您必须通过将“locale.useragent”设置为“Y”来激活它,因为它在 I18N.gwt.xml 中默认禁用。
5.) 创建您自己的属性提供程序并使用 JavaScript 自己填充“locale”属性值。在这里,您可以完全自由地获取价值。
GWT 的默认搜索顺序是“查询参数、cookie、元数据、用户代理”,但如果您不配置/激活它们,则会跳过 cookie 和用户代理。您还可以通过在 gwt.xml 中设置“locale.searchorder”来修改搜索顺序。
现在选择一种解决方案......
| 归档时间: |
|
| 查看次数: |
1998 次 |
| 最近记录: |