1. 什么是 i18n 国际化?

QQ截图20200804154339.jpg


2. 国际化相关要素介绍

QQ截图20200804154408.jpg


3. 国际化资源 properties 测试

国际化测试代码:

说明:使用的是Locale这个类,getDefault()获取你所在的对应语言标识,getAvailableLocals()获取所有国家标识,也可以直接选择对应国家标识,如Locale.CHINA是中国。

  1. @Test
  2. public void test01(){
  3. // 获取你系统默认的语言。国家信息
  4. Locale aDefault = Locale.getDefault();
  5. //System.out.println(aDefault);//zh_CN
  6. //遍历所有国家的标识
  7. // for (Locale locale : Locale.getAvailableLocales()) {
  8. // System.out.println(locale);
  9. // }
  10. // 获取中文,中文的常量的 Locale 对象
  11. System.out.println(Locale.CHINA);//zh_CN
  12. // 获取英文,美国的常量的 Locale 对象
  13. System.out.println(Locale.US);//en_US
  14. }

通过配置文件与Local来进行获取

  • 首先创建配置文件

QQ截图20200804160739.jpg
QQ截图20200804160829.jpg

  • 来进行测试

说明:
① 选择对应位置标识,如中国 。
② 使用ResourceBundle.getBundle()将对应整合的名字以及Locale作为参数。
③ 使用getString()方法就能取得对应配置文件的键值对了。

  1. @Test
  2. public void test02(){
  3. //1.选择对应的位置标识
  4. //Locale china = Locale.US;
  5. Locale china = Locale.CHINA;
  6. //2.通过指定的basename与locale对象,读取相应的配置文件
  7. ResourceBundle bundle = ResourceBundle.getBundle("i18n", china);
  8. //3.使用getString()来输出对应properties中的内容
  9. System.out.println(bundle.getString("username"));//用户名
  10. System.out.println(bundle.getString("password"));//密码
  11. System.out.println(bundle.getString("sex"));//性别
  12. System.out.println(bundle.getString("age"));//年龄
  13. }

这里是获取对应zn-CN中的。
**


4. 通过请求头国际化页面

浏览器的请求头说明

QQ截图20200804163313.jpgQQ截图20200804163341.jpg

代码演示:

QQ截图20200804164128.jpg

  1. <%@ page import="java.util.Locale" %>
  2. <%@ page import="java.util.ResourceBundle" %>
  3. <%@ page language="java" contentType="text/html; charset=UTF-8"
  4. pageEncoding="UTF-8"%>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  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. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  12. <title>Insert title here</title>
  13. </head>
  14. <body>
  15. <%
  16. //获取访问页面的用户的位置语言
  17. Locale locale = request.getLocale();
  18. // System.out.println(locale);
  19. //获取资源包
  20. ResourceBundle i18n = ResourceBundle.getBundle("i18n", locale);
  21. %>
  22. <a href="">中文</a>|
  23. <a href="">english</a>
  24. <center>
  25. <h1><%=i18n.getString("regist")%></h1>
  26. <table>
  27. <form>
  28. <tr>
  29. <td><%=i18n.getString("username")%></td>
  30. <td><input name="username" type="text" /></td>
  31. </tr>
  32. <tr>
  33. <td><%=i18n.getString("password")%></td>
  34. <td><input type="password" /></td>
  35. </tr>
  36. <tr>
  37. <td><%=i18n.getString("sex")%></td>
  38. <td><input type="radio" /><%=i18n.getString("boy")%><input type="radio" /><%=i18n.getString("girl")%></td>
  39. </tr>
  40. <tr>
  41. <td><%=i18n.getString("email")%></td>
  42. <td><input type="text" /></td>
  43. </tr>
  44. <tr>
  45. <td colspan="2" align="center">
  46. <input type="reset" value="<%=i18n.getString("reset")%>" />&nbsp;&nbsp;
  47. <input type="submit" value="<%=i18n.getString("submit")%>" /></td>
  48. </tr>
  49. </form>
  50. </table>
  51. <br /> <br /> <br /> <br />
  52. </center>
  53. 国际化测试:
  54. <br /> 1、访问页面,通过浏览器设置,请求头信息确定国际化语言。
  55. <br /> 2、通过左上角,手动切换语言
  56. </body>
  57. </html>

运行结果:
QQ截图20200804164259.jpg


5. 通过显示的选择语言类型进行国际化

说明:就是根据请求参数,再在本页面进行判断进行选择
QQ截图20200804165038.jpg

<%@ 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>
    <%
        //从参数中获取对应国家的语言
        String county = request.getParameter("county");
        Locale locale = null;
        //通过if来判断国家的语言选择
        if("cn".equals(county)){
            locale = Locale.CHINA;
        }else if("us".equals(county)){
            locale = Locale.US;
        }else{
            //如果都不是就根据请求头来
            locale = request.getLocale();
        }
        //获取到资源包
        ResourceBundle i18n = ResourceBundle.getBundle("i18n", locale);
    %>
    <a href="http://localhost:8080/16_JSON_Ajax/i18n.jsp?county=cn">中文</a>|
    <a href="http://localhost:8080/16_JSON_Ajax/i18n.jsp?county=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>

运行结果:
QQ截图20200804164820.jpgQQ截图20200804164842.jpg


6. JSTL 标签库实现国际化

前提说明


首先需要导入包
QQ截图20200804171522.jpg

引入fmt标签库
QQ截图20200804171616.jpg

使用三种标签来进行设置以及获取
QQ截图20200804171505.jpg


**

代码演示


<%@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"/>

    <a href="i18n_fmt.jsp?locale=zh_CN">中文</a>|
    <a href="i18n_fmt.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>

运行结果:
QQ截图20200804171331.jpgQQ截图20200804171356.jpg


整理者:长路 时间:2020/8/4