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

package com.aff.i18n;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle; import org.junit.Test; public class I18nTest { /**
* ResourceBundle: 资源包类.
*
* 1. 在类路径下需要有对应的资源文件: baseName.properties. 其中 baseName 是基名.
* 2. 可以使用 基名_语言代码_国家代码.properties 来添加不同国家或地区的资源文件.
i18n_zh_CN.properties
* 3. 要求所有基名相同的资源文件的 key 必须完全一致.
* 4. 可以使用 native2ascii 命令来得到 汉字 对一个的 asc 码. Eclipse 内置了工具
* 5. 可以调用 ResourceBundle 的 getBundle(基名, Locale 实例) 获取获取 ResourceBundle 对象
* 6. 可以调用 ResourceBundle 的 getString(key) 来获取资源文件的 value 字符串的值.
* 7. 结合 DateFormat, NumberFormat, MessageFormat 即可实现国际化.
*
*/
@Test
public void testResourceBundle(){
Locale locale = Locale.CHINA;
ResourceBundle resourceBundle = ResourceBundle.getBundle("i18n", locale); System.out.println(resourceBundle.getString("date"));
System.out.println(resourceBundle.getString("salary")); String dateLabel = resourceBundle.getString("date");
String salLabel = resourceBundle.getString("salary"); String str = "{0}:{1}, {2}:{3}"; Date date = new Date();
double sal = 12345.12; DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
String dateStr = dateFormat.format(date); NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
String salStr = numberFormat.format(sal); String result = MessageFormat.format(str, dateLabel, dateStr, salLabel, salStr);
System.out.println(result);
} /**
* MessageFormat: 可以格式化模式字符串
* 模式字符串: 带占位符的字符串: "Date: {0}, Salary: {1}"
* 可以通过 format 方法会模式字符串进行格式化
*/
@Test
public void testMessageFormat(){
String str = "Date: {0}, Salary: {1}"; Locale locale = Locale.CHINA;
Date date = new Date();
double sal = 12345.12; DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
String dateStr = dateFormat.format(date); NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
String salStr = numberFormat.format(sal); String result = MessageFormat.format(str, dateStr, salStr);
System.out.println(result);
} /**
* NumberFormat: 格式化数字到数字字符串, 或货币字符串的工具类
* 1. 通过工厂方法获取 NumberFormat 对象
* NumberFormat.getNumberInstance(locale); //仅格式化为数字的字符串
* NumberFormat.getCurrencyInstance(locale); //格式为货币的字符串
*
* 2. 通过 format 方法来进行格式化
* 3. 通过 parse 方法把一个字符串解析为一个 Number 类型.
*/
@Test
public void testNumberFormat() throws ParseException{
double d = 123456789.123d;
Locale locale = Locale.FRANCE; //
NumberFormat numberFormat = NumberFormat.getNumberInstance(locale); String str = numberFormat.format(d);
System.out.println(str); NumberFormat numberFormat2 = NumberFormat.getCurrencyInstance(locale);
str = numberFormat2.format(d);
System.out.println(str); str = "123 456 789,123";
d = (Double) numberFormat.parse(str);
System.out.println(d); str = "123 456 789,12 €";
d = (Double) numberFormat2.parse(str);
System.out.println(d); } @Test
public void testDateFormate2() throws ParseException {
String str = "1993-04-23 12:12:12";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = dateFormat.parse(str);
System.out.println(date);// Fri Apr 23 00:12:12 CST 1993 } /*
* DateFormat: 格式化日期的工具类.
* DateFormate 本身是一个抽象类.
*
* 1. 若只希望通过 DateFormat 把一个 Date 对象转为一个字符串,
则可以通过 DateFormat 的工厂方法来获取 DateFormat 对象
* 2. 可以获取只格式化 Date 的 DateFormat 对象: getDateInstance(int style, Locale aLocale)
* 3. 可以获取只格式化 Time 的 DateFormat 对象: getTimeInstance(int style, Locale aLocale)
* 4. 可以获取既格式化 Date, 也格式化 Time 的 DateFormat 对象:
* getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
* 5. 其中 style 可以取值为: DateFormat 的常量: SHORT, MEDIUM, LONG, FULL. Locale
则为代表国家地区的 Locale 对象
* 6. 通过 DateFormat 的 format 方法来格式化个 Date 对象到字符串.
*
* 7. 若有一个字符串, 如何解析为一个 Date 对象呢 ?
* I. 先创建 DateFormat 对象: 创建 DateFormat 的子类 SimpleDateFormat 对象
* SimpleDateFormat(String pattern).
* 其中 pattern 为日期, 时间的格式, 例如: yyyy-MM-dd hh:mm:ss
* II. 调用 DateFormat 的 parse 方法来解析字符串到 Date 对象.
*
*/
@Test
public void testDateFormate() {
Locale locale = Locale.CHINA; Date date = new Date();
System.out.println(date);// Sun May 03 20:59:58 CST 2020 // 创建 DateFormate 对象
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
String str = dateFormat.format(date);
System.out.println(str);// 2020-5-3 20:59:58
} // Locale :java 中表示国家或地区的类,
// 可以通过 Locale(languageCode,countryCode) 的方式 来创建
// 在web应用 中可以通过 request.getLocale() 的方法来获取
@Test
public void testLocale() {
Locale locale = Locale.CHINA;
System.out.println(locale.getDisplayCountry());// 中国
System.out.println(locale.getLanguage());// zh
} }

中英文切换操作

index.jsp

<%@page import="java.util.Locale"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body> <%
Date date = new Date();
request.setAttribute("date", date); request.setAttribute("salary", 12345.67);
%> <%--
<fmt:bundle basename="i18n">
<fmt:message key="date"></fmt:message>:
<fmt:formatDate value="${date }"/>,
<fmt:message key="salary"></fmt:message>:
<fmt:formatNumber value="${salary }"></fmt:formatNumber>
</fmt:bundle>
<br><br>
--%> <%
String code = request.getParameter("code"); if(code != null){
if("en".equals(code)){
session.setAttribute("locale", Locale.US);
}else if("zh".equals(code)){
session.setAttribute("locale", Locale.CHINA);
} }
%> <c:if test="${sessionScope.locale != null }">
<fmt:setLocale value="${sessionScope.locale }"/>
</c:if> <fmt:setBundle basename="i18n"/> <fmt:message key="date"></fmt:message>:
<fmt:formatDate value="${date }" dateStyle="FULL"/>,
<fmt:message key="salary"></fmt:message>:
<fmt:formatNumber value="${salary }" type="currency"></fmt:formatNumber>
<br><br> <a href="index.jsp?code=en">English</a>
<a href="index.jsp?code=zh">中文</a> </body>
</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. Fiddler手机端抓包环境设置与过滤(一)

    一.PC端Fiddler设置 1.安装https 证书 打开Fiddler->Tool->Fiddler Options->HTTPS tab,勾选上并Capture HTTPS C ...

  2. 现代企业要求上什么样的MES(四)

    一个制造企业要想盈利,在生产方面要做的无非是提高资源利用效率和缩短生产通过时间(生产周期),而实现这俩步骤需要生产状况的在线透明及避免薄弱环节的分析数据,由此达到改善生产状态的目的.在erp系统中,通 ...

  3. Zabbix 添加vmware esxi监控

    1) Import the provided template. - TEMPLATE.VMWARE_ESXi_6.0_CIM.xml 2) Install Dependencies: # yum - ...

  4. 王颖奇 201771010129《面向对象程序设计(java)》第六周学习总结

    实验六 继承定义与使用 实验时间 2018-9-28 1.目的与要求 理论部分: 继承(inheritance): 继承的特点:具有结构层次:子类继承了父类的域和方法. 主要内容: (1)类.子类.超 ...

  5. GroupJoin()各参数的意义及用法

    EF Core中GroupJoin的注释比较复杂: public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey ...

  6. Java return 关键字

    一.基本概念 return一方面用在循环语句中来结束循环,另一方面用来终止函数的执行或者退出类的方法,并把控制权返回该方法的调用者.如果方法有返回类型,则return的返回该类型的值:如果没有返回值, ...

  7. Python --函数学习3 (将函数存储在模块中)

    将函数存储在模块 函数可以将代码块和主程序分离,通过给函数指定描述性名称,可以让主程序更加容易理解,还可以更进一步,将函数存储在模块的独立文件中,再将模块导入到主程序.import 语句允许再当前运行 ...

  8. Tomcat session的实现:线程安全与管理

    本文所说的session是单机版本的session, 事实上在当前的互联网实践中已经不太存在这种定义了.我们主要讨论的是其安全共享的实现,只从理论上来讨论,不必太过在意实用性问题. 1. sessio ...

  9. json 格式要求

    json 格式中, 字符串类型需要使用双引号,不能为单引号

  10. Zabbix-部署

    目录 一. apt安装 Zabbix 部署结构图和主机环境 1.1 Zabbix-server 安装配置 1.1.1 安装zabbix仓库 1.1.2 安装Zabbix server.web前端.ag ...