java资源国际化

注:尽可能把显示的文字、图片、颜色等有可能会因为地区而变化的资源都放到资源文件中;

一、资源文件properties

messages.properties是默认资源文件,messages_zh_CH.properties是中文资源文件,messages_en_US.properties是英文资源文件(文件位于classpath下,其他路径加上相对应classpath路径前缀,例如com.company.messages);

二、界面使用

指定资源名称:

<fmt:setBundle basename="messages" var="msg"/>

显示key为hello的值:

<fmt:message bundle="${msg}" key="hello"/>

如果值带参数,则使用<fmt:param>,下标从0开始:hello=今天是{0},天气是{1}。

<fmt:message bundle="${msg}" key="hello">
    <fmt:param value=”星期一”/>
    <fmt:param value=”晴”/>
<fmt:message>    

常用数据:数字、百分比、货币、日期等国际化;

<jsp:useBean id="currentDate" class="java.util.Date" />
<fmt:formatDate value="${currentDate }" type="both"/>
<fmt:formatNumber value="10000.5"/>
<fmt:formatNumber value="10000.5" type="currency"/>
<fmt:formatNumber value="10000.5" type="percent"/>
//2016-7-29 15:22:06 10,000.5  ¥10,000.50    1,000,050%

三、java类中使用:ResourceBundle类

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
public class Messages {
    //资源名称
    private static final String BUNDLE_NAME="messages";
    
    //国际化资源
    public static String getString(String key,Locale locale) {
        try {
            return ResourceBundle.getBundle(BUNDLE_NAME,locale).getString(key);
        } catch (MissingResourceException e) {
            System.out.println(key);
            return '!' + key + '!';
        }
    }
    
    //带参数
    public static String getString(String key,Object...params){
        try {
            String value=RESOURCE_BUNDLE.getString(key);
            return MessageFormat.format(value, params);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    } 
    
    //根据请求语言国际化资源
    public static String getStringFromRequest(String key,HttpServletRequest request) {
        return Messages.getString(key,request.getLocale());
    }
}

调用:Messages.getStringFromRequest("hello",request);

注:eclipse中为java类提供Source|Externalize String命令,将类中所有字符串提取到properties中;

常用数据:数字、百分比、货币、日期等国际化;

String s1=DateFormat.getDateInstance().format(new Date());
String s2=DateFormat.getTimeInstance().format(new Date());    
String s3=DateFormat.getDateTimeInstance().format(new Date());
String s4=NumberFormat.getInstance().format(10000.2345);//数字
String s5=NumberFormat.getCurrencyInstance().format(10000.2345);//货币
String s6=NumberFormat.getPercentInstance().format(10000.3d);//百分比
//2016-7-29 15:32:26 2016-7-29 15:32:26 10,000.234 ¥10,000.23 1,000,030%

四、任意字符转ASCII字符:JDK安装目录bin下native2asii.exe工具

(1)双击运行,输入中文,回车;

(2)转换整个properties文件:

native2ascii -[options] [inputfile [outputfile]];

-[options]包括-encoding enconding_name(按照指定编码方式转换,-encoding参数可省略,若指定,注意与源文件编码方式一致)与-reverse(将编码后的文件还原);

[inputfile]是将要转换的源文件路径;

[outputfile]是转换后的文件输出路径,若缺省,则输出到控制台屏幕;

注:若native2ascii命令无法使用,将JDK的bin目录加到系统环境变量path中即可;

五、实例,显示全球时间:TimeZone

<%
Map<String,TimeZone> hashMap=new HashMap<String,TimeZone>();
for(String id:TimeZone.getAvailableIDs()){
    hashMap.put(id, TimeZone.getTimeZone(id));
}
request.setAttribute("timeZoneIds", TimeZone.getAvailableIDs());
request.setAttribute("hashMap", hashMap);
%>
现在时刻:<%TimeZone.getDefault().getDisplayName(); %>
<fmt:formatDate value="${currentDate }" type="both"/><br/>
<table>
    <tr>
        <td>时区</td>
        <td>时区中文</td>
        <td>现在时间</td>
        <td>时差</td>
   </tr>
   <c:forEach var="id" items="${timeZoneIds }" varStatus="status">
   <tr>
        <td>${id}</td>
        <td>${hashMap[id].displayName }</td>
        <td>
            <fmt:timeZone value="${id }">
            <fmt:formatDate value="${currentDate }" type="both" timeZone="${id }"/>
            </fmt:timeZone>
        </td>
        <td>${hashMap[id].rawOffset/60/60/1000 }</td>
    </tr>
    </c:forEach>
</table>                   

 

posted @ 2016-07-29 15:50  Black_Elf  阅读(276)  评论(0编辑  收藏  举报