j2ee國際化數據方式
阿新 • • 發佈:2017-05-25
imei 加載 sta 技術分享 etc 位數 路徑 mage new
靜態國際化數據,在msg(定義數據名)_zh(語言縮寫)_CN(國家縮寫).propertices文件中自定義相關數據
然後進行調用
/* * 靜態國際化 */ @Test public void testInternational_static() throws Exception { //當前語言環境 // Locale locale = Locale.CHINA; Locale locale = Locale.US; //創建工具類對象ResourceBundle ResourceBundle resourceBundle = ResourceBundle.getBundle("com.xinzhi.test.msg" , locale); String string= resourceBundle.getString("hello"); System.out.println(string); }
下面是其他類型數據的國際化方法
/* * 數字國際化 */ @Test public void testInternational_number() throws Exception { // Locale locale = Locale.CHINA; Locale locale = Locale.US; NumberFormat numberFormat= NumberFormat.getNumberInstance(locale); String format = numberFormat.format(1000000000); System.out.println(format); } /* * 貨幣數字國際化 */ @Test public void testInternational_currency() throws Exception { // Locale locale = Locale.CHINA; Locale locale = Locale.US; NumberFormat numberFormat= NumberFormat.getCurrencyInstance(locale); String format = numberFormat.format(100); System.out.println(format); } /* * 貨幣數字計算$100*10 */ @Test public void testInternational_currency2() throws Exception { // Locale locale = Locale.CHINA; Locale locale = Locale.US; String currencyString = "$100"; int num = 10; NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale); Number parse = numberFormat.parse(currencyString); System.out.println(parse.intValue() * num); } /* * 時間國際化 */ @Test public void testInternational_time() throws Exception { // Locale locale = Locale.CHINA; Locale locale = Locale.US; int dateStyle = DateFormat.FULL; int timeStyle = DateFormat.SHORT; DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale); String format = dateTimeInstance.format(new Date()); System.out.println(format); }
在jsp中使用國際化:
將資源文件在msg(定義數據名)_zh(語言縮寫)_CN(國家縮寫).propertices處理完成後
在需要國際化的jsp界面中插入如下代碼:
<[email protected] uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>//用於加載jstl <fmt:setLocale value="${pageContext.request.locale }"/>//設置使用的語言場景(在請求域中獲取) <fmt:setBundle basename="com.xinzhi.utils.msg" var="varbundle"/>//拿到資源文件路徑,定義資源變量 <fmt:message key="title" bundle="${varbundle}"></fmt:message>//根據鍵來獲取相應的值
/**
* jsp中數字國際化,保留兩位有效數字,沒有的用0填充,用#.##則表示保留兩位數字但是不保留0
*/
<fmt:formatNumber pattern="0.00" value="100"></fmt:formatNumber>
/**
* jsp中日期國際化
*/
<fmt:formatDate value="2017-05-13" pattern="yyyy-MM-dd"/>
j2ee國際化數據方式