2020年11月18日星期三

技术点18:国际化

i18n 国际化(了解内容)

一、什么是 i18n 国际化?

 

二、国际化相关要素介绍

 

三、国际化资源 properties 测试

配置两个语言的配置文件:i18n_en_US.properties 英文:
username=usernamepassword=passwordsex=sexage=age
i18n_zh_CN.properties 中文:
username=用户名password=密码sex=性别age=年龄
国际化测试代码:
package com.zixue.i18n;import org.junit.Test;import java.util.Locale;import java.util.ResourceBundle;/** * @author Mr Guo * @create 2020-11-18 13:50 */public class I18nTest { @Test public void testLocale(){  //获取系统默认的语言国家信息//  Locale locale = Locale.getDefault();//  System.out.println(locale);//zh_CN  //获取所有国家的语言信息//  for (Locale availableLocale : Locale.getAvailableLocales()){//   System.out.println(availableLocale);//  }  //获取中文,中文的常量的Locale对象  System.out.println(Locale.CHINA);//zh_CN  //获取英文,英文的常量的Locale对象  System.out.println(Locale.US);//en_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();  }  //获取资源包(根据指定的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 标签库实现国际化

 

<%@ taglib prefix="fmt" uri="" %><%@ 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> <fmt:setLocale value="${param.locale}" /> <fmt:setBundle basename="i18n" /> <a href="i18n.jsp?locale=zh_CN">中文</a>| <a href="i18n.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> 国际化测试: <br /> 1、访问页面,通过浏览器设置,请求头信息确定国际化语言。 <br /> 2、通过左上角,手动切换语言</body></html>

 

 

原文转载:http://www.shaoqun.com/a/490423.html

r标:https://www.ikjzd.com/w/1070

rakuten:https://www.ikjzd.com/w/2718

商标抢注:https://www.ikjzd.com/w/1053


i18n国际化(了解内容)一、什么是i18n国际化?二、国际化相关要素介绍三、国际化资源properties测试配置两个语言的配置文件:i18n_en_US.properties英文:username=usernamepassword=passwordsex=sexage=agei18n_zh_CN.properties中文:username=用户名password=密码sex=性别age=年龄国
四海商舟:四海商舟
环球b2b:环球b2b
Postling:Postling
Qiwi:Qiwi
广东首创可反复使7天用医用口罩!一个能用7天:广东首创可反复使7天用医用口罩!一个能用7天

没有评论:

发表评论