test1:
- public void test1() {
- // 获取 中国,中文 常量的Locale对象
- System.out.println(Locale.CHINA); //zh_CN
-
- // 获取 英文,美国 常量的Local对象
- System.out.println(Locale.US); //en_US
-
- // 获取你系统默认的 语言,国家信息
- Locale aDefault = Locale.getDefault();
- System.out.println(aDefault); //zh_CN
-
- //获取所有可用的 语言,国家信息
- for (Locale availableLocale : Locale.getAvailableLocales()) {
- System.out.println(availableLocale);
- }
-
- }
- username=username
- password=password
- age=age
- sex=sex
- boy=boy
- girl=girl
- email=email
- regist=regist
- reset=reset
- submit=submit
- username=用户名
- password=密码
- age=年龄
- sex=性别
- boy=男
- girl=女
- email=邮箱
- regist=注册
- reset=重置
- submit=提交
test2 国际化测试代码:
- public void test2() {
- // 得到我们需要的 Locale 对象
- Locale china = Locale.CHINA;
- // 通过指定的 basename 和 Locale 对象,读取 相应的配置文件
- ResourceBundle bundle = ResourceBundle.getBundle("i18n", china);
- System.out.println("username="+bundle.getString("username"));
- System.out.println("password="+bundle.getString("password"));
- System.out.println("age="+bundle.getString("age"));
- System.out.println("sex="+bundle.getString("sex"));
- System.out.println("boy="+bundle.getString("boy"));
- System.out.println("girl="+bundle.getString("girl"));
- System.out.println("email="+bundle.getString("email"));
- System.out.println("submit="+bundle.getString("submit"));
- System.out.println("reset="+bundle.getString("reset"));
- System.out.println("regist="+bundle.getString("regist"));
-
- }
结果:

- <%@ page import="java.util.Locale" %>
- <%@ page import="java.util.ResourceBundle" %>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- 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 heretitle>
- head>
- <body>
- <%
- // 从请求头中获取 Locale 信息(语言)
- Locale locale = null;
- String country = request.getParameter("country");
- if ("cn".equals(country)) {
- locale = Locale.CHINA;
- } else if ("us".equals(country)) {
- locale = Locale.US;
- } else {
- locale = request.getLocale();
- }
- // 获取读取包(根据 指定的 baseName 和 Locale 读取 语言信息)
- ResourceBundle i18n = ResourceBundle.getBundle("i18n", locale);
- %>
- <a href="i18n.jsp?country=cn">中文a>|
- <a href="i18n.jsp?country=us">englisha>
- <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") %>" />
- <input type="submit" value="<%= i18n.getString("submit") %>" />td>
- tr>
- form>
- table>
- <br /> <br /> <br /> <br />
- center>
- 国际化测试:
- <br /> 1、访问页面,通过浏览器设置,请求头信息确定国际化语言。
- <br /> 2、通过左上角,手动切换语言
- body>
- html>
结果:

也可以在浏览器中设置默认语言进行切换。
<%--1 使用标签设置 Locale 信息--%><fmt:setLocale value="" /><%--2 使用标签设置 baseName--%><fmt:setBundle basename=""/><%--3 输出指定 key 的国际化信息--%><fmt:message key="" />
2. 代码示例:
- <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8" %>
- 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 heretitle>
- head>
- <body>
- <%--1 使用标签设置 Locale 信息 <fmt:setLocale value="" />--%>
- <fmt:setLocale value="${param.locale}"/>
- <a href="i18n_fmt.jsp?locale=zh_CN">中文a>|
- <a href="i18n_fmt.jsp?locale=en_US">englisha>
- <%--2 使用标签设置 baseName <fmt:setBundle basename=""/>--%>
- <fmt:setBundle basename="i18n"/>
-
- <%--3 输出指定 key 的国际化信息 <fmt:message key="" /> --%>
- <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="
reset "/>"/> - <input type="submit" value="
submit "/>"/>td> - tr>
- form>
- table>
- <br/> <br/> <br/> <br/>
- center>
- body>
- html>
结果同上结果