当前位置: 首页 > news >正文

JavaWeb学习|i18n

学习材料声明

所有知识点都来自互联网,进行总结和梳理,侵权必删。
引用来源:尚硅谷最新版JavaWeb全套教程,java web零基础入门完整版

i18n

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

很有趣的命名。
主要是实现多种网页语言的转换。
在这里插入图片描述
需要的配置properties文件
在这里插入图片描述
如何使用:

@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"));
}

1.页面语言修改方式1:获取浏览器的默认语言配置。

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

2.通过页面设置

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

3.利用JSTL标签库实现国际化

<%@ 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>
http://www.lryc.cn/news/299870.html

相关文章:

  • 数据库日志已经很大了,比如200多G,不能收缩到几兆,实际操作过只能到30G
  • docker常用容器命令
  • 蓝桥杯(Web大学组)2022省赛真题:冬奥大抽奖
  • 单调队列 单调栈
  • Java基础-泛型
  • Vue 全组件 局部组件
  • 几个经典金融理论
  • c++语言max函数的使用
  • c++阶梯之类与对象(下)
  • 机器学习--K-近邻算法常见的几种距离算法详解
  • <网络安全>《30 网络信息安全基础(1)常用术语整理》
  • Git远程仓库的使用(Gitee)及相关指令
  • vscode +markdown 的安装和使用
  • Python爬虫之自动化测试Selenium#7
  • 快速学习Spring
  • c语言操作符(上)
  • vue3 可视化大屏自适应屏幕组件
  • SpringCloud入门概述
  • 刷题计划_冲绿名
  • 【微信小程序开发】小程序版的防抖节流应该怎么写
  • 单片机学习笔记---蜂鸣器播放提示音音乐(天空之城)
  • 软件实例分享,茶楼收银软件管理系统,支持计时计费商品销售会员管理定时语音提醒功能
  • clang前端
  • ARM:AI 的翅膀,还能飞多久?
  • 【C语言】常见字符串函数的功能与模拟实现
  • pyGMT初步使用
  • 神经网络 | CNN 与 RNN——深度学习主力军
  • thinkphp6入门(20)-- 如何上传图片、文件
  • 【Linux技术宝典】深入理解Linux基本指令:命令行新手指南
  • C++:Level1阶段测试