@author zhong
思路:
1.讀取配置文件(包括本地文選項、哪些資源文件)
2.根據配置文件讀取對應本地語言所有的國際化資源文件
3.將所有國際化資源文件存放到map中
4.提供對外調用的方法
首先創建一個名爲:i18n.properties 的國際化基本配置類,內容如下:
i18nLocale=en_US
i18nFiles=pageMessage,XXXX,XXXXX,XXX
說明:這裏配置文件根據項目需求進行配置,基本i18nFiles可以配置多個國際化配置文件的前綴。
如這兩個配置文件,它們的前綴是pageMessage:
pageMessage_en_US.properties、pageMessage_zh_CN.properties
創建一個工具類:PageMessageUtil.java,示例代碼如下:
/**
*
*/
package com.yz.util;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
/**
* @author zhong
* 思路:
* 1.讀取配置文件(包括本地文選項、哪些資源文件)
* 2.根據配置文件讀取對應本地語言所有的國際化資源文件
* 3.將所有國際化資源文件存放到map中
* 4.提供
*
*/
public class PageMessageUtil {
//國際化配置文件的名稱
private static final String CONFIG_FILE_NAME = "i18n.properties";
//國際化配置文件裏面的的key、value
private static final String RESOURCE_FILES = "i18nFiles";
//國際化配置文件裏當前語言
private static final String LOCALE_CODE = "i18nLocale";
private static Map<String, String> i18nMap = null;
//加載這個類的時候就執行該靜態方法
static{
load();
}
private static void load() {
//讀取配置文件
Properties property = new Properties();
try {
property.load(PageMessageUtil.class.getClassLoader()//
.getResourceAsStream(CONFIG_FILE_NAME));
//獲取所有國際化資源的前綴,如:pageMessage、後綴:zh_CN
String resourceFile = property.getProperty(RESOURCE_FILES).toString();
System.out.println(resourceFile);
String locale = property.getProperty(LOCALE_CODE).toString();
System.out.println(locale);
//將資源文件中的配置轉化成map
String[] resources = resourceFile.split(",");
i18nMap = new HashMap<String, String>();
ResourceBundle bundle = null;
for (int i = 0; i < resources.length; i++) {
//struts2中用於國際化的類,這裏是初始化這個類的
try {
ResourceBundle.clearCache();
//獲取到某個資源文件裏面的所有內容
bundle = ResourceBundle.getBundle(resources[i]+"_"+locale);
@SuppressWarnings("rawtypes")
Set set = bundle != null ? bundle.keySet() : null;
@SuppressWarnings("unchecked")
Iterator<String> iterator = (set != null) ? set.iterator() : null;
while (iterator != null && iterator.hasNext()) {
String key = iterator.next();
i18nMap.put(key, bundle.getString(key));
}
System.out.println(i18nMap.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static String get(String key) {
if(i18nMap.containsKey(key)){
return i18nMap.get(key);
}else{
return key;
}
}
public static Set<String> getKeys() {
Set<String> set = null;
if(i18nMap != null && i18nMap.size() > 0){
set = new HashSet<String>();
for(Map.Entry<String, String> entry:i18nMap.entrySet()){
set.add(entry.getKey());
}
return set;
}else{
return set;
}
}
public static void main(String[] args) {
System.out.println(getKeys().toString());
}
}
JSP頁面調用示例:
<button type="submit"><%=PageMessageUtil.get("login_page_loginBtnText")%></button>
在JS中調用示例:
<script type="text/javascript">
//將工具類的map轉換成js對象
var pageMessage = {
<%
Set<String> keySet = PageMessageUtil.getKeys();
Iterator<String> iterator = keySet != null ? keySet.iterator() : null;
while(iterator != null && iterator.hasNext()){
String key = iterator.next();
String value = PageMessageUtil.get(key);
%>
<%= key %>: "<%= value %>" ,
<%
}
%>
i18n : function(key){
if(typeof(pageMessage[key]) != 'undefined'){
return pageMessage[key];
}else{
return key;
}
}
};
</script>