i18n国际化

一、什么是 i18n 国际化?

  • 国际化(Internationalization)指的是同一个网站可以支持多种不同的语言,以方便不同国家,不同语种的用户访问。
  • 关于国际化我们想到的最简单的方案就是为不同的国家创建不同的网站,比如苹果公司,他的英文官网是:
    http://www.apple.com 而中国官网是 http://www.apple.com/cn。
  • 苹果公司这种方案并不适合全部公司,而我们希望相同的一个网站,而不同人访问的时候可以根据用户所在的区域显示
    不同的语言文字,而网站的布局样式等不发生改变。
  • 于是就有了我们说的国际化,国际化总的来说就是同一个网站不同国家的人来访问可以显示出不同的语言。但实际上这
    种需求并不强烈,一般真的有国际化需求的公司,主流采用的依然是苹果公司的那种方案,为不同的国家创建不同的页
    面。所以国际化的内容我们了解一下即可。
  • 国际化的英文 Internationalization,但是由于拼写过长,老外想了一个简单的写法叫做 I18N,代表的是 Internationalization
    这个单词,**以 I 开头,以 N 结尾,而中间是 18 个字母,所以简写为 I18N。**以后我们说 I18N 和国际化是一个意思。

二、国际化相关要素介绍

ios 国际化语言本地化 苹果国际化_i18n

三、国际化资源 properties 测试

配置两个语言的配置文件:

i18n_en_US.properties 英文

username=username
password=password
sex=sex
age=age

i18n_zh_CN.properties 中文

username=用户名
password=密码
sex=性别
age=年龄

国际化测试代码:

public void testLocale(){
    //getDefault():获取你系统默认的语言、国家信息
    Locale locale = Locale.getDefault();

    System.out.println(locale);// zh_CN

    //getAvailableLocales():获取支持的语音、国家信息
    //遍历
    for (Locale availableLocale : Locale.getAvailableLocales()) {
        System.out.println(availableLocale);
    }

    //获取中文,中文的常量的Locale对象
    System.out.println(Locale.CANADA);// zh_CN
    //获取英文,美国的常量的Locale对象
    System.out.println(Locale.US);// en_US

}

public void testI18n(){
    //得到需要的local对象
    Locale locale = Locale.CHINA;
    //通过指定的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"));

}

四、通过请求头国际化页面

<%@ 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 locale = request.getLocale();
        System.out.println(locale);
        // 获取读取包(根据 指定的 baseName 和 Locale 读取 语言信息)
        ResourceBundle i18n = ResourceBundle.getBundle("i18n", locale);
       
	%>


	<a href="i18n.jsp?countyr=cn">中文</a>|
	<a href="i18n.jsp?countyr=en">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");%>" />  
				<input type="submit" value="<%i18n.getString("submit");%>" /></td>
			</tr>
			</form>
		</table>
		<br /> <br /> <br /> <br />
	</center>
	国际化测试:
	<br /> 1、访问页面,通过浏览器设置,请求头信息确定国际化语言。
	<br /> 2、通过左上角,手动切换语言
</body>
</html>

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

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

	<%
		//从请求头中获取Local信息(语言)
		Locale locale = null;

		String country = request.getParameter("country");
		if(country=="en"){
			locale = locale.US;
		}else if(country=="cn"){
			locale = locale.CHINA;
		}else{
			locale = request.getLocale();
		}

		//获取读取资源包(根据 指定的baseName和Locale读取语言信息)
		ResourceBundle i18n = ResourceBundle.getBundle("i18n", locale);
		System.out.println(i18n);


	%>


	<a href="i18n.jsp?countyr=cn">中文</a>|
	<a href="i18n.jsp?countyr=en">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");%>" />  
				<input type="submit" value="<%i18n.getString("submit");%>" /></td>
			</tr>
			</form>
		</table>
		<br /> <br /> <br /> <br />
	</center>
	国际化测试:
	<br /> 1、访问页面,通过浏览器设置,请求头信息确定国际化语言。
	<br /> 2、通过左上角,手动切换语言
</body>
</html>

六、JSTL 标签库实现国际化

导入jstl的一系列的jar包

在开头引入

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

1、使用标签设置Locale信息
2、使用标签设置baseName信息
3、使用标签输出国际化信息

<%--1、使用标签设置Locale信息--%>
	<fmt:setLocale value="${param.locale}"> //通过获取浏览器locale的值来调整网页的显示
<%--2、使用标签设置baseName信息--%>
	<fmt:setBundle basename="i18n">
<%--3、使用标签输出国际化信息--%>
	<fmt:message key="xxxx">

完整的代码段:

<%@ 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信息
	2、使用标签设置baseName信息
	3、使用标签输出国际化信息
--%>


<%--1、使用标签设置Locale信息--%>
	<fmt:setLocale value="${param.locale}">
<%--2、使用标签设置baseName信息--%>
	<fmt:setBundle basename="i18n">
<%--3、使用标签输出国际化信息--%>
	<fmt:message key="username">

	<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>
</body>
</html>