码迷,mamicode.com
首页 > 其他好文 > 详细

国际化问题

时间:2018-07-24 00:44:01      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:界面   哪些   throw   height   exp   ica   efi   tde   color   

国际化问题:

软件开发时,要使他能同时应对世界各地不同地区的访问,并针对不同地区和国家的访问,提供相应的,符合来访者阅读习惯的页面数据。

国际化又称:i18n:internationalization

1.哪些信息需要国际化?

  提示文本

  日期和货币

  货币符号

2.固定文本的国际化:

  借助消息资源包:一个消息资源包由多个*.properties文件组成。属于同一个消息资源包的多个properties文件有着以下特点:

          msg_语言代码_国家代码.properties(即msg    .properties 相同,语言或者国家代码可以不同)消息资源包中有着同一的key

 

3.

 

msg_zh_CN.properties   中国大陆

 

msg_zh_HK.properties   中国香港

 

msg_en_US.properties   美国佬

 

msg_en_UK.properties   大不列颠

 

3.2读取消息资源包

 

java.util.ResourceBundle:专门读取properties文件的

 

java.util.Locale:代表着一个地区信息

语言代码(两位小写字母)和国家代码(两位大写字母),由ISO同一指定。

 

 

          技术分享图片

  *.properties文件:

技术分享图片
1 hello=\u60A8\u5403\u4E86\u5417
2 jsp.login.title=\u7528\u6237\u767B\u5F55
3 jsp.login.username=\u7528\u6237\u540D
4 jsp.login.password=\u5BC6\u7801
5 jsp.login.submit=\u767B\u5F55
msg_zh_CN.properties
技术分享图片
1 hello=hello,good morning
2 jsp.login.title=User Login
3 jsp.login.username=username
4 jsp.login.password=password
5 jsp.login.submit=Login
msg_en_US.properties

  获取内容:

技术分享图片
 1 import java.util.Locale;
 2 import java.util.ResourceBundle;
 3 
 4 import org.junit.Test;
 5 
 6 public class ResourceBundleDemo {
 7     //按照本地的区域信息读取消息资源中的内容
 8     @Test
 9     public void test1(){
10         //消息资源包的基名:
11             //classes/msg_en_US.properties           msg
12             //classes/com/itheima/resources/msg_en_US.properties com.itheima.resources.msg
13         ResourceBundle rb = ResourceBundle.getBundle("com.itheima.resources.msg");
14         String value = rb.getString("hello");
15         System.out.println(value);
16     }
17     //指定区域信息
18     @Test
19     public void test2(){
20         Locale locale = Locale.US;
21         ResourceBundle rb = ResourceBundle.getBundle("com.itheima.resources.msg",locale);
22         String value = rb.getString("hello");
23         System.out.println(value);
24     }
25 }
ResourceBundleDemo

登入界面实例:

技术分享图片
 1 <%@ page import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4     <%
 5     Locale locale = request.getLocale();
 6     ResourceBundle rb = ResourceBundle.getBundle("com.itheima.resources.msg", locale);
 7     %>
 8 
 9   <head>
10     <title><%=rb.getString("jsp.login.title") %></title> 
11     <meta http-equiv="pragma" content="no-cache">
12     <meta http-equiv="cache-control" content="no-cache">
13     <meta http-equiv="expires" content="0">    
14     <!--
15     <link rel="stylesheet" type="text/css" href="styles.css">
16     -->
17 
18   </head>
19   
20   <body>
21     <form action="">
22         <%=rb.getString("jsp.login.username") %>:<input type="text" name="name"/><br/>
23         <%=rb.getString("jsp.login.password") %>:<input type="password" name="password"/><br/>
24         <input type="submit" value="<%=rb.getString("jsp.login.submit") %>"/>
25     </form>
26   </body>
27 </html>
Login.jsp

标签简化:

技术分享图片
 1 <%@ page import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5     <fmt:setLocale value="${pageContext.request.locale}"/>
 6     <fmt:setBundle basename="com.itheima.resources.msg" var="msg" scope="page"/>
 7   <head>
 8     <title>
 9         <fmt:message key="jsp.login.title" bundle="${msg}"></fmt:message>
10     </title> 
11     <meta http-equiv="pragma" content="no-cache">
12     <meta http-equiv="cache-control" content="no-cache">
13     <meta http-equiv="expires" content="0">    
14     <!--
15     <link rel="stylesheet" type="text/css" href="styles.css">
16     -->
17 
18   </head>
19   
20   <body>
21     <form action="">
22         <fmt:message key="jsp.login.username" bundle="${msg}"></fmt:message>:<input type="text" name="name"/><br/>
23         <fmt:message key="jsp.login.password" bundle="${msg}"></fmt:message>:<input type="password" name="password"/><br/>
24         <input type="submit" value="<fmt:message key="jsp.login.submit" bundle="${msg}"></fmt:message>"/>
25     </form>
26   </body>
27 </html>
Login.jsp

 

 

 

 

4、日期和时间的格式化

 

保存数据时:String------------>java.util.Date:解析

 

显示数据时:java.util.Date------------->String:格式化

 

DateFormat:

String DateFormat.format(Date)

Date DateFormat.parse(String)

Locale locale=Locale.US//美国区域

问题:按照什么格式呢?

DateFormat df = SimpleDateFormat(String pattern):自由指定格式

DateFormat df = DateFormat .getDateInstance(int style, Locale aLocale)

DateFormat df = DateFormat .getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)

DateFormat df = DateFormat .getTimeInstance(int style, Locale aLocale)

 

DateFormat

date

time

both(date+time)

SHORT

14-5-21

上午10:25

14-5-21 上午10:25

MEDIUM

2014-5-21

10:21:51

2014-5-21 10:21:51

DEFAULT

2014-5-21

10:21:51

2014-5-21 10:21:51

LONG

2014年5月21日

上午10时25分22秒

2014年5月21日上午10时25分22秒

FULL

2014年5月21日 星期三

上午10时21分51秒 CST

2014年5月21日 星期三 上午10时21分51秒 CST

 

技术分享图片
 1 public class DataFormatDemo {
 2 
 3     @Test
 4     public void TestTime(){
 5         Date now=new Date();
 6         DateFormat df=DateFormat.getDateTimeInstance();
 7         System.out.println(df.format(now));
 8         
 9         df=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
10         System.out.println(df.format(now));
11         
12         df=DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
13         System.out.println(df.format(now));
14         
15         df=DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
16         System.out.println(df.format(now));
17     }
18     @Test
19     public void TestTime2() throws ParseException{
20         String now="2018年7月23日 星期一 下午06时15分01秒 CST";
21         DateFormat df=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
22         Date date=df.parse(now);
23         System.out.println(date.toString());
24     }
25 }
26 /*
27 实验结果:
28 2018-7-23 18:19:44
29 2018年7月23日 星期一 下午06时19分44秒 CST
30 2018年7月23日 下午06时19分44秒
31 18-7-23 下午6:19
32 Mon Jul 23 18:15:01 CST 2018
33 
34 */
DateFormat

 

5、数字的格式化

保存数据时:String------------>Number:解析

显示数据时:Number------------->String:格式化

NumberFormat类:

技术分享图片
 1 import java.text.NumberFormat;
 2 import java.text.ParseException;
 3 import java.util.Locale;
 4 
 5 import org.junit.Test;
 6 
 7 public class NumberFormatDemo {
 8     @Test
 9     public void test1(){
10         int money = 1000000;
11         NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
12         String s = nf.format(money);
13         System.out.println(s);
14     }
15     @Test
16     public void test2() throws ParseException{
17         String s = "$1,000,000.00";
18         NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
19         Number n = nf.parse(s);
20         System.out.println(n.intValue());
21     }
22     @Test
23     public void test3(){
24         float f = 3.0f/12;
25         NumberFormat nf = NumberFormat.getPercentInstance();
26         String s = nf.format(f);
27         System.out.println(s);
28     }
29 }
NumberFormatDemo
技术分享图片
 1 import java.text.DateFormat;
 2 import java.text.MessageFormat;
 3 import java.text.NumberFormat;
 4 import java.util.Date;
 5 import java.util.Locale;
 6 
 7 import org.junit.Test;
 8 
 9 public class MessageFormatDemo {
10     @Test
11     public void test1(){
12         String time = "";
13         String date = "";
14         String smoney = "";
15         
16         
17         Date d = new Date();
18         int money = 1000000;
19         
20         Locale locale = Locale.US;
21         DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT, locale);
22         time = df.format(d);
23         df = DateFormat.getDateInstance(DateFormat.DEFAULT,locale);
24         date = df.format(d);
25         NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
26         smoney = nf.format(money);
27         
28         String s = "At "+time+" on "+date+", a hurricance destroyed 99 houses and caused "+smoney+" of damage";
29         System.out.println(s);
30         
31     }
32     @Test
33     public void test2(){
34         // {0}占位符。对应的是数组中的元素
35         String pattern = "At {0} on {0}, a hurricance destroyed 99 houses and caused {1} of damage";
36         Locale locale = Locale.US;
37         MessageFormat mf = new MessageFormat(pattern, locale);
38         String s = mf.format(new Object[]{new Date(),100000});
39         System.out.println(s);
40     }
41     @Test
42     public void test3(){
43         // {索引,类型,样式}占位符。对应的是数组中的元素
44         String pattern = "At {0,time,short} on {0,date,medium}, a hurricance destroyed 99 houses and caused {1,number,currency} of damage";
45         Locale locale = Locale.US;
46         MessageFormat mf = new MessageFormat(pattern, locale);
47         String s = mf.format(new Object[]{new Date(),100000});
48         System.out.println(s);
49     }
50 }
MessageFormatDemo

 

技术分享图片

6.JSTL_format标签简介:

参考文件:

技术分享图片Login.jsp
技术分享图片
 1 <%@ page import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 5 <html>
 6   <head>
 7     <title>title</title> 
 8     <meta http-equiv="pragma" content="no-cache">
 9     <meta http-equiv="cache-control" content="no-cache">
10     <meta http-equiv="expires" content="0">    
11     <!--
12     <link rel="stylesheet" type="text/css" href="styles.css">
13     -->
14 
15   </head>
16   
17   <body>
18     <%
19     pageContext.setAttribute("now", new Date());
20      %>
21      ${now}<br/>
22      <fmt:formatDate value="${now}" type="date" dateStyle="full"/><br/>
23      <fmt:formatDate value="${now}" type="time"/><br/>
24      <fmt:formatDate value="${now}" type="both"/><br/>
25      <fmt:formatDate value="${now}" pattern="yyyy-MM-dd"/><br/>
26   </body>
27 </html>
Date.jsp
技术分享图片
 1 <%@ page import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5   <head>
 6     <title>title</title> 
 7     <meta http-equiv="pragma" content="no-cache">
 8     <meta http-equiv="cache-control" content="no-cache">
 9     <meta http-equiv="expires" content="0">    
10     <!--
11     <link rel="stylesheet" type="text/css" href="styles.css">
12     -->
13 
14   </head>
15   
16   <body>
17     <%
18     pageContext.setAttribute("money", 1000000);
19      %>
20      ${money}<br/>
21      <fmt:formatNumber groupingUsed="true" type="currency" value="${money}"></fmt:formatNumber>
22   </body>
23 </html>
Number.jsp

 

国际化问题

标签:界面   哪些   throw   height   exp   ica   efi   tde   color   

原文地址:https://www.cnblogs.com/biaogejiushibiao/p/9356295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!