I18n国际化输出 javaWeb完结

什么是 i18n 国际化?

国际化(Internationalization)指的是同一个网站可以支持多种不同的语言,以方便不同国家,不同语种的用户访问。 

关于国际化我们想到的最简单的方案就是为不同的国家创建不同的网站,比如苹果公司,他的英文官网是: http://www.apple.com 而中国官网是 http://www.apple.com/cn  

苹果公司这种方案并不适合全部公司,而我们希望相同的一个网站,而不同人访问的时候可以根据用户所在的区域显示 不同的语言文字,而网站的布局样式等不发生改变。 

于是就有了我们说的国际化,国际化总的来说就是同一个网站不同国家的人来访问可以显示出不同的语言。但实际上这 种需求并不强烈,一般真的有国际化需求的公司,主流采用的依然是苹果公司的那种方案,为不同的国家创建不同的页 面。所以国际化的内容我们了解一下即可。

 国际化的英文 Internationalization,但是由于拼写过长,老外想了一个简单的写法叫做 I18N,代表的是 Internationalization 这个单词,以 I 开头,以 N 结尾,而中间是 18 个字母,所以简写为 I18N。以后我们说 I18N 和国际化是一个意思

 Locale对象的获取

  没有 Locale locale = new Locale  

  初始化用 Locale locale = Locale.getDefalut(); //获取主机的默认语言

ResourseBundle

也没有对应的new 构造器来创建对象

而是 ResourseBundle bundle = ResourseBundle.getBundle(“baseName”,locale);

这个locale是上面创建的实例

bundle.getString(String key) 通过这个key值来获取国际化信息 

package i18n;

import org.junit.Test;

import java.util.Locale;
import java.util.ResourceBundle;

public class LocalTest {

    @Test
    public void test() {
        Locale locale = Locale.getDefault();
        System.out.println(locale);
        Locale[] locale1 = Locale.getAvailableLocales();
        //获取所有
//        for (Locale locale2 : locale1) {
//            System.out.println(locale2  );
//        }
        //获取中文
        Locale locale2 = Locale.CANADA;
        //获取英文
        Locale locale3 = Locale.US;
        System.out.println(locale2);
        System.out.println(locale3);
    }
    @Test
    public void test1(){
        //得到我们需要的Locale对象
        Locale locale = Locale.US;
        ResourceBundle i18 = ResourceBundle.getBundle("abc", locale);
        String username = i18.getString("username");
        String password = i18.getString("password");
        System.out.println(username);
        System.out.println(password);
    }
}

通过请求头国际化页面

<%@ page import="java.util.Locale" %>
<%@ page import="java.util.ResourceBundle" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="pragma" content="no-cache" />
  <meta http-equiv="cache-control" content="no-cache" />
  <meta http-equiv="Expires" content="0" />
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Insert title here</title>
</head>
<body>
<%
          //获取头信息
//          Locale locale =request.getLocale();
            //默认中文
            Locale locale = request.getLocale();
            //当客户按下指定名字时 切换字体
            String country = request.getParameter("country");
             if("cn".equals(country)){
                locale = Locale.CANADA;
            }else if("us".equals(country)){
              locale = Locale.US;
              }System.out.println(locale);
             //读取配置文件 配置文件的第一个_之前就是baseName
             ResourceBundle i18n = ResourceBundle.getBundle("i18n",locale);
             //用ResourceBundle的实例 xxx.getString 得到配置信息
%>
<%--传参数给服务器告知需要什么语言--%>
<a href="I111.jsp?country=cn">中文</a>|
<a href="I111.jsp?country=us">english</a>
<center>
  <h1><%=i18n.getString("regist")%></h1>
  <table>
    <form>
      <tr>
        <td><%=i18n.getString("username")%></td>
        <td><input name="username" type="text" /></td>
      </tr>
      <tr>
        <td><%=i18n.getString("password")%></td>
        <td><input type="password" /></td>
      </tr>
      <tr>
        <td><%=i18n.getString("sex")%></td>
        <td>
          <input type="radio" /><%=i18n.getString("boy")%>
          <input type="radio" /><%=i18n.getString("girl")%>
        </td>
      </tr>
      <tr>
        <td><%=i18n.getString("email")%></td>
        <td><input type="text" /></td>
      </tr>
      <tr>
        <td colspan="2" align="center">
          <input type="reset" value="<%=i18n.getString("reset")%>" />&nbsp;&nbsp;
          <input type="submit" value="<%=i18n.getString("submit")%>" /></td>
      </tr>
    </form>
  </table>
  <br /> <br /> <br /> <br />
</center>
国际化测试:
<br /> 1、访问页面,通过浏览器设置,请求头信息确定国际化语言。
<br /> 2、通过左上角,手动切换语言
</body>
</html>

使用JSML来实现国际化

<%--1 使用标签设置Locale信息--%>
<fmt:setLocale value="${param.locale}" />
<%--2 使用标签设置baseName--%>
<fmt:setBundle basename="i18n"/>
<%--使用标签 <fmt:message key="key名">输出国际化信息--%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="pragma" content="no-cache" />
    <meta http-equiv="cache-control" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
</head>
<body>
<%--1 使用标签设置Locale信息--%>
<fmt:setLocale value="${param.locale}" />
<%--2 使用标签设置baseName--%>
<fmt:setBundle basename="i18n"/>
<%--使用标签 <fmt:message key="key名">输出国际化信息--%>

<a href="JsmlI18n.jsp?locale=zh_CN">中文</a>|
<a href="JsmlI18n.jsp?locale=en_US">english</a>
<center>
    <h1><fmt:message key="regist" /></h1>
    <table>
        <form>
            <tr>
                <td><fmt:message key="username" /></td>
                <td><input name="username" type="text" /></td>
            </tr>
            <tr>
                <td><fmt:message key="password" /></td>
                <td><input type="password" /></td>
            </tr>
            <tr>
                <td><fmt:message key="sex" /></td>
                <td>
                    <input type="radio" /><fmt:message key="boy" />
                    <input type="radio" /><fmt:message key="girl" />
                </td>
            </tr>
            <tr>
                <td><fmt:message key="email" /></td>
                <td><input type="text" /></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="reset" value="<fmt:message key="reset" />" />&nbsp;&nbsp;
                    <input type="submit" value="<fmt:message key="submit" />" /></td>
            </tr>
        </form>
    </table>
    <br /> <br /> <br /> <br />
</center>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hyong~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值