GWT学习笔记(四)

来源:互联网 发布:梦龙网络计划软件 编辑:程序博客网 时间:2024/05/16 08:16

    嗯,这两周都在做GWT1.4的开发,用GWT中的国际化支持部份的比较多。所以这次想讲讲GWT中的车际化支持部分。其实,我也觉得我那组长说得没错,说国际化支持,应该有两个部分的,一部份就是界面提示,能够用多种语言来显示,另外一个部分是你自己本身的程序中的数据,要支持多种语言来显示。当然GWT做的国际化支持,只是帮你做了前台部分的多种语言支持的方案,程序本身的数据国际化还是要自己想办法做的。不说这么多了,下面来说说GWT的做法。

    GWT中有一个I18N的包,这个包就是用来做国际经支持的。 下面以GWT中自带的HELLO为例子,让其显示,“HELLO!”和“您好!”

  • 1.在GWT的目录下运行:i18nCreator -out samples/Hello -overwrite com.google.gwt.sample.hello.client.Constants

注:关于i18nCreator 的用法,可以参考GWT的使用说明。

这时,在HELLO的目录下会多出一个文件Constants-i18n.bat,在src/com/google/gwt/sample/hello/client下会多一个Constants.properties

  • 2.打开Constants.properties,删除里面所有的内容,写上
hello = hello

跟properties的用法一样的。

然后建立一个Constants_zh_CN.properties,写上

hello = 您好!

注:Constants.properties与Constants_zh_CN.properties是对应关系的,实际上Constants_zh_CN.properties只上把Constants.properties里的值改为中文属性名不变。

  • 3.运行Constants-i18n,这时,src/com/google/gwt/sample/hello/client目录下会多一个Constants.java,这样,你就做好一个国际化支持的接口了。内容如下:
package com.google.gwt.sample.hello.client;


/**
 * Interface to represent the constants contained in resource  bundle:
 *     'D:/Hello/src/com/google/gwt/sample/hello/client/Constants.properties'.
 
*/

public interface Constants extends com.google.gwt.i18n.client.Constants {
  
  
/**
   * Translated "hello".
   * 
   * 
@return translated "hello"
   * @gwt.key hello
   
*/

  String hello();
}

  • 4.修改Hello.java第33行,
Window.alert("Hello, AJAX");

Constants constants = (Constants) com.google.gwt.core.client.GWT.create(Constants.class);
Window.alert(constants.hello());

 在src/com/google/gwt/sample/i18n目录下I18N.gwt.xml文件,增加

<extend-property name="locale" values="zh_CN" />

修改后文件为:

<module>
    
<inherits name="com.google.gwt.user.User"/>
    
<entry-point class="com.google.gwt.sample.hello.client.Hello"/>
    
    
<extend-property name="locale" values="zh_CN" />
</module>
  • 5.运行Hello-compile,然后运行www/com.google.gwt.sample.hello.Hello这个目录下的Hello.html,点[Click me]是“hello”

在你查看IE的地址中补:?locale=zh_CN,点[Click me]是“您好”


 GWT有以下几种不同的国际化支持类,可以查看文档:

  • Constants
    Useful for localizing typed constant values
  • Messages
    Useful for localizing messages requiring arguments
  • ConstantsWithLookup
    Like Constants but with extra lookup flexibility for highly data-driven applications
  • Dictionary
    Useful when adding a GWT module to existing localized web pages
  • Localizable Useful for localizing algorithms encapsulated in a class