首页 > 代码库 > java web 国际化
java web 国际化
一、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="http://www.mamicode.com/${requestScope.locale}"/> ${requestScope.locale} <fmt:setBundle basename="i18n"/> <fmt:message key="userName"></fmt:message> <fmt:formatNumber type="currency" value="http://www.mamicode.com/123"></fmt:formatNumber>
本文出自 “11137669” 博客,请务必保留此出处http://11147669.blog.51cto.com/11137669/1907741
java web 国际化
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。