java web 國際化

來源:互聯網
上載者:User

標籤:locale   dateformat   resourcebundle   numberformate   messageformat   

一、Locale類

//Locale locale = Locale.CHINA;Locale locale = new Locale("zh", "CN");System.out.println(locale.getLanguage());System.out.println(locale.getDisplayCountry());System.out.println(locale.getDisplayLanguage());

二、DateFormat類

Date date = new Date();System.out.println(date);Locale locale = new Locale("zh", "cn");DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);System.out.println(dateFormat.format(date));String dates = "2017-3-17";DateFormat df = new SimpleDateFormat("yyyy-MM-dd");System.out.println(df.parse(dates));

三、NumberFormat類

double d = 1.12d;Locale locale = Locale.CHINA;//格式化為貨幣NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);//格式化為數字NumberFormat numberFormat2 = NumberFormat.getNumberInstance(locale);//格式化為百分比NumberFormat numberFormat3 = NumberFormat.getPercentInstance(locale);System.out.println(numberFormat.format(d));System.out.println(numberFormat2.format(d));System.out.println(numberFormat3.format(d));

四、MessageFormat類

String str = "{0},{1}";String strs = MessageFormat.format(str, "userName","admin");System.out.println(strs);

五、ResourceBundle類

ResourceBundle bundle = ResourceBundle.getBundle("i18n",new Locale("en","US"));System.out.println(bundle.getString("userName"));

六、國際化資源檔

i18n.properties userName=userName

i18n_zh_CN.properties userName=\u7528\u6237\u540D

i18n_en_US.properties userName=userName


註:i18n是基名,zh是語言代碼,CN是國家代碼


七、JSP頁面的fmt標籤

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <%Locale locale = Locale.US;//Locale locale = Locale.CHINA;request.setAttribute("locale", locale);System.out.println(request.getLocale());//列印出所有應用中配置的資源國際檔案Enumeration<Locale> enumeration = request.getLocales();while(enumeration.hasMoreElements()){Locale locale2 = enumeration.nextElement();System.out.println(locale2);}%><fmt:setLocale value="${requestScope.locale}"/>${requestScope.locale}<fmt:setBundle basename="i18n"/><fmt:message key="userName"></fmt:message><fmt:formatNumber type="currency" value="123"></fmt:formatNumber>


本文出自 “11137669” 部落格,請務必保留此出處http://11147669.blog.51cto.com/11137669/1907741

java web 國際化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.