struts2之国际化(i18n)

来源:互联网 发布:otg软件下载 编辑:程序博客网 时间:2024/05/14 08:22

(一)在struts.xml中配置<constant name="struts.custom.i18n.resources" value="bbs2009"></constant>其中Value=“bbs2009”表示资源文件的前缀。

统一管理资源文件:app_en_US.properties     app_zh_CN.properties

其中:中文要转换为UTF-8格式的ASCII编码才可以;

我们可以使用插件来自动转换:PropertiesEditor插件

使用方法:先下载解压,将其中的feature  plugin 文件覆盖中myeclipse中的 eclipse目录里即可

(二)import java.util.Locale;
import java.util.ResourceBundle;

public class Test {
public static void main(String[] args) {
ResourceBundle res = ResourceBundle.getBundle("app", Locale.CHINA);
System.out.println(res.getString("welcome.msg" ));
}
}

(三)app_en_US.properties文件中的内容如下:

login.username=username:
login.password=password:
login.login=login


(四)登陆界面:实现中英文的转换:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>管理员登录</title>
  </head>
  <body>
  <form action="admin/Login-login" method="post">
  <s:property value="getText('login.username')"/><input name="username" />
  <s:property value="getText('login.password')"/><input name="password" type="password" />
  <input type="submit" value="<s:property value="getText('login.login')"/>" /> 
  </form>
  <s:debug></s:debug>
  <a href="admin/lang?request_locale=en_US">en</a>
  <a href="admin/lang?request_locale=zh_CN">cn</a>

  
  </body>
</html>

其中:参数格式:request_locale=en_US  必须以这种形式传递

原创粉丝点击