在gwt.xml中设置语言环境不起作用 [英] Setting locales in gwt.xml do not work

查看:82
本文介绍了在gwt.xml中设置语言环境不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < module rename-to =UIGenerator> 
<继承了name =com.google.gwt.http.HTTP/>
<继承了name =com.google.gwt.xml.XML/>
<继承了name =com.google.gwt.i18n.I18N/>
< inherits name =com.google.gwt.i18n.CldrLocales/>
< set-configuration-property name =locale.useragentvalue =Y/>
< extend-property name =localevalues =de/>
< extend-property name =localevalues =fr/>
< set-property name =localevalue =de,fr/>
< set-property-fallback name =localevalue =de/>
< entry-pointclass =mypackage.UIGeneratorEntryPoint/>
< / module>

但是每当我尝试使用语言环境时,它都会返回默认的英语。

  LocaleInfo.getAvailableLocaleNames();此返回数组的字符串只有一个值 - 默认

如果我设置了它,其他!



预先感谢您!

解决方案

运行应用程序,因为你的gwt.xml无效。

  [致命错误]:13:53:元素类型set-configuration-属性必须后跟任一属性规范,>或/>。 

value 之前放置一个空格,如下所示:

 < set-configuration-property name =locale.useragentvalue =Y/> 

我在同一个上下文中发布了一个答案。



请查看如何将语言环境设置为GWT DateBox


$ b

德语(de ) locale:



>


$ b 法语(fr) locale的截图:






完整代码
$ b 注意:只需更改 com.gwt .test.client.GWTTestProject 它与你实际的入口点类在你的gwt.xml文件中。


$ b $

gwt.xml

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE module PUBLIC - // Google Inc. //DTD Google Web Toolkit 2.5.0 // EN
http://google-web-toolkit.googlecode.com/svn /tags/2.5.0/distro-source/core/src/gwt-module.dtd\">
< module rename-to ='gwttestproject'>
<! - 继承核心Web Toolkit的东西。 - >
<继承了name ='com.google.gwt.user.User'/>
<继承了name =com.google.gwt.http.HTTP/>
<继承了name =com.google.gwt.xml.XML/>
< inherits name =com.google.gwt.i18n.CldrLocales/>

< inherits name ='com.google.gwt.user.theme.clean.Clean'/>
<继承了name =com.google.gwt.i18n.I18N/>

< set-configuration-property name =locale.searchorder
value =cookie,queryparam/>
< set-configuration-property name =locale.useragent
value =Y/>

< extend-property name =localevalues =de/>
< extend-property name =localevalues =fr/>
< set-property name =localevalue =de,fr/>
< set-property-fallback name =localevalue =de/>

<! - 指定应用程序入口点类。 - >
< entry-point class ='com.gwt.test.client.GWTTestProject'/>

<! - 指定可翻译代码的路径 - >
< source path ='client'/>
< source path ='shared'/>

< / module>

入门课程

  import java.util.Arrays; 
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DefaultDateTimeFormatInfo;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.i18n.client.impl.cldr.DateTimeFormatInfoImpl_de;
import com.google.gwt.i18n.client.impl.cldr.DateTimeFormatInfoImpl_en;
import com.google.gwt.i18n.client.impl.cldr.DateTimeFormatInfoImpl_es;
import com.google.gwt.i18n.client.impl.cldr.DateTimeFormatInfoImpl_fr;
import com.google.gwt.i18n.client.impl.cldr.DateTimeFormatInfoImpl_ru;
import com.google.gwt.i18n.client.impl.cldr.DateTimeFormatInfoImpl_zh;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.datepicker.client.DateBox;

public class GWTTestProject implements EntryPoint {

public static final native String getLanguage()/ * - {
return navigator.language;
} - * /;
$ b $ public void onModuleLoad(){
System.out.println(Arrays.toString(LocaleInfo.getAvailableLocaleNames()));

loacleDate();
}

public void loacleDate(){
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 key:formats.keySet()){
System.out.println(key + - + formats.get(key).dateFormat());


}

String language = getLanguage();

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


System.out.println(Date formatted:+ dateFormat.format(new Date()));

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

HTML

 <!doctype html> 
< html>
< head>
< meta http-equiv =content-typecontent =text / html; charset = UTF-8>
< link type =text / css =stylesheethref =GWTTestProject.css>
< title> Web Application Starter Project< / title>
< script type =text / javascriptlanguage =javascript
src =gwttestproject / gwttestproject.nocache.js>< / script>
< / head>
< body>

< / body>
< / html>


In my module xml file I have this:

 <module rename-to="UIGenerator">
 <inherits name="com.google.gwt.http.HTTP" />
 <inherits name="com.google.gwt.xml.XML" />
 <inherits name="com.google.gwt.i18n.I18N" />
 <inherits name="com.google.gwt.i18n.CldrLocales" />
 <set-configuration-property name="locale.useragent"value="Y" />
 <extend-property name="locale" values="de" />
 <extend-property name="locale" values="fr" />
 <set-property name="locale" value="de, fr" />
 <set-property-fallback name="locale" value="de" />
 <entry-pointclass="mypackage.UIGeneratorEntryPoint" />
 </module>

But every time when I try to use locales it returns me default one which is English.

   LocaleInfo.getAvailableLocaleNames(); this return Array of String with only one value   - default 

IF I set It works but only for German without others!

Thank you in advance!

解决方案

I got an error while running application because of invalid your gwt.xml.

[Fatal Error] :13:53: Element type "set-configuration-property" must be followed by either attribute specifications, ">" or "/>".

put a space before value as shown below:

<set-configuration-property name="locale.useragent" value="Y" /> 

I have posted a answer in the same context.

Please have a look at How do I set locale to GWT DateBox

Screenshot for German(de) locale:

Screenshot for French(fr) locale:


Complete Code:

Note: just change com.gwt.test.client.GWTTestProject it with you actual Entry Point clas in your gwt.xml file.

gwt.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.5.0//EN"
  "http://google-web-toolkit.googlecode.com/svn/tags/2.5.0/distro-source/core/src/gwt-module.dtd">
<module rename-to='gwttestproject'>
    <!-- Inherit the core Web Toolkit stuff. -->
    <inherits name='com.google.gwt.user.User' />
    <inherits name="com.google.gwt.http.HTTP" />
    <inherits name="com.google.gwt.xml.XML" />
    <inherits name="com.google.gwt.i18n.CldrLocales" />

    <inherits name='com.google.gwt.user.theme.clean.Clean' />
    <inherits name="com.google.gwt.i18n.I18N" />

    <set-configuration-property name="locale.searchorder"
        value="cookie,queryparam" />
    <set-configuration-property name="locale.useragent"
        value="Y" />

    <extend-property name="locale" values="de" />
    <extend-property name="locale" values="fr" />
    <set-property name="locale" value="de, fr" />
    <set-property-fallback name="locale" value="de" />

    <!-- Specify the app entry point class. -->
    <entry-point class='com.gwt.test.client.GWTTestProject' />

    <!-- Specify the paths for translatable code -->
    <source path='client' />
    <source path='shared' />

</module>

Entry Point class

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DefaultDateTimeFormatInfo;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.i18n.client.impl.cldr.DateTimeFormatInfoImpl_de;
import com.google.gwt.i18n.client.impl.cldr.DateTimeFormatInfoImpl_en;
import com.google.gwt.i18n.client.impl.cldr.DateTimeFormatInfoImpl_es;
import com.google.gwt.i18n.client.impl.cldr.DateTimeFormatInfoImpl_fr;
import com.google.gwt.i18n.client.impl.cldr.DateTimeFormatInfoImpl_ru;
import com.google.gwt.i18n.client.impl.cldr.DateTimeFormatInfoImpl_zh;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.datepicker.client.DateBox;

public class GWTTestProject implements EntryPoint {

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

public void onModuleLoad() {
    System.out.println(Arrays.toString(LocaleInfo.getAvailableLocaleNames()));

    loacleDate();
}

public void loacleDate() {
    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);

    for (String key : formats.keySet()) {
        System.out.println(key + " - " + formats.get(key).dateFormat());
    }

    String language = getLanguage();

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

    System.out.println("Date formatted:" + dateFormat.format(new Date()));

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

HTML

<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="GWTTestProject.css">
<title>Web Application Starter Project</title>
<script type="text/javascript" language="javascript"
    src="gwttestproject/gwttestproject.nocache.js"></script>
</head>
<body>

</body>
</html>

这篇关于在gwt.xml中设置语言环境不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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