struts国际化,资源文件读取二

package com.lwf.struts.util;

import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

public class Testl18n {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		//测试国际化:打开IE浏览器,语言>>可添加其它国家语言,并放在最上面。这样系统读到的就是这个语言了。
		//下面演示从系统获得语言设置
		Locale locale = Locale.getDefault();
		String country = locale.getCountry();
		String lang = locale.getLanguage();
		
		
		ResourceBundle bundle =ResourceBundle.getBundle("ApplicationResources", locale);
		String message = bundle.getString("user.pwd.null");
		System.out.println(message);
		
		//以下演示自定义语言设置
		Locale locale1 = new Locale("en", "us");
		ResourceBundle bundle1 =ResourceBundle.getBundle("ApplicationResources", locale1);
		message = bundle1.getString("user.pwd.null");
		System.out.println(message);
		
		//以下演示显示参数
		//MessageFormat用法一
		MessageFormat format = new MessageFormat(message);
		String megFormat = format.format(new Object[]{new Date()});
		System.out.println(megFormat);
		
		
		
		//MessageFormat用法二
		 int planet = 7;
		 String event = "a disturbance in the Force";
		 String result = MessageFormat.format(
		     "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
		     planet, new Date(), event);
		 System.out.println(result);

		
	}

}

 

在src目录下两个资源文件ApplicationResources_zh_CN.txt文件内容,将其转换成ApplicationResources_zh_CN.properties

user.pwd.null = 用户名不能为空,参数:{0}

ApplicationResources_en_US.properties内容为:

user.pwd.null = user or pwd must not null param :{0}

程序运行结果为:

用户名不能为空{0}
user or pwd must not null param :{0}
user or pwd must not null param :10-4-2 下午5:07
At 17:07:45 on 2010-4-2, there was a disturbance in the Force on planet 7.

 

 

我们来看Struts中源码是怎么处理国际化的:

protected void processLocale(HttpServletRequest request,
        HttpServletResponse response) {
        // Are we configured to select the Locale automatically?
        if (!moduleConfig.getControllerConfig().getLocale()) {
            return;
        }

        // Has a Locale already been selected?
        HttpSession session = request.getSession();

        if (session.getAttribute(Globals.LOCALE_KEY) != null) {
            return;
        }

        // Use the Locale returned by the servlet container (if any)
        Locale locale = request.getLocale();

        if (locale != null) {
            if (log.isDebugEnabled()) {
                log.debug(" Setting user locale '" + locale + "'");
            }

            session.setAttribute(Globals.LOCALE_KEY, locale);
        }
    }

 

源码显示如果session中没有设置locale则从request中读取,并创建新的Locale然后再存放到session中。

那么我们可以在页面上包含设置locale的地方,设置完后放入session中。

如下代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.util.Locale,org.apache.struts.*" %>
    <%@include file="share/jsp_head_include.jspf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
	Locale locale = new Locale("zh","cn");
	session.setAttribute(Globals.LOCALE_KEY,locale);
%>
<bean:message key="login.fail" /><br>

<%
%>
</body>
</html>

 

注意上面的页面中新建了Locale对象,并放入session中,Globals.LOCALE_KEY正是struts源码中的session.getAttribute的key值。

 

通常的做法是提供设置中文,英文等checkbox客户选择后,保存.我们就象上面JSP中做法一样,新建Locale并把它放入session中.

 

再看看资源文件路径的问题:

上面我们测试的时候是把资源文件放在src的根目录,如果现在把资源文件放在com.lwf.struts.util.property包下面。那么测试的代码应该

由:
ResourceBundle bundle =ResourceBundle.getBundle("ApplicationResources", locale);
改成:
ResourceBundle bundle =ResourceBundle.getBundle("com.lwf.struts.util.property.ApplicationResources", locale);
就可以了

 

而对于struts来说只需要配置文件修改一下就行:

由:
<message-resources parameter="ApplicationResources" />
改成:
<message-resources parameter="com.lwf.struts.util.property.ApplicationResources" />

 

即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值