struts国際化、リソースファイル読み出し2
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ディレクトリの下にある2つのリソースファイル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);
}
}
ソースコードは、セッションにlocaleが設定されていない場合はrequestから読み出し、新しいLocaleを作成してセッションに保存することを示します.
では、ページに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オブジェクトが作成され、セッションに挿入されていることに注意してください.Globals.LOCALE_KEYはstrutsソースコードのセッションです.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" />
すぐ