1. 什么是国际化和本地化:

I. 本地化:一个软件在某个国家或地区使用时,采用该国家或地区的语言,数字,货币,日期等习惯。
II. 国际化:软件开发时,让它能支持多个国家和地区的本地化应用。使得应用软件能够适应多个地区的语言和文化风俗习惯
III. 本地敏感数据: 随用户区域信息而变化的数据称为本地信息敏感数据。例如数字,货币, 日期,时间等数据

2. 相关的 API:

I. DateFormat 和 SimpleDateFormat √.
II. NumberFormat
III. MessageFormat
IV. ResourceBundle
V. Locale

3. 关于国际化资源文件:

I. properties 文件格式
II. 必须提供 基名.properties 文件和 基名_语言代码_国家代码.properties 文件
III. 相同的 基名 的资源文件必须有相同的 key.
IV. 可能需要使用 native2ascii 工具把非 asc 码转为 asc 码.

4. WEB 的国际化

I. 可以使用 request.getLocale() 获取 Locale 对象
II. 可以使用 JSTL 的 fmt 标签完成的国际化. 后面使用框架提供的标签完成.
III. 实现 "中文" "英文" 的切换:

> 提供两个超简洁. 携带不同的变量值
> 根据变量值确定对应的 Locale 对象
> 把 Locale 对象放入到 session 中
> 绑定 Locale 对应的资源文件.

IV. 其他 fmt 标签可以参考 standard-examples.war 中的例子.

I18nTest

  1. package com.aff.i18n;
  2.  
  3. import java.text.DateFormat;
  4. import java.text.MessageFormat;
  5. import java.text.NumberFormat;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.Locale;
  10. import java.util.ResourceBundle;
  11.  
  12. import org.junit.Test;
  13.  
  14. public class I18nTest {
  15.  
  16. /**
  17. * ResourceBundle: 资源包类.
  18. *
  19. * 1. 在类路径下需要有对应的资源文件: baseName.properties. 其中 baseName 是基名.
  20. * 2. 可以使用 基名_语言代码_国家代码.properties 来添加不同国家或地区的资源文件.
    i18n_zh_CN.properties
  21. * 3. 要求所有基名相同的资源文件的 key 必须完全一致.
  22. * 4. 可以使用 native2ascii 命令来得到 汉字 对一个的 asc 码. Eclipse 内置了工具
  23. * 5. 可以调用 ResourceBundle 的 getBundle(基名, Locale 实例) 获取获取 ResourceBundle 对象
  24. * 6. 可以调用 ResourceBundle 的 getString(key) 来获取资源文件的 value 字符串的值.
  25. * 7. 结合 DateFormat, NumberFormat, MessageFormat 即可实现国际化.
  26. *
  27. */
  28. @Test
  29. public void testResourceBundle(){
  30. Locale locale = Locale.CHINA;
  31. ResourceBundle resourceBundle = ResourceBundle.getBundle("i18n", locale);
  32.  
  33. System.out.println(resourceBundle.getString("date"));
  34. System.out.println(resourceBundle.getString("salary"));
  35.  
  36. String dateLabel = resourceBundle.getString("date");
  37. String salLabel = resourceBundle.getString("salary");
  38.  
  39. String str = "{0}:{1}, {2}:{3}";
  40.  
  41. Date date = new Date();
  42. double sal = 12345.12;
  43.  
  44. DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
  45. String dateStr = dateFormat.format(date);
  46.  
  47. NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
  48. String salStr = numberFormat.format(sal);
  49.  
  50. String result = MessageFormat.format(str, dateLabel, dateStr, salLabel, salStr);
  51. System.out.println(result);
  52. }
  53.  
  54. /**
  55. * MessageFormat: 可以格式化模式字符串
  56. * 模式字符串: 带占位符的字符串: "Date: {0}, Salary: {1}"
  57. * 可以通过 format 方法会模式字符串进行格式化
  58. */
  59. @Test
  60. public void testMessageFormat(){
  61. String str = "Date: {0}, Salary: {1}";
  62.  
  63. Locale locale = Locale.CHINA;
  64. Date date = new Date();
  65. double sal = 12345.12;
  66.  
  67. DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
  68. String dateStr = dateFormat.format(date);
  69.  
  70. NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
  71. String salStr = numberFormat.format(sal);
  72.  
  73. String result = MessageFormat.format(str, dateStr, salStr);
  74. System.out.println(result);
  75. }
  76.  
  77. /**
  78. * NumberFormat: 格式化数字到数字字符串, 或货币字符串的工具类
  79. * 1. 通过工厂方法获取 NumberFormat 对象
  80. * NumberFormat.getNumberInstance(locale); //仅格式化为数字的字符串
  81. * NumberFormat.getCurrencyInstance(locale); //格式为货币的字符串
  82. *
  83. * 2. 通过 format 方法来进行格式化
  84. * 3. 通过 parse 方法把一个字符串解析为一个 Number 类型.
  85. */
  86. @Test
  87. public void testNumberFormat() throws ParseException{
  88. double d = 123456789.123d;
  89. Locale locale = Locale.FRANCE;
  90.  
  91. //
  92. NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
  93.  
  94. String str = numberFormat.format(d);
  95. System.out.println(str);
  96.  
  97. NumberFormat numberFormat2 = NumberFormat.getCurrencyInstance(locale);
  98. str = numberFormat2.format(d);
  99. System.out.println(str);
  100.  
  101. str = "123 456 789,123";
  102. d = (Double) numberFormat.parse(str);
  103. System.out.println(d);
  104.  
  105. str = "123 456 789,12 €";
  106. d = (Double) numberFormat2.parse(str);
  107. System.out.println(d);
  108.  
  109. }
  110.  
  111. @Test
  112. public void testDateFormate2() throws ParseException {
  113. String str = "1993-04-23 12:12:12";
  114. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  115.  
  116. Date date = dateFormat.parse(str);
  117. System.out.println(date);// Fri Apr 23 00:12:12 CST 1993
  118.  
  119. }
  120.  
  121. /*
  122. * DateFormat: 格式化日期的工具类.
  123. * DateFormate 本身是一个抽象类.
  124. *
  125. * 1. 若只希望通过 DateFormat 把一个 Date 对象转为一个字符串,
    则可以通过 DateFormat 的工厂方法来获取 DateFormat 对象
  126. * 2. 可以获取只格式化 Date 的 DateFormat 对象: getDateInstance(int style, Locale aLocale)
  127. * 3. 可以获取只格式化 Time 的 DateFormat 对象: getTimeInstance(int style, Locale aLocale)
  128. * 4. 可以获取既格式化 Date, 也格式化 Time 的 DateFormat 对象:
  129. * getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
  130. * 5. 其中 style 可以取值为: DateFormat 的常量: SHORT, MEDIUM, LONG, FULL. Locale
    则为代表国家地区的 Locale 对象
  131. * 6. 通过 DateFormat 的 format 方法来格式化个 Date 对象到字符串.
  132. *
  133. * 7. 若有一个字符串, 如何解析为一个 Date 对象呢 ?
  134. * I. 先创建 DateFormat 对象: 创建 DateFormat 的子类 SimpleDateFormat 对象
  135. * SimpleDateFormat(String pattern).
  136. * 其中 pattern 为日期, 时间的格式, 例如: yyyy-MM-dd hh:mm:ss
  137. * II. 调用 DateFormat 的 parse 方法来解析字符串到 Date 对象.
  138. *
  139. */
  140. @Test
  141. public void testDateFormate() {
  142. Locale locale = Locale.CHINA;
  143.  
  144. Date date = new Date();
  145. System.out.println(date);// Sun May 03 20:59:58 CST 2020
  146.  
  147. // 创建 DateFormate 对象
  148. DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
  149. String str = dateFormat.format(date);
  150. System.out.println(str);// 2020-5-3 20:59:58
  151. }
  152.  
  153. // Locale :java 中表示国家或地区的类,
  154. // 可以通过 Locale(languageCode,countryCode) 的方式 来创建
  155. // 在web应用 中可以通过 request.getLocale() 的方法来获取
  156. @Test
  157. public void testLocale() {
  158. Locale locale = Locale.CHINA;
  159. System.out.println(locale.getDisplayCountry());// 中国
  160. System.out.println(locale.getLanguage());// zh
  161. }
  162.  
  163. }

中英文切换操作

index.jsp

  1. <%@page import="java.util.Locale"%>
  2. <%@page import="java.util.Date"%>
  3. <%@ page language="java" contentType="text/html; charset=UTF-8"
  4. pageEncoding="UTF-8"%>
  5. <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
  6. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  7.  
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  9. <html>
  10. <head>
  11. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  12. <title>Insert title here</title>
  13. </head>
  14. <body>
  15.  
  16. <%
  17. Date date = new Date();
  18. request.setAttribute("date", date);
  19.  
  20. request.setAttribute("salary", 12345.67);
  21. %>
  22.  
  23. <%--
  24. <fmt:bundle basename="i18n">
  25. <fmt:message key="date"></fmt:message>:
  26. <fmt:formatDate value="${date }"/>,
  27. <fmt:message key="salary"></fmt:message>:
  28. <fmt:formatNumber value="${salary }"></fmt:formatNumber>
  29. </fmt:bundle>
  30. <br><br>
  31. --%>
  32.  
  33. <%
  34. String code = request.getParameter("code");
  35.  
  36. if(code != null){
  37. if("en".equals(code)){
  38. session.setAttribute("locale", Locale.US);
  39. }else if("zh".equals(code)){
  40. session.setAttribute("locale", Locale.CHINA);
  41. }
  42.  
  43. }
  44. %>
  45.  
  46. <c:if test="${sessionScope.locale != null }">
  47. <fmt:setLocale value="${sessionScope.locale }"/>
  48. </c:if>
  49.  
  50. <fmt:setBundle basename="i18n"/>
  51.  
  52. <fmt:message key="date"></fmt:message>:
  53. <fmt:formatDate value="${date }" dateStyle="FULL"/>,
  54. <fmt:message key="salary"></fmt:message>:
  55. <fmt:formatNumber value="${salary }" type="currency"></fmt:formatNumber>
  56. <br><br>
  57.  
  58. <a href="index.jsp?code=en">English</a>
  59. <a href="index.jsp?code=zh">中文</a>
  60.  
  61. </body>
  62. </html>

国际化之fmt标签的更多相关文章

  1. [原创]java WEB学习笔记52:国际化 fmt 标签,国际化的总结

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  2. 使用JSP的fmt标签实现国际化支持

    使用JSP的fmt标签配置i18n国际化资源文件可以实现根据不同的地区和语言切换不同的显示. 具体做法如下: 1.在JSP页面中添加fmt标签的引用: <%@ taglib prefix=&qu ...

  3. 使用JSP的fmt标签实现国际化支持 - smart-framework ; smart-plugin-i18n

    使用JSP的fmt标签实现国际化支持   Smart-framework框架使用smart-plugin-i18n插件来完成国际化处理,原理相同,使用过滤器进行参数设置. ============== ...

  4. JSTL标签库中fmt标签,日期,数字的格式化

    首先介绍日期的格式化:(不要嫌多哦) JSTL格式化日期(本地化) 类似于数字和货币格式化,本地化环境还会影响生成日期和时间的方式. <%@ page pageEncoding="UT ...

  5. jsp fmt标签详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt326 JSTL标签提供了对国际化(I18N)的支持,它可以根据发出请求的客户 ...

  6. JSP第五篇【JSTL的介绍、core标签库、fn方法库、fmt标签库】

    什么是JSTL JSTL全称为 JSP Standard Tag Library 即JSP标准标签库. JSTL作为最基本的标签库,提供了一系列的JSP标签,实现了基本的功能:集合的遍历.数据的输出. ...

  7. fmt标签格式化数字和时间

    有时候需要格式化输出数字和时间,fmt 标签是个很好用的标签,下面是我做的总结: 在页面的头部加入这个标签 <%@ taglib uri="http://java.sun.com/js ...

  8. JAVAWEB 一一 fmt标签 和日期插件

    fmt标签 效果 操作:  ①导入标签 <%@ taglib uri=httpjava.sun.comjspjstlfmt prefix=fmt %> ②标签这么写 <td>& ...

  9. jstl-日期格式化-jsp页面需引入fmt标签

    jsp页面需引入fmt标签: <taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"> ...

随机推荐

  1. 编译原理-第四章 语法分析-4.7 规范的LR分析

    规范的LR分析 一.规范LR(l)项 二.规范LR(l)项集族 1.构建项目集 2.例 三.规范LR(1)语法分析表 1.构造 2.例1 3.例2 四.LALR语法分析表 1.重要性 2.特点 3.构 ...

  2. redis-py中的坑

    今天发现,使用redis-py从redis中获取的数据竟然是加密的. conn = redis.Redis(host='redis_serverip', port=6379, password='re ...

  3. HDU 3038 (向量图解)

    题意:\(有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X.\) \(然后求出这m个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突\) \ ...

  4. IDEA中如何使用debug调试项目 一步一步详细教程

    转载该文章:https://blog.csdn.net/yxl_1207/article/details/80973622 一.Debug开篇 首先看下IDEA中Debug模式下的界面. 如下是在ID ...

  5. Android自定义顶部栏及侧滑菜单和fragment+viewpag滑动切换的实现

    嘿嘿嘿,关于android滑动的操作,是不是经常都会用到呢. 我肯定也要学习一下啦. https://blog.csdn.net/u013184970/article/details/82882107 ...

  6. 构建自己的专用OpenCV----记录一次由applyColorMap()引发的探索

    在编写实际项目的过程中,我需要实现绿色主题的"伪彩色"变换.在目前提供的模板中,只有summer最为接近,但是它的颜色太浅了,看上去不是很清晰.所以我结合ocean和summer两 ...

  7. LabVIEW(数据库连接)

    Driver={Microsoft Access Driver (*.mdb)}; Dbq=路径; Uid=Admin; Pwd=密码; 插入: INSERT INTO xs(学号,姓名,专业名,性别 ...

  8. [hdu1506]单调队列(栈)

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=1506看图一目了然.两个方向单调队列维护下. #include <iostream> #incl ...

  9. 手机app传统邀请码安装与免邀请码安装区别,如何选择呢?

    App 邀请机制是每个产品几乎必做的功能点,它一般以两种形式存在:一是作为常置功能用于推荐,二是作为裂变活动用于邀请. 无论以哪种形式出现,都可以归为社交分享的一种表现方式.相较于营销推广,邀请好友机 ...

  10. python 基础知识5-集合

    1.集合set概念: 集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 1.去重,把一个列表变成集合, ...