《i18n国际化》

 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")%>" />&nbsp;&nbsp;
				<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"/>" />&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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,i18n国际化HTML是指通过使用i18n插件和相关工具,生成多语言版本的HTML文件。这样可以方便地在不同语言环境下展示网页内容。具体步骤如下: 1. 创建并封装language.js文件:在该文件中定义了网站支持的语言种类,并设置默认语言为繁体中文。通过使用$.i18n.properties方法,加载对应语言的资源文件,并将文本、value属性和placeholder属性进行替换。 2. 在HTML文件中引入language.js文件和相关依赖库:在HTML文件的<head>标签内引入jquery.js和jquery.i18n.properties-min.js等依赖库。然后在<body>标签内,通过使用i18n属性、i18n-v属性和i18n-p属性来标记需要国际化的文本、value值和placeholder属性。同时,可以通过按钮点击事件来切换不同的语言版本。 3. 使用i18nProp函数获取文本:在<script>标签内,可以使用i18nProp函数来获取对应的国际化文本。 总结来说,i18n国际化HTML是通过使用i18n插件和相关工具,在网页中实现多语言版本的生成和展示。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [HTML——国际化实现](https://blog.csdn.net/qq812457115/article/details/126191259)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [webpack-static-i18n-plugin:一个Webpack插件,实现了node-static-i18n来生成国际化HTML文件](https://download.csdn.net/download/weixin_42165980/18554275)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值