国际化和本地化

如何做到国际化的软件,要求:

  1. 软件中存储特定的字符串
  2. 知道浏览器当前使用哪种语言(Locale  )
package com.loaderman.demo.f_i18n;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import org.junit.Test;

public class App {

    // 国际化 - 静态数据
    @Test
    public void testI18N() throws Exception {

        // 中文语言环境
        Locale locale = Locale.US;

        // 创建工具类对象ResourceBundle
        ResourceBundle bundle = ResourceBundle.getBundle("com.loaderman.demo.f_i18n.msg", locale);
        // 根据key获取配置文件中的值
        System.out.println(bundle.getString("hello"));
        System.out.println(bundle.getString("username"));
        System.out.println(bundle.getString("pwd"));

    }

    // 国际化 - 动态文本 - 0. 概述
    @Test
    public void testI18N2() throws Exception {
        // 国际化货币
        NumberFormat.getCurrencyInstance();
        // 国际化数字
        NumberFormat.getNumberInstance();
        // 国际化百分比
        NumberFormat.getPercentInstance();
        // 国际化日期
        //DateFormat.getDateTimeInstance(dateStyle, timeStyle, aLocale)
    }

    // 国际化 - 动态文本 - 1. 国际化货币
    @Test
    public void testI18N3() throws Exception {
        // 模拟语言环境
        Locale locale = Locale.CHINA;
        // 数据准备
        double number = 100;
        // 工具类
        NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
        // 国际化货币
        String m = nf.format(number);
        // 测试
        System.out.println(m);
    }

    //面试题:  代码计算:  $100 * 10
    @Test
    public void eg() throws Exception {
        String str = "$100";
        int num = 10;

        // 1. 分析str值是哪一国家的货币
        Locale us = Locale.US;
        // 2. 国际化工具类
        NumberFormat nf = NumberFormat.getCurrencyInstance(us);
        // 3. 解析str国币
        Number n = nf.parse(str);

        System.out.println(n.intValue() * num);
    }

    // 国际化 - 动态文本 - 2. 国际化数值
    @Test
    public void testI18N4() throws Exception {
        // 模拟语言环境
        Locale locale = Locale.CHINA;
        NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
        String str = nf.format(1000000000);
        System.out.println(str);
    }

    // 国际化 - 动态文本 - 3. 国际化日期
    /*
     * 日期
     *       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 testI18N5() throws Exception {

        // 日期格式
        int dateStyle = DateFormat.SHORT;
        // 时间格式
        int timeStyle = DateFormat.SHORT;

        // 工具类
        DateFormat df =
                DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.CHINA);
        String date = df.format(new Date());

        System.out.println(date);
    }

    // 面试2: 请将时间值:09-11-28 上午10时25分39秒 CST,反向解析成一个date对象。
    @Test
    public void eg2() throws Exception {
        String str = "09-11-28 上午10时25分39秒 CST";
        // 创建DateFormat工具类,国际化日期
        DateFormat df = DateFormat.getDateTimeInstance(
                DateFormat.SHORT, DateFormat.FULL, Locale.getDefault());
        Date d = df.parse(str);

        System.out.println(d);
    }

}

msg.properties

hello=\u4F60\u597D
username=\u7528\u6237\u540D
pwd=\u5BC6\u7801
title=\u767B\u9646\u9875\u9762
submit=\ \u767B\u9646 

msg_en_US.properties

hello=Hello
username=User Name
pwd=Password
title=Login Page
submit=Submit \!

Java提供了一个本地化的对象!封装当前语言、国家、环境等特征!

package com.loaderman.demo.e_locale;

import java.util.Locale;

import org.junit.Test;

public class App {

    @Test
    //1. 本地化对象:Locale
    // 封装语言、国家信息的对象,有java.util提供
    public void testLocale() throws Exception {
        // 模拟中国语言等环境
        //Locale locale = Locale.CHINA;
        Locale locale = Locale.getDefault();            // 当前系统默认的语言环境
        System.out.println(locale.getCountry());           // CN  国家的简称
        System.out.println(locale.getDisplayCountry()); // 国家名称
        System.out.println(locale.getLanguage());        // zh 语言简称

        // 模拟美国国家
        Locale l_us = Locale.US;
        System.out.println(l_us.getCountry());
        System.out.println(l_us.getDisplayCountry());
    }
}

Jsp页面国际化

<%@ 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.loaderman.demo.f_i18n.msg",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>
  <form name="frmLogin" action="${pageContext.request.contextPath }/admin?method=login" method="post">
      <table align="center" border="1">
          <tr>
              <td><%=bundle.getString("username") %></td>
              <td>
                  <input type="text" name="userName">
              </td>
          </tr>
          <tr>
              <td><%=bundle.getString("pwd") %></td>
              <td>
                  <input type="password" name="pwd">
              </td>
          </tr>
          <tr>
              <td>
                  <input type="submit" value="<%=bundle.getString("submit") %>">
              </td>
          </tr>
      </table>
  </form>
  </body>
</html>

Jsp页面国际化 – 使用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}"/>

<%@ 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.loaderman.demo.f_i18n.msg" var="bundle"/>

    <title><fmt:message key="title" bundle="${bundle}"></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>
  <form name="frmLogin" action="${pageContext.request.contextPath }/admin?method=login" method="post">
      <table align="center" border="1">
          <tr>
              <td><fmt:message key="username" bundle="${bundle}"></fmt:message></td>
              <td>
                  <input type="text" name="userName">
              </td>
          </tr>
          <tr>
              <td><fmt:message key="pwd" bundle="${bundle}"></fmt:message></td>
              <td>
                  <input type="password" name="pwd">
              </td>
          </tr>
          <tr>
              <td>
                  <input type="submit" value="<fmt:message key="submit" bundle="${bundle}"/>">
              </td>
          </tr>
      </table>
  </form>
  </body>
</html>

猜你喜欢

转载自www.cnblogs.com/loaderman/p/10020193.html
今日推荐