JavaWeb学习记录(二十一)——国际化处理

¨国际化又称为 i18n:internationalization
¨对于软件中的菜单栏、导航条、错误提示信息,状态信息等这些固定不变的文本信息,可以把它们写在一个properties文件中,并根据不同的国家编写不同的properties文件。这一组properties文件称之为一个资源包。
  (1)hytc.properties:
  username=u9ED8u8BA4u7528u6237u540D
  name=mName
  pass=mPass
  login=mLogin
  reset=mReset
  (2)hytc_zh.properties:
  username=u7528u6237u540D
  name=u7528u6237u540D
  pass=u5BC6u7801
  login=u767Bu5F55
  reset=u91CDu7F6E
  (3)hytc_en.properties:
  username=userName
  name=Name
  pass=Pass
  login=Login
  reset=Reset
  
一、基本测试
public class I18NTest {
    @Test
    public void test1(){
        ResourceBundle bundle=ResourceBundle.getBundle("hytc", Locale.CHINA);
        
        String userName=bundle.getString("username");
        
        System.out.println(userName);
    }
}
 
网页中的应用
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
 <div>
        <table>
            <tr>
                <td>
                    <fmt:bundle basename="hytc">
                        <fmt:message key="name"/>
                    </fmt:bundle>
                </td>
                <td>
                    <input type="text" name="name"/>
                </td>
            </tr>
            <tr>
                <td>
                    <fmt:bundle basename="hytc">
                        <fmt:message key="pass"/>
                    </fmt:bundle>
                </td>
                <td>
                    <input type="password" name="pass"/>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center;">
                    <fmt:bundle basename="hytc">
                        <fmt:message key="login" var="login" scope="page"/>
                    </fmt:bundle>
                    <fmt:bundle basename="hytc">
                        <fmt:message key="reset" var="reset" scope="page"/>
                    </fmt:bundle>
                    <input type="submit" value="${login}" style="margin-right: 50px"/>
                    <input type="reset" value="${reset}"/>
                </td>
            </tr>
        </table>
    </div>
二、添加前缀
  (1)hytc.properties:
  hytc.test.username=u9ED8u8BA4u7528u6237u540D
  hytc.test.name=mName
  hytc.test.pass=mPass
  hytc.test.login=mLogin
  hytc.test.reset=mReset
  (2)hytc_zh.properties:
  hytc.test.username=u7528u6237u540D
  hytc.test.name=u7528u6237u540D
  hytc.test.pass=u5BC6u7801
  hytc.test.login=u767Bu5F55
  hytc.test.reset=u91CDu7F6E
  (3)hytc_en.properties:
  hytc.test.username=userName
  hytc.test.name=Name
  hytc.test.pass=Pass
  hytc.test.login=Login
  hytc.test.reset=Reset
网页中的应用
<div>
        <table>
            <tr>
                <td>
                    <fmt:bundle basename="hytc">
                        <fmt:message key="hytc.test.name"/>
                    </fmt:bundle>
                </td>
                <td>
                    <input type="text" name="name"/>
                </td>
            </tr>
            <tr>
                <td>
                    <fmt:bundle basename="hytc">
                        <fmt:message key="hytc.test.pass"/>
                    </fmt:bundle>
                </td>
                <td>
                    <input type="password" name="pass"/>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center;">
                    <fmt:bundle basename="hytc" prefix="hytc.test.">
                        <fmt:message key="login" var="login" scope="page"/>
                    </fmt:bundle>
                    <fmt:bundle basename="hytc" prefix="hytc.test.">
                        <fmt:message key="reset" var="reset" scope="page"/>
                    </fmt:bundle>
                    <input type="submit" value="${login}" style="margin-right: 50px"/>
                    <input type="reset" value="${reset}"/>
                </td>
            </tr>
        </table>
    </div>
三、占位符使用
在.properties文件中添加以下内容
msg=u4F60u597D {0}! {1}
应用:
 
 <div>
        <fmt:message key="msg" bundle="${bname }">
            <fmt:param value="zsf"/>
            <fmt:param>哈哈</fmt:param>
        </fmt:message>
    </div>
 
、数值,货币,时间,日期等数据由于可能在程序运行时动态产生,所以无法像文字一样简单地将它们从应用程序中分离出来,而是需要特殊处理。Java 中提供了解决这些问题的 API 类(位于 java.util 包和 java.text 包中)
¨在JSTL之中的国际化标签主要包括<fmt:setLocale>、<fmt:bundle>、<fmt:setBundle>、<fmt:message>等。
<fmt:setLocale>:指定Locale环境。
<fmt:bundle>:指定消息资源使用的文件。
<fmt:setBundle>:设置消息资源文件。
<fmt:message>:显示消息资源文件中指定key的消息,支持带参数消息
五、标签详解:
(1)<fmt:setLocale>
<fmt:setLocale>标签用于在JSP页面中显式地设置用户的本地化信息,并将设置的本地化信息以Locale对象的形式保存在某个Web域中,其在Web域中的属性名称为“javax.servlet.jsp.jstl.fmt.locale”。使用<fmt:setLocale>标签设置本地化信息后,国际化标签库中的其他标签将使用该本地化信息,而忽略客户端浏览器传递过来的本地信息。

<fmt:setLocale value="locale"

  [variant="variant"]

  [scope="{page|request|session|application}"] />

(2)<fmt:setBundle>

<fmt:setBundle>标签用于根据<fmt:setLocale>标签设置的本地化信息创建一个资源包(ResourceBundle)实例对象,并将其绑定到一个Web域的属性上。

标签的语法格式如下:

<fmt:setBundle basename="basename"

  [var="varName"]

  [scope="{page|request|session|application}"] />

(3)<fmt:bundle>标签

<fmt:bundle>标签与<fmt:setBundle>标签的功能类似,但它创建的ResourceBundle实例对象只在其标签体内有效。

<fmt:bundle basename="basename“ >

  body content

</fmt:bundle>

(4)<fmt:message>标签

  <fmt:message>标签用于从一个资源包中读取信息并进行格式化输出。

 <fmt:message key="messageKey" 

  [bundle="resourceBundle"]

  [var="varName"]     [scope="{page|request|session|application}"] />

(5)<fmt:param>标签

<fmt:param>标签用于为格式化文本串中的占位符设置参数值,它只能嵌套在<fmt:message>标签内使用。

语法1,用value属性指定参数值:

<fmt:param value="messageParameter" />

语法2,在标签体中指定参数的值的情况:

<fmt:param>

  body content

</fmt:param>

(6)<fmt:formatDate>标签

<fmt:formatDate>标签用于对日期和时间按本地化信息进行格式化,或对日期和时间按化为JSP页面作者自定义的格式进行格式化。语法格式如下:

<fmt:formatDate value="date" 

  [type="{time|date|both}"]

  [dateStyle="{default|short|medium|long|full}"]

  [timeStyle="{default|short|medium|long|full}"]

  [pattern="customPattern"]

  [timeZone="timeZone"]

  [var="varName"]

  [scope="{page|request|session|application}"] />

(7)<fmt:parseDate>标签

 <fmt:parseDate>标签与<fmt: formatDate >标签的作用正好相反,它用于将一个表示日期和时间的字符串解析成java.util.Date实例对象.

  语法1,没有标签体的情况:

    <fmt:parseDate value="dateString"

  [type="time|date|both"]

  [dateStyle="default|short|medium|long|full"]

  [timeStyle="default|short|medium|long|full"]

  [pattern="customPattern"]

  [timeZone="timeZone"]

  [parseLocale="parseLocale"]

  [var="varName"]

  [scope="{page|request|session|application}"] />

(8)<fmt:formatNumber>标签

  <fmt:formatNumber>标签用于将数值、货币或百分数按本地化信息进行格式化,或者按JSP页面作者自定义的格式进行格式化。

  <fmt:formatNumber value="numericValue"

     [type="{number|currency|percent}"]

     [pattern="customPattern"]

     [currencyCode="currencyCode"]

     [currencySymbol="currencySymbol"]

     [groupingUsed="{true|false}"]

     [maxIntegerDigits="maxIntegerDigits"]

     [minIntegerDigits="minIntegerDigits"]

     [maxFractionDigits="maxFractionDigits"]

     [minFractionDigits="minFractionDigits"]

     [var="varName"]

     [scope="{page|request|session|application}"] />

(9)<fmt:parseNumber>标签

  <fmt:parseDate>标签与<fmt: formatNumber >标签的作用正好相反,它用于将一个按本地化方式被格式化后的数值、货币或百分数解析为数值

  语法1,没有标签体的情况:

  <fmt:parseNumber value="numericValue" 

  [type="{number|currency|percent}"]

  [pattern="customPattern"]

  [parseLocale="parseLocale"]

  [integerOnly="{true|false}"]

  [var="varName"

  [scope="{page|request|session|application}"] />

六、日期应用的案例

    <div>
        
            <%
                request.setAttribute("date", new Date(System.currentTimeMillis()) );
             %>
              <!-- 1.日期格式输出  -->
             <fmt:formatDate value="${date }"/><br/>
             <!-- 2.日期格式输出   时间格式 9:49:34 AM -->
             <fmt:formatDate value="${date }" type="time"/><br/>
             <!-- 2.日期格式输出   日期格式Mar 31, 2015 -->
             <fmt:formatDate value="${date }" type="date"/><br/>
             <!-- 2.日期格式输出  日期加时间格式:Mar 31, 2015 9:49:34 AM -->
             <fmt:formatDate value="${date }" type="both"/><br/>
             <!-- 3.日期格式输出  日期加时间格式:Mar 31, 2015 9:49:34 AM  short|medium|long|full -->
             <fmt:formatDate value="${date }" type="both" dateStyle="short"/><br/>
             <fmt:formatDate value="${date }" type="both" dateStyle="medium"/><br/>
             <fmt:formatDate value="${date }" type="both" dateStyle="long"/><br/>
             <fmt:formatDate value="${date }" type="both" dateStyle="full"/><br/>
             <!-- 4.日期格式输出  日期加时间格式:Mar 31, 2015 9:49:34 AM  short|medium|long|full -->
             <fmt:formatDate value="${date }" type="both" timeStyle="short"/><br/>
             <fmt:formatDate value="${date }" type="both" timeStyle="medium"/><br/>
             <fmt:formatDate value="${date }" type="both" timeStyle="long"/><br/>
             <fmt:formatDate value="${date }" type="both" timeStyle="full"/><br/>
             <!-- 5.日期格式输出  -->
             <fmt:formatDate value="${date }" pattern="yyyy-MM-dd HH:mm:ss"/><br/>
             <fmt:formatDate value="${date }" pattern="yyyy年MM月dd日 HH时mm分ss秒"/><br/>
            
             <!-- 时区 -->
             <%
                 TimeZone tz=TimeZone.getDefault();
                 request.setAttribute("tz", tz);
              %>
              
              <fmt:formatDate value="${date }" timeZone="${tz }" var="st" scope="page"/>
              直接使用:
              ${st}
    </div>

  <!-- 日期处理 -->
      <fmt:parseDate var="t1" pattern="yyyy-MM-dd HH:mm:ss">2015-3-31 10:36:47</fmt:parseDate>
      <fmt:parseDate var="t2" value="2015-3-31 10:36:47" pattern="yyyy-MM-dd HH:mm:ss"/>
      
      <!-- 输出 -->
      1.${t1}
      <br/>
      2.${t2}

七、数字转换的案例

<!-- 数字处理 -->
      <!-- 1.钱-->
      <fmt:formatNumber value="12"/>
      <br/>
      <fmt:formatNumber value="12" type="currency"/>
      <br/>
      <fmt:formatNumber value="12" type="currency" currencySymbol="$"/>
      <fmt:formatNumber value="12" type="currency" currencySymbol="¥"/>
     <br/>
     <%
        //输出所有国家的货币的编码与符号
        Currency china = Currency.getInstance(Locale.CHINA);
        String code = china.getCurrencyCode();
        String symbol= china.getSymbol();
        
        pageContext.setAttribute("code", code);
        pageContext.setAttribute("symbol", symbol);
       
      %>
      <br/>
      编码:${code},符号:${symbol}
      <br/>
      <fmt:formatNumber value="12" type="currency" currencyCode="${code}"/>
      
      
        <%
        //输出所有国家的货币的编码与符号
        Currency riben = Currency.getInstance(Locale.JAPAN);
        String rcode = riben.getCurrencyCode();
        String rsymbol= riben.getSymbol();
        
        pageContext.setAttribute("rcode", rcode);
        pageContext.setAttribute("rsymbol", rsymbol);
       
      %>
      <br/>
      编码:${rcode},符号:${rsymbol}
      <br/>
      <fmt:formatNumber value="12" type="currency" currencyCode="${rcode}" currencySymbol="${rsymbol }"/>
      
      
      <br/>
      <h1>百分比</h1>
        1:<fmt:formatNumber value="0.183856" type="percent" maxIntegerDigits="2" maxFractionDigits="3"/>
        
        <h1>数字</h1>
        2:<fmt:formatNumber value="0.12" type="number" var="n" scope="page"/>
        <br/>
        ${n}
        
      <h1>自定义的格式</h1>
      <!-- 自己查数字的api 看这个格式什么含义 -->
       1:<fmt:formatNumber value="1101.183856" pattern="#,#00.0#"/>
        
     <h2>parseNumber</h2>
     <!-- 把百分比转换成数字 -->
     <fmt:parseNumber value="12%" type="percent"/>
     <br/>
     <fmt:parseNumber value="$12" type="currency" parseLocale="<%=Locale.US %>" var="sn" scope="page"/>
     
     <br/>
     ${sn }

原文地址:https://www.cnblogs.com/ly-radiata/p/4380743.html