国际化—— i18n:internationalization
来源:互联网 发布:老僧扫矿软件 病毒 编辑:程序博客网 时间:2024/05/20 02:25
如何做到国际化的软件,要求:
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类,可以读取国际化的资源文件!
动态文本国际化
中文:1987-09-19 ¥1000
英文: Sep/09 1987 $100
数值,货币,时间,日期等数据由于可能在程序运行时动态产生,所以无法像文字一样简单地将它们从应用程序中分离出来,而是需要特殊处理。Java 中提供了解决这些问题的 API 类(位于 java.util 包和 java.text 包中)
// 国际化 - 静态数据 @Test public void testI18N() throws Exception { // 中文语言环境 Locale locale = Locale.US; // 创建工具类对象ResourceBundle ResourceBundle bundle = ResourceBundle.getBundle("cn.dota.f_i18n.msg", locale); // 根据key获取配置文件中的值 System.out.println(bundle.getString("hello")); System.out.println(bundle.getString("username")); System.out.println(bundle.getString("pwd")); }
// 国际化 - 动态文本 - 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.dota.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>
0 0
- 国际化—— i18n:internationalization
- Struts Internationalization (i18n 国际化)
- Struts Internationalization (i18n 国际化)
- i18n(Internationalization)
- 国际化(Internationalization)被缩写为I18N, 即只取首尾两个字母, 中间字母为18个。
- Internationalization (国际化)
- Flex2 国际化(internationalization)
- Flex3 国际化(internationalization)
- 12. Internationalization国际化支持
- 国际化 i18n ———— 国家语言代码
- javaweb学习总结(三十一)——国际化(i18n)
- 【小企】javaweb学习总结(三十一)——国际化(i18n)
- javaweb学习总结(三十一)——国际化(i18n)
- javaweb学习总结(三十一)——国际化(i18n)
- 菜鸟学Struts——I18N对国际化的支持
- javaweb学习总结(三十一)——国际化(i18n)
- struts.custom.i18n.resources——struts国际化
- javaweb学习总结(三十一)——国际化(i18n)
- jquery 合并相同内容的单元格,根据某列可以指定合并后面的任意列
- python中的list、dict、string基本操作
- java中四种引用类型
- 错误处理error C4996: 'fopen': This function or variable may be unsafe
- Spring分布式事务实现
- 国际化—— i18n:internationalization
- YUV420数据转为IplImage格式
- Java多线程编程(一)
- BZOJ2705: [SDOI2012]Longge的问题
- Hashmap实现原理
- 接口和抽象类
- 面向对象编程的关键点(用php实现面向对象的、变化跟不变化进行伪代码逻辑进阶编程)
- iOS 一些关键描述字
- c++快速简易入门教程_007虚函数与多态性、纯虚函数