国际化
2016-05-03 14:14
253 查看
国际化又简称为 i18n:internationalization 国际化的人: 人,英语,汉语; 可以说这个人是国际化的人; 软件的国际化: 软件 中国: 显示中文,以及服务符合中国习惯的文本字符串! 1999-09-09 美国: 显示英文,以及服务符合他国习惯的文本字符串! 这种软件,就叫国际化的软件! 如何做到国际化的软件,要求: 1. 软件中存储特定的字符串 2. 知道浏览器当前使用哪种语言(Locale)
Locale 本地化
Java提供了一个本地化的对象!封装当前语言、国家、环境等特征!
public class App { @Test //1. 本地化对象:Locale // 封装语言、国家信息的对象,有java.util提供 public void testLocale() throws Exception { // 模拟中国语言等环境 //Locale locale = Locale.CHINA; Locale locale = Locale.getDefault(); // 当前系统默认的语言环境 System.out.println(locale.getCountry()); // CN 国家的简称 System.out.println(locale.getDisplayCountry()); // 国家名称 System.out.println(locale.getLanguage()); // zh 语言简称 // 模拟美国国家 Locale l_us = Locale.US; System.out.println(l_us.getCountry()); System.out.println(l_us.getDisplayCountry()); } }
国际化
静态数据国际化
网站中显示的固定文本的国际化: “用户名”“密码“ 国际化的软件: 1. 存储所有国家显示的文本的字符串 a) 文件: properties b) 命名: 基础名_语言简称_国家简称.properties 例如:msg_zh_CN.properties 存储所有中文 msg_en_US.properties 存储所有英文 2. 程序中获取 ResourceBundle类,可以读取国际化的资源文件!
@Test public void testI18N() throws Exception { // 中文语言环境 Locale locale = Locale.US; // 创建工具类对象ResourceBundle ResourceBundle bundle = ResourceBundle.getBundle("cn.itcast.f_i18n.msg", locale); // 根据key获取配置文件中的值 System.out.println(bundle.getString("hello")); System.out.println(bundle.getString("username")); System.out.println(bundle.getString("pwd")); }
动态文本国际化
中文:1987-09-19 ¥1000 英文: Sep/09 1987 $100 数值,货币,时间,日期等数据由于可能在程序运行时动态产生,所以无法像文字一样简单地将它们从应用程序中分离出来,而是需要特殊处理。Java 中提供了解决这些问题的 API 类(位于 java.util 包和 java.text 包中)
// 国际化 - 动态文本 - 0. 概述 @Test public void testI18N2() throws Exception { // 国际化货币 NumberFormat.getCurrencyInstance(); // 国际化数字 NumberFormat.getNumberInstance(); // 国际化百分比 NumberFormat.getPercentInstance(); // 国际化日期 //DateFormat.getDateTimeInstance(dateStyle, timeStyle, aLocale) } // 国际化 - 动态文本 - 1. 国际化货币 @Test public void testI18N3() throws Exception { // 模拟语言环境 Locale locale = Locale.CHINA; // 数据准备 double number = 100; // 工具类 NumberFormat nf = NumberFormat.getCurrencyInstance(locale); // 国际化货币 String m = nf.format(number); // 测试 System.out.println(m); } //面试题: 代码计算: $100 * 10 @Test public void eg() throws Exception { String str = "$100"; int num = 10; // 1. 分析str值是哪一国家的货币 Locale us = Locale.US; // 2. 国际化工具类 NumberFormat nf = NumberFormat.getCurrencyInstance(us); // 3. 解析str国币 Number n = nf.parse(str); System.out.println(n.intValue() * num); } // 国际化 - 动态文本 - 2. 国际化数值 @Test public void testI18N4() throws Exception { // 模拟语言环境 Locale locale = Locale.CHINA; NumberFormat nf = NumberFormat.getNumberInstance(Locale.US); String str = nf.format(1000000000); System.out.println(str); } // 国际化 - 动态文本 - 3. 国际化日期 /* * 日期 * FULL 2015年3月4日 星期三 * LONG 2015年3月4日 * FULL 2015年3月4日 星期三 * MEDIUM 2015-3-4 * SHORT 15-3-4 * * 时间 * FULL 下午04时31分59秒 CST * LONG 下午04时32分37秒 * MEDIUM 16:33:00 * SHORT 下午4:33 * * */ @Test public void testI18N5() throws Exception { // 日期格式 int dateStyle = DateFormat.SHORT; // 时间格式 int timeStyle = DateFormat.SHORT; // 工具类 DateFormat df = DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.CHINA); String date = df.format(new Date()); System.out.println(date); } // 面试2: 请将时间值:09-11-28 上午10时25分39秒 CST,反向解析成一个date对象。 @Test public void eg2() throws Exception { String str = "09-11-28 上午10时25分39秒 CST"; // 创建DateFormat工具类,国际化日期 DateFormat df = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.FULL, Locale.getDefault()); Date d = df.parse(str); System.out.println(d); }
Jsp页面国际化
<html> <head> <% ResourceBundle bundle = ResourceBundle.getBundle("cn.itcast.f_i18n.msg",request.getLocale()); %> <title><%=bundle.getString("title") %></title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <form name="frmLogin" action="${pageContext.request.contextPath }/admin?method=login" method="post"> <table align="center" border="1"> <tr> <td><%=bundle.getString("username") %></td> <td> <input type="text" name="userName"> </td> </tr> <tr> <td><%=bundle.getString("pwd") %></td> <td> <input type="password" name="pwd"> </td> </tr> <tr> <td> <input type="submit" value="<%=bundle.getString("submit") %>"> </td> </tr> </table> </form> </body> </html>
Jsp页面国际化 – 使用jstl标签
JSTL标签: 核心标签库 国际化与格式化标签库 数据库标签库(没用) 函数库 <fmt:setLocale value=""/> 设置本地化对象 <fmt:setBundle basename=""/> 设置工具类 <fmt:message></fmt:message> 显示国际化文本 格式化数值 <fmt:formatNumber pattern="#.##" value="100.99"></fmt:formatNumber> 格式化日期: <fmt:formatDate pattern="yyyy-MM-dd" value="${date}"/>
<html> <head> <!-- 一、设置本地化对象 --> <fmt:setLocale value="${pageContext.request.locale}"/> <!-- 二、设置工具类 --> <fmt:setBundle basename="cn.itcast.f_i18n.msg" var="bundle"/> <title><fmt:message key="title" bundle="${bundle}"></fmt:message></title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <form name="frmLogin" action="${pageContext.request.contextPath }/admin?method=login" method="post"> <table align="center" border="1"> <tr> <td><fmt:message key="username" bundle="${bundle}"></fmt:message></td> <td> <input type="text" name="userName"> </td> </tr> <tr> <td><fmt:message key="pwd" bundle="${bundle}"></fmt:message></td> <td> <input type="password" name="pwd"> </td> </tr> <tr> <td> <input type="submit" value="<fmt:message key="submit" bundle="${bundle}"/>"> </td> </tr> </table> </form> </body> </html>
相关文章推荐
- 第十一周 阅读程序(3)
- Linux上下文切换监控
- iOS 自定义手势
- Win10系统怎么安装Office2016?Win10安装Office2016教程
- 双线性插值的图像缩放算法的研究与实现
- 为什么防止跨数据库操作数据?
- 使用 Adium测试ejabberd服务器
- Python list 数据类型:列表
- Spring常用注解
- RxJava 详解for Android
- solrStudy1-solr字段详解
- 背包问题
- Android开发中,那些让你相见恨晚的方法、类或接口
- android动画xml参数解析
- HttpClient工具正确使用方式
- centos7.1 安装 svnmanager1.10
- Android Studio进阶使用技巧
- 代理模式
- Request.InputStream 接收Post Data
- centos下安装python mysqldb