目录
1、建立语言包,如下图所示
2、对语言properties添加数据
res.properties默认语言,当没有进行调试时默认执行里面的数据
title=国际化语言
res_zh_CN中文
title=国际化语言
res_en_US美式英文
title=International language
3、实验效果
方法一
缺点:需要调试浏览器的语言才能进行语言变更,一般浏览器只支持中英两种语言,如果你设置的有日、韩等语言就需要浏览器下载相应的语言才行
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2022/3/9
Time: 19:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%--注:不能少--%>
<fmt:bundle basename="res"> <%--注:不能少--%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>标题</title>
</head>
<body>
<h1>国际化项目</h1>
<fmt:message key="title"></fmt:message> <%--注:不能少--%>
</body>
</html>
</fmt:bundle> <%--注:不能少--%>
结果图
切换语言,需要调试浏览器语言,步骤如下
重新运行的结果如下
方法二
优点:可以自由切换语言,不需要浏览器的支持,代码下:
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2022/3/9
Time: 19:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %><%--注:此行不能少--%>
<fmt:setLocale value="${param.lang}"/><%--注:此行不能少--%>
<fmt:bundle basename="res"><%--注:此行不能少--%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>标题</title>
</head>
<body>
<h1>国际化项目</h1>
<form action="" method="get" onchange="this.submit()">
<select name="lang">
<option value="zh_CN">选择语言</option>
<option value="zh_CN">简单中文</option>
<option value="en_US">美国英语</option>
</select></form>
<fmt:message key="title"></fmt:message>
</body>
</html>
</fmt:bundle><%--注:此行不能少--%>
常见问题
1、中文显示出现乱码
2、其他问题可以通过配置pom.xml试试
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.wzt</groupId>
<artifactId>web03</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<testSourceDirectory>src/test/java</testSourceDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<!-- 处理无法加载资源配置文件 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>