1、什么是i18n国际化?
2、国际化相关要素介绍
3、i18n国际化基础示例
public void testI18n(){
//得到我们需要的locale对象
Locale locale = Locale.US;
//通过相应的Basename对象和Locale对象,读取相应的配置文件
ResourceBundle bundle = ResourceBundle.getBundle("i18n",locale);
System.out.println("用户名:"+bundle.getString("username"));
System.out.println("密 码:"+bundle.getString("password"));
System.out.println("性 别:"+bundle.getString("sex"));
System.out.println("年 龄:"+bundle.getString("age"));
}
结果
4、页面国际化的实现
<%@ 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 = null;
String country = request.getParameter("country");
if("cn".equals(country)){
locale=Locale.CHINA;
}else if("usa".equals(country)){
locale=Locale.US;
}else {
locale=request.getLocale();
}
ResourceBundle bundle = ResourceBundle.getBundle("i18n",locale);
%>
<a href="i18n.jsp?country=cn">中文</a>|
<a href="i18n.jsp?country=usa">English</a>
<center>
<h1><%=bundle.getString("regist")%></h1>
<table>
<form>
<tr>
<td><%=bundle.getString("username")%></td>
<td><input name="username" type="text" /></td>
</tr>
<tr>
<td><%=bundle.getString("password")%></td>
<td><input type="password" /></td>
</tr>
<tr>
<td><%=bundle.getString("sex")%></td>
<td><input type="radio" /><%=bundle.getString("boy")%><input type="radio" /><%=bundle.getString("girl")%></td>
</tr>
<tr>
<td><%=bundle.getString("email")%></td>
<td><input type="text" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="reset" value="<%=bundle.getString("reset")%>" />
<input type="submit" value="<%=bundle.getString("submit")%>" /></td>
</tr>
</form>
</table>
<br /> <br /> <br /> <br />
</center>
国际化测试:
<br /> 1、访问页面,通过浏览器设置,请求头信息确定国际化语言。
<br /> 2、通过左上角,手动切换语言
</body>
</html>
结果
点击中文
点击English
5、使用JSTL标签库fmt实现国际化
首先引入jar包
<%@ page import="java.util.Locale" %>
<%@ page import="java.util.ResourceBundle" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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信息
2、使用标签设置basename
3、使用标签输出国际化信息
--%>
<%--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"/>" />
<input type="submit" value="<fmt:message key="submit"/>" /></td>
</tr>
</form>
</table>
<br /> <br /> <br /> <br />
</center>
国际化测试:
<br /> 1、访问页面,通过浏览器设置,请求头信息确定国际化语言。
<br /> 2、通过左上角,手动切换语言
</body>
</html>