java学习笔记—国际化(41)
國際化:internationalization即I18N。
舉例:
本科高校的網站,一般的都有中文和英文兩種頁面風格。因此將這種根據不同用戶群體顯示不同的頁面風格的方式稱之為頁面的國際化。
翻譯 VS 國際化
翻譯:Chrome
國際化:主要做的是頁面的顯示信息的國際化,至于文件等其他的資源是無法進行國際化。
1? 國際化的原理
Locale通過靜態屬性獲取區域對象。
全部使用properties配置文件進行實現。但是文件名需要按照指定的規則進行書寫。
規范: 基本名_語言名.properties
基本名是隨意的,語言名是固定可以參考瀏覽器的語言。
?? ResourceBundle 的靜態方法static ResourceBundle getBundle(String baseName,
Locale locale)
綁定資源的原理:獲取資源基本名和區域語言名鎖定資源名。
?? String getString(String key)
? 一? 體驗
1. 編寫兩個如下的資源文件
greet_zh.properties
hello=\u4F60\u597D
greet_en.properties
hello=Hello
2. 實現一個如下的java類
public static void main(String[] args) {// 獲取本地區域信息Locale locale = Locale.US;// 綁定資源ResourceBundle bundler = ResourceBundle.getBundle("cn.itcast.ps.greet", locale);System.out.println(bundler.getString("hello"));}總結:
如果沒有可視化編輯器那么直接使用native2ascii.exe進行中文的轉碼。
國際化變量
1 日期國際化
DateFormat該類主要提供的是以不同的區域顯示不同的日期格式。
根據區域信息格式化和解析日期數據。
static DateFormat getDateInstance(int style, Locale aLocale) à 獲取日期實例
static DateFormat getTimeInstance(int style, Locale aLocale) à 獲取時間實例
static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)à 獲取日期和時間
Date parse(String source) à 解析日期
String format(Object obj) à 格式化日期
舉例:
獲取日期和時間
Long 2013年1月21日 下午04時16分41秒 January 21, 2013 4:17:27 PM CST FULL 2013年1月21日 星期一 下午04時18分19秒 CST Monday, January 21, 2013 4:18:37 PM CST MEDIUM 2013-1-21 16:19:33 Jan 21, 2013 4:19:45 PM SHORT 13-1-21 下午4:20 1/21/13 4:20 PM練習:將以下的字符號串解析為日期對象。
String date = “2013年1月21日 下午4:20”;解析為日期對象。
?? Long?? Short
public static void main(String[] args) throws Exception{//I18NDate();String date = "2013年1月21日 下午4:20";Locale locale = Locale.CHINA;DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, locale);Date new_date = format.parse(date);System.out.println(new_date);}數字國際化
NumberFormat
static NumberFormat getInstance(Locale inLocale) ? 國際化普通數字 static NumberFormat getCurrencyInstance(Locale inLocale) ? 國際化貨幣 static NumberFormat getPercentInstance(Locale inLocale) ? 國際化百分比國際化
String format(Object obj) Number parse(String source)舉例:
public static void main(String[] args) throws Exception{// 獲取國際化普通數字的對象Locale locale = Locale.CHINA; NumberFormat format = NumberFormat.getInstance(locale);String str = format.format(123456.789);System.out.println(str);locale = Locale.CHINA; format = NumberFormat.getCurrencyInstance(locale);str = format.format(123456.789);System.out.println(str);locale = Locale.CHINA; format = NumberFormat.getPercentInstance(locale);str = format.format(0.789);System.out.println(str);}練習:將String str = "¥123.7";解析為數字。
public static void main(String[] args) throws Exception{//I18NNumber();String str = "¥123.7";Locale locale = Locale.CHINA; NumberFormat format = NumberFormat.getCurrencyInstance(locale);Number num = format.parse(str);System.out.println(num.doubleValue()*3);}動態文本國際化
public static void main(String[] args) throws Exception{String str = "On {0}, a hurricance destroyed {1} houses and caused {2} of damage.";String date = "Jul 3, 1998 12:30 PM"; DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.US);Date new_date = format.parse(date);Double money = new Double(1E7);NumberFormat numfor = NumberFormat.getCurrencyInstance(Locale.CHINA);String new_str = numfor.format(money);Object[] os = new Object[]{new_date,new Integer(77),new_str}MessageFormat msg_for = new MessageFormat(str,Locale.CHINA);String for_str = msg_for.format(os);System.out.println(for_str);}以上的動態文本國際化每次需要將所有需要國際化的數據單獨的進行國際化好之后逐一的進行填充。因此比較繁瑣,可以使用以下的模式字符串。
public static void main(String[] args) throws Exception{String pattern = "At {0, time, short} on {0, date}, a hurricance destroyed "+ "{1} houses and caused {2, number, currency} of damage.";String datetimeString = "Jul 3, 1998 12:30 PM";DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.US);Date new_date = format.parse(datetimeString);Object[] os = new Object[]{new_date,new Integer(77),new Double(1E7)};MessageFormat msg_for = new MessageFormat(pattern,Locale.US);String for_str = msg_for.format(os);System.out.println(for_str);}?
案例一:實現登陸的國際化
<body><%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="in" %><in:setLocale value="${pageContext.request.locale}"/><in:setBundle basename="cn.itcast.login.greet" var="bund" scope="page"/><form action=""><table align="center" border="1"><tr><td><in:message bundle="${pageScope.bund}" key="name"/></td><td><input type="text" name="uname"></td></tr><tr><td><in:message bundle="${pageScope.bund}" key="psw"/></td><td><input type="text" name="uname"></td></tr><tr><td colspan="2"><input type="submit" value='<in:message bundle="${pageScope.bund}" key="submit"/>'><input type="reset" value='<in:message bundle="${pageScope.bund}" key="reset"/>'></td></tr></table></form></body>后期在Struts2.x中需要使用struts的國際化標簽。
轉載于:https://www.cnblogs.com/zhenghongxin/p/4436625.html
總結
以上是生活随笔為你收集整理的java学习笔记—国际化(41)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle connect by用法
- 下一篇: vs2012 发布网站时出现 sgen.