zoukankan      html  css  js  c++  java
  • 【JavaWeb】i18n 国际化

    i18n 国际化

    什么是 i18n

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

    希望相同的一个网站,不同人访问的时候可以根据用户所在的区域显示
    不同的语言文字,但是网站的布局样式等不发生改变。

    相关要素

    国际化三要素

    • Local 对象 表示不同的时区,位置,语言
      • zh_CN 中国 中文
      • en_US 美国 英文
    • Properties 属性配置文件 配置文件命名规则为:baseName_local.properties,例如
      • 中文的配置文件名是:i18n_zh_CN.properties
      • 英文的配置文件名是:i18n_en_US.properties
    • ResourceBundle 资源包
      • ResourceBundle.getBundle() 根据给定的 baseName 和 Local 读取相应的配置文件,得到文件内容信息
      • ResourceBundle.getString(key) 得到不同国家的语言信息

    国际化资源使用

    i18n_en_US.properties 文件

    username=username
    password=password
    sex=sex
    age=age
    regist=regist
    boy=boy
    email=email
    girl=girl
    reset=reset
    submit=submit
    

    i18n_zh_CN.properties 文件

    username=用户名
    password=密码
    sex=性别
    age=年龄
    regist=注册
    boy=男
    girl=女
    email=邮箱
    reset=重置
    submit=提交
    
    public class I18nTest {
        @Test
        public void testLocale(){
            // 获取你系统默认的语言。国家信息
            // Locale locale = Locale.getDefault();
            // System.out.println(locale);
            // for (Locale availableLocale : Locale.getAvailableLocales()) {
            // System.out.println(availableLocale);
            // }
            // 获取中文,中文的常量的 Locale 对象
            System.out.println(Locale.CHINA);
            // 获取英文,美国的常量的 Locale 对象
            System.out.println(Locale.US);
        }
    
        @Test
        public void testI18n(){
            // 得到我们需要的 Locale 对象
            Locale locale = Locale.CHINA;
            // 通过指定的 basename 和 Locale 对象,读取 相应的配置文件
            ResourceBundle bundle = ResourceBundle.getBundle("i18n", locale);
            System.out.println("username:" + bundle.getString("username"));
            System.out.println("password:" + bundle.getString("password"));
            System.out.println("Sex:" + bundle.getString("sex"));
            System.out.println("age:" + 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="">中文</a>|
    	<a href="">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>
    

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

    <%@ 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 = 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();
    		}
    		System.out.println(locale);
    // 获取读取包(根据 指定的 baseName 和 Locale 读取 语言信息)
    		ResourceBundle i18n = ResourceBundle.getBundle("i18n", locale);
    	%>
    
    	<a href="i18n.jsp?country=cn">中文</a>|
    	<a href="i18n.jsp?country=usa">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>
    

    JSTL 标签库实现国际化

    • <%--1 使用标签设置 Locale 信息--%> <fmt:setLocale value="" />

    • <%--2 使用标签设置 baseName--%> <fmt:setBundle basename=""/>

    • <%--3 输出指定 key 的国际化信息--%> <fmt:message 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"/>
    
    	<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>
    

    练习和总结

  • 相关阅读:
    使用PrintDocument进行打印
    【转】封装原生JS实现Ajax
    休眠到指定时分秒
    [原创]vscode初体验
    反编译网站
    命令行工具aspnet_regiis.exe实现加密和解密web.config
    Sqlserver内置函数实现MD5
    [转]如何循序渐进向dotnet架构师发展
    [转]高级系统架构师培训笔记
    理解RESTful
  • 原文地址:https://www.cnblogs.com/parzulpan/p/14135718.html
Copyright © 2011-2022 走看看