本地化和国际化

本地化

Java提供了一个本地化的对象!封装当前语言、国家、环境等特征!所在包:java.util.Locale。

代码示例

 1 import java.util.Locale;
 2 
 3 import org.junit.Test;
 4 
 5 public class App {
 6 
 7     @Test
 8     //1. 本地化对象:Locale
 9     // 封装语言、国家信息的对象,有java.util提供
10     public void testLocale() throws Exception {
11         // 模拟中国语言等环境
12         //Locale locale = Locale.CHINA;
13         Locale locale = Locale.getDefault();            // 当前系统默认的语言环境
14         System.out.println(locale.getCountry());           // CN  国家的简称
15         System.out.println(locale.getDisplayCountry()); // 国家名称
16         System.out.println(locale.getLanguage());        // zh 语言简称
17         
18         // 模拟美国国家
19         Locale l_us = Locale.US;
20         System.out.println(l_us.getCountry());
21         System.out.println(l_us.getDisplayCountry());
22     }
23 }
View Code

国际化

静态数据国际化

1、使用场景

网站中显示的固定文本的国际化,如:用户名和密码等固定文本。

2、操作步骤

(1)使用properties文件存储所有国家显示的文本字符串。properties文件的命名规则:基础名_语言简称_国家简称.properties。例如:msg_zh_CN.properties、 Msg_en_US.properties。

(2)使用ResourceBundle类,读取国际化资源文件。

3、代码示例

 1 public void testI18N() throws Exception {
 2         
 3         // 中文语言环境
 4         Locale locale = Locale.US;
 5         
 6         // 创建工具类对象ResourceBundle
 7         ResourceBundle bundle = ResourceBundle.getBundle("cn.itcast.f_i18n.msg", locale);
 8         // 根据key获取配置文件中的值
 9         System.out.println(bundle.getString("hello"));
10         System.out.println(bundle.getString("username"));
11         System.out.println(bundle.getString("pwd"));
12         
13     }
View Code

动态国际化文本

1、使用场景及解决方法

 数值,货币,时间,日期等数据由于可能在程序运行时动态产生,所以无法像文字一样简单地将它们从应用程序中分离出来,而是需要特殊处理。Java 中提供了解决这些问题的 API (位于 java.util 包和 java.text 包中)。

2、代码示例

2.1、常用类示例

 1 public void testI18N2() throws Exception {
 2         // 国际化货币
 3         NumberFormat.getCurrencyInstance();
 4         // 国际化数字
 5         NumberFormat.getNumberInstance();
 6         // 国际化百分比
 7         NumberFormat.getPercentInstance();  
 8         // 国际化日期
 9 //        DateFormat.getDateTimeInstance(dateStyle, timeStyle, aLocale)
10     }
View Code

2.2、国际化货币

 1 public void testI18N3() throws Exception {
 2         // 模拟语言环境
 3         Locale locale = Locale.CHINA;
 4         // 数据准备
 5         double number = 100;
 6         // 工具类
 7         NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
 8         // 国际化货币
 9         String m = nf.format(number);
10         // 测试
11         System.out.println(m);
12     }
View Code

2.3、面试题:代码计算:  $100 * 10

 1 public void eg() throws Exception {
 2         String str = "$100";
 3         int num = 10;
 4         
 5         // 1. 分析str值是哪一国家的货币
 6         Locale us = Locale.US;
 7         // 2. 国际化工具类
 8         NumberFormat nf = NumberFormat.getCurrencyInstance(us);
 9         // 3. 解析str国币
10         Number n = nf.parse(str);
11         
12         System.out.println(n.intValue() * num);
13     }
View Code

2.4、国际化数值

1 public void testI18N4() throws Exception {
2         // 模拟语言环境
3         Locale locale = Locale.CHINA;
4         NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
5         String str = nf.format(1000000000);
6         System.out.println(str);
7     }
View Code

2.5、国际化日期

 1 // 国际化 - 动态文本 - 3. 国际化日期
 2     /*
 3      * 日期
 4      *       FULL   2015年3月4日 星期三
 5      *       LONG   2015年3月4日
 6      *       FULL   2015年3月4日 星期三
 7      *    MEDIUM 2015-3-4
 8      *    SHORT  15-3-4
 9      *    
10      * 时间
11      *       FULL   下午04时31分59秒 CST
12      *       LONG   下午04时32分37秒
13      *    MEDIUM 16:33:00
14      *    SHORT  下午4:33
15      *    
16      * 
17      */
18     @Test
19     public void testI18N5() throws Exception {
20         
21         // 日期格式
22         int dateStyle = DateFormat.SHORT;
23         // 时间格式
24         int timeStyle = DateFormat.SHORT;
25         
26         // 工具类
27         DateFormat df = 
28             DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.CHINA);        
29         String date = df.format(new Date());
30         
31         System.out.println(date);
32     }
View Code

2.6、面试题: 请将时间值:09-11-28 上午10时25分39秒 CST,反向解析成一个date对象。

1 public void eg2() throws Exception {
2         String str = "09-11-28 上午10时25分39秒 CST";
3         // 创建DateFormat工具类,国际化日期
4         DateFormat df = DateFormat.getDateTimeInstance(
5                 DateFormat.SHORT, DateFormat.FULL, Locale.getDefault());
6         Date d = df.parse(str);
7         
8         System.out.println(d);
9     }
View Code

页面国际化

页面国际化采用JSTL标签:核心标签库,国际化与格式化标签库。

常用代码:

1 <fmt:setLocale value=""/>        设置本地化对象
2       <fmt:setBundle basename=""/>     设置工具类
3       <fmt:message></fmt:message>     显示国际化文本
4 格式化数值
5 <fmt:formatNumber pattern="#.##" value="100.99"></fmt:formatNumber>
6 格式化日期:
7 <fmt:formatDate pattern="yyyy-MM-dd" value="${date}"/>

代码示例

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%--引入jstl国际化与格式化标签库 --%>
 3 <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
 4 
 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 6 <html>
 7   <head>
 8       <!-- 一、设置本地化对象 -->
 9       <fmt:setLocale value="${pageContext.request.locale}"/>
10       <!-- 二、设置工具类 -->
11       <fmt:setBundle basename="cn.itcast.f_i18n.msg" var="bundle"/>
12 
13     <title><fmt:message key="title" bundle="${bundle}"></fmt:message></title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17   </head>
18   
19   <body>
20   <form name="frmLogin" action="${pageContext.request.contextPath }/admin?method=login" method="post">
21       <table align="center" border="1">
22           <tr>
23               <td><fmt:message key="username" bundle="${bundle}"></fmt:message></td>
24               <td>
25                   <input type="text" name="userName">
26               </td>
27           </tr>
28           <tr>
29               <td><fmt:message key="pwd" bundle="${bundle}"></fmt:message></td>
30               <td>
31                   <input type="password" name="pwd">
32               </td>
33           </tr>
34           <tr>
35               <td>
36                   <input type="submit" value="<fmt:message key="submit" bundle="${bundle}"/>">
37               </td>
38           </tr>
39       </table>
40   </form>
41   </body>
42 </html>
View Code
原文地址:https://www.cnblogs.com/nicker/p/6930556.html