struts2》学习笔记——i18n国际化

 

         随着全球经济的一体化,软件开发者应该开发出支持多国语言、国际化的Web应用。对于Web应用来说,同样的页面在不同的语言环境下需要显示不同的效果。也就是说,一个Web应用程序在运行时能够根据客户端请求所来自的国家和语言显示不同的用户界面。这样 ,当需要在应用程序中添加对一种新的语言的支持时,无需修改应用程序的代码。

 

         eclipse上进行国际化开发建议导入

资源文本编辑器 com.essiembre.eclipse.i18n.resourcebundle_0.7.7 插件

 

实施步骤

1,  在工程内建立 resources 包。

2,  首先建立默认资源文件,命名要求:ApplicationResources.properties

3,  resources 包下 new – Other

选择通过资源文编辑器插件的资源文件

 

 

4,浏览至 resources 选择需要的国际化语言以及地区

如下图:

5,  右键资源文件 – OpenWith—选择资源文编辑器插件,

    以如图键值对形式输入不同语言的值。

 

6,  设定默认资源文件:

 

1 <?xml version="1.0" encoding="UTF-8"?>

2 <!DOCTYPE struts PUBLIC

3       "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

4       "http://struts.apache.org/dtds/struts-2.0.dtd">

5 <struts>

6     <!-- 指定国际化的base -->

7   <constant name="struts.custom.i18n.resources"           value="com.clprojeck.resources.ApplicationResources" />

8     <!-- 指定国际化编码使用的字符集 -->

9        <constant name="struts.il8n.encoding" value="utf-8"/>

10  </struts>

 

 

7,  jsp接受资源文件的值以下两种皆可:

<s:text name="commodity.id" />

<s:i18n name="commodity.id" />

 

 

 

8, 建立一个LangAction内容:

1    public class LangAction extends ActionSupport {

2   

 

3    //接受语言编号

4    private Integer id;

5   

 

6    @Override

7    public String execute() {

8        

9         try {

10           switch (id) {

11           case 1:

12               //中文——简体

13               ActionContext.getContext().setLocale(Locale.SIMPLIFIED_CHINESE);

14               ActionContext.getContext().getSession()

15                       .put("WW_TRANS_I18N_LOCALE","zh_CN");

16               break;

17           case 2:

18               //英文——美式

19               ActionContext.getContext().setLocale(Locale.US);

20               ActionContext.getContext().getSession()

21                       .put("WW_TRANS_I18N_LOCALE", Locale.US);

22               break;

23 

 

24           default:

25               //中文——简体

26               ActionContext.getContext().setLocale(Locale.SIMPLIFIED_CHINESE);

27               ActionContext.getContext().getSession()

28                       .put("WW_TRANS_I18N_LOCALE", Locale.SIMPLIFIED_CHINESE);

29               break;

30           }

31 

 

32           return SUCCESS;

33       } catch (Exception e) {

34           return ERROR;

35       }

36  }

37 

38 

39  public Integer getId() {

40       return id;

41  }

42 

 

43  public void setId(Integer id) {

44       this.id = id;

45  }

46  }

 

9,在主页 我们只需要将代表语言类型的id传至LangAction