國際化

圖片發(fā)自簡書App



圖片發(fā)自簡書App

/**

* ResourceBundle: 資源包類.

*

* 1. 在類路徑下需要有對應(yīng)的資源文件: baseName.properties. 其中 baseName 是基名.

* 2. 可以使用 基名_語言代碼_國家代碼.properties 來添加不同國家或地區(qū)的資源文件. i18n_zh_CN.properties

* 3. 要求所有基名相同的資源文件的 key 必須完全一致.

* 4. 可以使用 native2ascii 命令來得到 漢字 對一個的 asc 碼. Eclipse 內(nèi)置了工具

* 5. 可以調(diào)用 ResourceBundle 的 getBundle(基名, Locale 實例) 獲取獲取 ResourceBundle 對象

* 6. 可以調(diào)用 ResourceBundle 的 getString(key) 來獲取資源文件的 value 字符串的值.

* 7. 結(jié)合 DateFormat, NumberFormat, MessageFormat 即可實現(xiàn)國際化.

*

*/

@Test

public void testResourceBundle(){

Locale locale = Locale.CHINA;

ResourceBundle resourceBundle = ResourceBundle.getBundle("i18n", locale);

System.out.println(resourceBundle.getString("date"));

System.out.println(resourceBundle.getString("salary"));

String dateLabel = resourceBundle.getString("date");

String salLabel = resourceBundle.getString("salary");

String str = "{0}:{1}, {2}:{3}";

Date date = new Date();

double sal = 12345.12;

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);

String dateStr = dateFormat.format(date);

NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);

String salStr = numberFormat.format(sal);

String result = MessageFormat.format(str, dateLabel, dateStr, salLabel, salStr);

System.out.println(result);

}

/**

* MessageFormat: 可以格式化模式字符串

* 模式字符串: 帶占位符的字符串: "Date: {0}, Salary: {1}"

* 可以通過 format 方法會模式字符串進行格式化

*/

@Test

public void testMessageFormat(){

String str = "Date: {0}, Salary: {1}";

Locale locale = Locale.CHINA;

Date date = new Date();

double sal = 12345.12;

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);

String dateStr = dateFormat.format(date);

NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);

String salStr = numberFormat.format(sal);

String result = MessageFormat.format(str, dateStr, salStr);

System.out.println(result);

}

/**

* NumberFormat: 格式化數(shù)字到數(shù)字字符串, 或貨幣字符串的工具類

* 1. 通過工廠方法獲取 NumberFormat 對象

* NumberFormat.getNumberInstance(locale); //僅格式化為數(shù)字的字符串

* NumberFormat.getCurrencyInstance(locale); //格式為貨幣的字符串

*

* 2. 通過 format 方法來進行格式化

* 3. 通過 parse 方法把一個字符串解析為一個 Number 類型.

*/

@Test

public void testNumberFormat() throws ParseException{

double d = 123456789.123d;

Locale locale = Locale.FRANCE;

//

NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);

String str = numberFormat.format(d);

System.out.println(str);

NumberFormat numberFormat2 = NumberFormat.getCurrencyInstance(locale);

str = numberFormat2.format(d);

System.out.println(str);

str = "123?456?789,123";

d = (Double) numberFormat.parse(str);

System.out.println(d);

str = "123?456?789,12 €";

d = (Double) numberFormat2.parse(str);

System.out.println(d);

}

圖片發(fā)自簡書App

<%@page import="java.util.Locale"%><%@page import="java.util.Date"%><%@ page language="java" contentType="text/html; charset=UTF-8"? ? pageEncoding="UTF-8"%><%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>? ? <% Date date = new Date();request.setAttribute("date", date);request.setAttribute("salary", 12345.67);%><%--:,:

--%><% String code = request.getParameter("code");if(code != null){if("en".equals(code)){session.setAttribute("locale", Locale.US);}else if("zh".equals(code)){session.setAttribute("locale", Locale.CHINA);}}%>:,:

English中文

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,627評論 18 399
  • 參考文獻:《Java瘋狂講義》(第三版) 國際化與格式化 Java國際化的思路: Java程序國際化的主要通過如下...
    houc閱讀 262評論 0 0
  • java語言的國際化思路 在java語言中,國際化的思路是將程序中的標(biāo)簽,提示等信息放到資源文件中,這些資源文件是...
    junson閱讀 669評論 0 0
  • 畢竟期待了那么久嘛,想要去的地方都要做好標(biāo)記,而且第一次自駕游,一定要做好功課’ 6月29日12:35 倫敦蓋德威...
    考拉大魔王閱讀 616評論 2 49
  • 花開無聲,花謝有聲。 和人的一生正好相反, 人出生時總會伴著哇哇大哭的聲音, 但人在離開時,往往會悄然無聲。
    喜兒的小日子閱讀 446評論 0 5

友情鏈接更多精彩內(nèi)容