javaweb国际化

根据数据的类型不同,国际化分为2类:静态数据国际化和动态数据的国际化。

 

静态数据,包括 “标题”、“用户名”、“密码”这样的文字数据。

动态数据,包括日期、货币等可以动态生成的数据。

 

国际化涉及到java.util.Locale和java.util.ResourceBundle类。

 

java.util.Locale

A Locale object represents a specific geographical, political, or cultural region.

Locale对象代表了一定的地理、政治、文化区域。

 

java.util.ResourceBundle

Resource bundles contain locale-specific objects. When your program needs a locale-specific resource, a String for example, your program can load it from the resource bundle that is appropriate for the current user's locale. In this way, you can write program code that is largely independent of the user's locale isolating most, if not all, of the locale-specific information in resource bundles. 

ResouceBundle,由两个单词组成Resouce和Bundle,合在一起就是“资源包”的意思。ResouceBundle是包含不同区域(Locale)资源的集合,只要向ResouceBundle提供一个特定的Locale对象,ResouceBundle就会把相应的资源返回给你。

wKiom1d5hMbSGxdUAAAyLcgcnhk020.png

1、静态数据国际化

 

静态数据国际化的步骤:

(1).建立资源文件,存储所有国家显示的文本的字符串

    a)文件: .properties

    b)命名:  基础名_语言简称_国家简称.properties

        例如: msg_zh_CN.properties     存储所有中文

            msg_en_US.properties    存储所有英文

(2).程序中获取

    ResourceBundle类,可以读取国际化的资源文件!

    Locale类,代表某一区域,用于决定使用哪一个国际化的资源。

 

1.1、Locale的API

static Locale getDefault()    得到JVM中默认的Locale对象

String getCountry()         得到国家名称的简写

String getDisplayCountry()    得到国家名称的全称

String getLanguage()        得到当前语言的简写

String getDisplayLanguage()    得到语言的全称

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.rk.i18n.demo;
import java.util.Locale;
public class Demo01
{
    public static void main(String[] args)
    {
        //本地化对象:Locale
        // 封装语言、国家信息的对象,由java.util提供
//      Locale locale = Locale.CHINA;
//      Locale locale = Locale.US;
        Locale locale = Locale.getDefault();
        String country = locale.getCountry();
        String displayCountry = locale.getDisplayCountry();
        String language = locale.getLanguage();
        String displayLanguage = locale.getDisplayLanguage();
         
        System.out.println(country);          //      CN       
        System.out.println(displayCountry);   //     中国       
        System.out.println(language);         //       zh       
        System.out.println(displayLanguage);  //      中文       
    }
}

 

1.2、ResourceBundle的API

static ResourceBundle getBundle(String baseName,Locale locale) 获取ResourceBundle实例

String getString(String key)    根据key获取资源中的值

 

 

1.3、示例

 

(1)建立properties文件:msg_zh_CN.properties和msg_en_US.properties

msg_zh_CN.properties

1
2
uname=\u59D3\u540D
pwd=\u5BC6\u7801

wKioL1d5jefT6vNlAAALbK1OAno343.png

 

msg_en_US.properties

1
2
uname=User Name
pwd=Password

(2)代码获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.rk.i18n.demo;
import java.util.Locale;
import java.util.ResourceBundle;
// 国际化 - 静态数据
public class Demo02
{
    public static void main(String[] args)
    {
        // 中文语言环境
        Locale locale = Locale.US;
         
        // 创建工具类对象ResourceBundle
        ResourceBundle bundle = ResourceBundle.getBundle("com.rk.i18n.demo.msg", locale);
         
        // 根据key获取配置文件中的值
        String uname = bundle.getString("uname");
        String pwd = bundle.getString("pwd");
         
        //输出
        System.out.println(uname);
        System.out.println(pwd);
    }
}

 

1.4、关于ResourceBundle的资源文件properties

文件命名:基础名、语言简称

Resource bundles belong to families whose members share a common base name, but whose names also have additional components that identify their locales. For example, the base name of a family of resource bundles might be "MyResources".  The family can then provide as many locale-specific members as needed, for example a German one named "MyResources_de". 

 

文件命名:国家简称

If there are different resources for different countries, you can make specializations: for example, "MyResources_de_CH" contains objects for the German language (de) in Switzerland (CH). If you want to only modify some of the resources in the specialization, you can do so. 

 

文件命名:默认的Resource Bundle

The family should have a default resource bundle which simply has the same name as its family - "MyResources" - and will be used as the bundle of last resort if a specific locale is not supported.

 

文件内容:属于同一个family的resource bundle要包含相同的items内容。

Each resource bundle in a family contains the same items, but the items have been translated for the locale represented by that resource bundle. For example, both "MyResources" and "MyResources_de" may have a String that's used on a button for canceling operations. In "MyResources" the String may contain "Cancel" and in "MyResources_de" it may contain "Abbrechen". 

 

Java代码:获取Resource Bundle

When your program needs a locale-specific object, it loads the ResourceBundle class using the getBundle method: 

 

 ResourceBundle myResources = ResourceBundle.getBundle("MyResources", currentLocale);

 

 

2、动态数据国际化

动态国际化则主要涉及到数字、货币、百分比和日期

例如:

    中文:1987-09-19   ¥1000

    英文: Sep/09 1987  $100

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.rk.i18n.demo;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import org.junit.Test;
public class Demo03
{
    // 国际化 - 动态文本 - 0. 概述
    public void testI18N()
    {
        // 国际化货币
        NumberFormat.getCurrencyInstance();
        // 国际化数字
        NumberFormat.getNumberInstance();
        // 国际化百分比
        NumberFormat.getPercentInstance();
        // 国际化日期
        // DateFormat.getDateTimeInstance(dateStyle, timeStyle, aLocale)
    }
    // 国际化 - 动态文本 - 1. 国际化货币
    @Test
    public void testI18NCurrency()
    {
        // 模拟语言环境
        Locale locale = Locale.CHINA;
        // 数据准备
        double number = 100;
        // 工具类
        NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
        // 国际化货币
        String str = nf.format(number);
        // 输出
        System.out.println(str);
    }
     
    //面试题:  代码计算:  $100 * 10  
    @Test
    public void testCurrency() throws ParseException
    {
        String str = "$100";
        int num = 10;
         
        // 1. 分析str值是哪一国家的货币
        Locale locale = Locale.US;
         
        // 2. 国际化工具类
        NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
         
        // 3. 解析str
        Number number = nf.parse(str);
         
        //4.进行计算
        int value = number.intValue() * num;
        //5.格式化输出
        str = nf.format(value);
        System.out.println(str);
    }
     
    // 国际化 - 动态文本 - 2. 国际化数值
    @Test
    public void testI18NNumber()
    {
        Locale locale = Locale.CHINA;
        NumberFormat nf = NumberFormat.getNumberInstance(locale);
        String str = nf.format(1000000000);
        System.out.println(str);
    }
     
    // 国际化 - 动态文本 - 3. 国际化百分比
    @Test
    public void testI18NPercent()
    {
        Locale locale = Locale.CHINA;
        NumberFormat nf = NumberFormat.getPercentInstance(locale);
        String str = nf.format(0.325);
        System.out.println(str);
    }
     
    // 国际化 - 动态文本 - 4. 国际化日期
    /*
     * 日期
     *      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 testI18NDate()
    {
        int dateStyle = DateFormat.FULL;
        int timeStyle = DateFormat.FULL;
        Locale locale = Locale.CHINA;
        DateFormat df = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
        String date = df.format(new Date());
        System.out.println(date);
    }
     
    // 面试2: 请将时间值:09-11-28 上午10时25分39秒 CST,反向解析成一个date对象。
    @Test
    public void testDate() throws ParseException
    {
        String str = "09-11-28 上午10时25分39秒 CST";
         
        int dateStyle = DateFormat.SHORT;
        int timeStyle = DateFormat.FULL;
        Locale locale = Locale.CHINA;
        // 创建DateFormat工具类,国际化日期
        DateFormat df = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
        Date date = df.parse(str);
        System.out.println(date);
    }
}

 

 

 

3、JSP页面国际化

 

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

 

3.1、准备工作:建立properties资源

建立2个properties文件:message_en_US.properties和message_zh_CN.properties。

 

message_zh_CN.properties

1
2
3
4
title=\u4F60\u597D\uFF0C\u8BF7\u767B\u5F55
uname=\u7528\u6237\u540D
pwd=\u5BC6\u7801
submit=\u63D0\u4EA4

wKiom1d5j8aArKdIAAAOCpAj0R8612.png

message_en_US.properties

1
2
3
4
title=Plean Log In
uname=User Name
pwd=Password
submit=Submit\!

 

3.2、使用JSP脚本进行国际化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <%
        ResourceBundle bundle = ResourceBundle.getBundle("com.rk.i18n.resource.message", 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>
    <table border="1">
        <tr>
            <td><%=bundle.getString("uname") %></td>
            <td><input type="text" name="uname"/></td>
        </tr>
        <tr>
            <td><%=bundle.getString("pwd") %></td>
            <td><input type="password" name="pwd"/></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="<%=bundle.getString("submit") %>"/></td>
        </tr>    
    </table>
  </body>
</html>

 

3.3、使用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}"/>

 

需要注意的一点是:HttpServletRequest有一个方法是getLocale(),可以获取当前request中的Locale信息,在EL表达式中可以使用${pageContext.request.locale}获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%--引入jstl国际化与格式化标签库 --%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <!-- 一、设置本地化对象 -->
    <fmt:setLocale value="${pageContext.request.locale }"/>
    <!-- 二、设置工具类 -->
    <fmt:setBundle basename="com.rk.i18n.resource.message" var="msg"/>
    <title><fmt:message bundle="${msg }" key="title"></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>
    <table border="1">
        <tr>
            <td><fmt:message bundle="${msg }" key="uname"></fmt:message></td>
            <td><input type="text" name="uname"/></td>
        </tr>
        <tr>
            <td><fmt:message bundle="${msg }" key="pwd"></fmt:message></td>
            <td><input type="password" name="pwd"/></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="<fmt:message bundle="${msg }" key="submit"></fmt:message>"/></td>
        </tr>    
    </table>
  </body>
</html>

 

格式化数值和日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>格式化</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
  <body>
      <!-- 
          格式化金额 
              格式: 0.00   保留2为小数,会自动补0
                   #.##  保留2为小数,不自动补0
      -->
    <fmt:formatNumber pattern="0.00" value="100" ></fmt:formatNumber> <br>
    <fmt:formatNumber pattern="#.##" value="100" ></fmt:formatNumber>  <br>
    <fmt:formatDate pattern="yyyyMMdd" value="<%=new Date() %>"/> <br>
    <%
        request.setAttribute("date", new Date());
    %>
    <fmt:formatDate pattern="yyyy-MM-dd"  value="${date }"/> <br>
  </body>
</html>

转载于:https://www.cnblogs.com/plan123/p/5639803.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/372714.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

20145335郝昊《网络攻防》Bof逆向基础——ShellCode注入与执行

20145335郝昊《网络攻防》Bof逆向基础——ShellCode注入与执行 实验原理 关于ShellCode&#xff1a;ShellCode是一段代码&#xff0c;作为数据发送给受攻击服务器&#xff0c;是溢出程序和蠕虫病毒的核心&#xff0c;一般可以获取权限。我们将代码存储到对方的堆栈中&#xff0…

Java枚举益智游戏

假设我们有以下代码&#xff1a; enum Case {CASE_ONE,CASE_TWO,CASE_THREE;private static final int counter;private int valueDependsOnCounter;static {int sum 0;for(int i 0; i<10; i) {sum i;}counter sum;} Case() {this.valueDependsOnCounter counter*counte…

jp在java中无法编译_JPanal上加图片的问题!

JPanal上加图片的问题&#xff01;import java.awt.BorderLayout;import java.awt.Dimension;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.*;import java.awt.*;public class Frame1 extends JFrame {JPanel contentPane;JLabel jLabel1 new JLa…

玩转Android之加速度传感器的使用,模仿微信摇一摇

Android系统带的传感器有很多种&#xff0c;最常见的莫过于微信的摇一摇了&#xff0c;那么今天我们就来看看Anroid中传感器的使用&#xff0c;做一个类似于微信摇一摇的效果。 OK ,废话不多说&#xff0c;我们就先来看看效果图吧&#xff1a; 当我摇动手机的时候这里的动画效果…

图像

背景图案的设置 将图片插入到网页中去 用图像作为超链接 使用工具建立地图索引 切片索引 为网站添加图标 5.1 背景图案的设置&#xff08;背景不占位置&#xff0c;不影响文本的输入&#xff09; 格式&#xff1a;<body background"URL"> 5.2 将图片插入…

Maven构建依赖项

熟悉发行版和快照依赖项的Maven和Gradle用户可能不了解TeamCity快照依赖项&#xff0c;或者认为他们与Maven相关&#xff08;这是不正确的&#xff09;。 熟悉工件和快照依赖关系的TeamCity用户可能不知道&#xff0c;除了TeamCity提供的插件之外&#xff0c;添加Artifactory插…

Java两种设计模式_23种设计模式(11)java策略模式

23种设计模式第四篇&#xff1a;java策略模式定义&#xff1a;定义一组算法&#xff0c;将每个算法都封装起来&#xff0c;并且使他们之间可以互换。类型&#xff1a;行为类模式类图&#xff1a;策略模式是对算法的封装&#xff0c;把一系列的算法分别封装到对应的类中&#xf…

Problem E: 平面上的点——Point类 (II)

Description 在数学上&#xff0c;平面直角坐标系上的点用X轴和Y轴上的两个坐标值唯一确定。现在我们封装一个“Point类”来实现平面上的点的操作。 根据“append.cc”&#xff0c;完成Point类的构造方法和show()方法&#xff0c;输出各Point对象的构造和析构次序。 接口描述&a…

MFC 控件RadioButton和CheckBox区别

1. 单个RadioButton在选中后&#xff0c;通过点击无法变为未选中 单个CheckBox在选中后&#xff0c;通过点击可以变为未选中 2. 一组RadioButton&#xff0c;只能同时选中一个 一组CheckBox&#xff0c;能同时选中多个 3. RadioButton在大部分UI框架中默认都以圆形表示 CheckBo…

什么是ActiveMQ?

尽管Active MQ网站已经对ActiveMQ进行了详尽的介绍&#xff0c;但我想在其定义中添加更多上下文。 从ActiveMQ项目的网站上&#xff1a; “ ActiveMQ是JMS 1.1的开源实现&#xff0c;是J2EE 1.4规范的一部分。” 这是我的看法&#xff1a;ActiveMQ是一种开源消息传递软件&…

字符串倒着输出java_Java 输出反转字符串

Java 输出反转字符串public class Test {public static void main(String args[]){try{// 获取键盘输入的字符串BufferReader f new BufferReader(new inputStreamReader(System.in));String str f.readline();for (int i str.length() -1 ; i >0 ; i--) {System.out.p…

webpack基础入门

我相信&#xff0c;有不少的朋友对webpack都有或多或少的了解。网上也有了各种各样的文章&#xff0c;文章内作者也写出了不少自己对于webpack这个工具的理解。在我刚刚接触webpack的时候&#xff0c;老实说&#xff0c;网上大部分的文章我是看不懂的。。webpack里面有很多名词…

位运算基础

异或运算的基础有点忘记了 先介绍一下。。2个数异或 就是对于每一个二进制位进行位运算 具有2个特殊的性质 1、一个数异或本身恒等于0&#xff0c;如5^5恒等于0&#xff1b; 2、一个数异或0恒等于本身&#xff0c;如5^0恒等于5。 3 满足交换律 1.交换数字这个性质能利用与交换数…

JAXB自定义绑定– Java.util.Date / Spring 3序列化

JaxB可以处理Java.util.Date序列化&#xff0c;但是需要以下格式&#xff1a; “ yyyy-MM-ddTHH&#xff1a;mm&#xff1a;ss ”。 如果需要将日期对象格式化为另一种格式怎么办&#xff1f; 我有同样的问题时&#xff0c;我正在同春MVC 3和Jackson JSON处理器 &#xff0c;最…

双足机器人简单步态生成

让机器人行走最简单的方法是先得到一组步态曲线&#xff0c;即腿部每个关节随时间运动的角度值。可以在ADAMS或3D Max、Blender等软件中建立好机构/骨骼模型&#xff0c;设计出脚踝和髋关节的运动曲线&#xff0c;然后进行逆运动学解算&#xff0c;测量每个关节在运动过程中的转…

重新访问了访客模式

访客模式是面向对象设计中最被高估但又被低估的模式之一。 高估了它&#xff0c;因为它常常被选择得太快&#xff08; 可能是由建筑宇航员选择的 &#xff09;&#xff0c;然后以错误的方式添加时会膨胀本来非常简单的设计。 如果您不遵循教科书示例&#xff0c;那么它可能会非…

java web开发技术大_2021年六大javaweb开发主流技术

作为历史最为悠久的编程语言——java&#xff0c;历经数十年依然盘踞在编程榜最前面的位置&#xff0c;这与它的技术和应用范围是分不开的&#xff0c;同时呢&#xff0c;javaweb开发主流技术更是java开发者时时刻刻关注的问题&#xff0c;接下来我们一起分析一下2020年互联网行…

ASP.NET—013:实现带控件的弹出层(弹出框)

http://blog.csdn.net/yysyangyangyangshan/article/details/38458169 在页面中用到弹出新页面的情况比较多的&#xff0c;一般来说都是使用JS方法showModalDialog("新页面相对路径?参数1&参数2",window,"新页面样式");然后会新弹出一个模态的page页。…

运维人员日常工作(转自老男孩)

1&#xff09;运维人员要谨记的6个字&#xff1a; 运维人员做事需遵循&#xff1a;简单、易用、高效 &#xff08;2&#xff09;运维人员服务的3大宗旨&#xff1a; 1、企业数据安全保障。 2、7*24小时业务持续提供服务。 3、不断提升用户感受、体验。 &#xff08;3&#xff0…

c# 操作DatatTable

dtTemp.Columns.Add("列名");//增加一列 dtTemp.Columns.Remove("列名");//删除一列 dtTemp.Columns["旧列名"].ColumnName "新列名";//修改列名 dtTemp.Columns["列名1"].SetOrdinal(dtTemp.Columns["列名2"].O…