文章目录
1. 引入Maven依赖
SpringBoot+Thymeleaf实现国际,中英文化切换功能
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>top.fengyulou</groupId>
<artifactId>chuli</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>chuli</name>
<description>SpringBoot国际化</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. resources目录创建i18n文件夹
创建login_zh_CN.properties
、login_en_US.properties
、login.properties
文件,编写国际化配置文件。
3. application.properties添加国际化配置
spring.messages.basename=i18n.login
4. 创建控制器类
package top.fengyulou.chuli.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
/**
* 登录
*/
@Controller
public class LoginController {
@GetMapping("/login")
public String login(){
return "index";
}
}
5. 创建页面文件index.html
使用th:text="#{login.btn}"
获取国际化配置文件的值
6. 启动SpringBoot项目
页面显示效果
7. 中英文切换
1.添加切换按钮
<a class="btn btn-sm" th:href="@{/login(lang='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/login(lang='en_US')}">English</a>
2.创建自定义国际化解析器类,根据lang参数值选择国际化
package top.fengyulou.chuli.config;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
/**
* 自定义国际化解析器
*/
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
String lang = httpServletRequest.getParameter("lang");
//请求参数中没有lang时使用SpringBoot默认的Local对象
Locale locale = Locale.getDefault();
if (!StringUtils.isBlank(lang)) {
String[] split = lang.split("_");
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
3.将自定义解析器注入到Spring容器
package top.fengyulou.chuli;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.LocaleResolver;
import top.fengyulou.chuli.config.MyLocaleResolver;
@SpringBootApplication
public class ChuliApplication {
public static void main(String[] args) {
SpringApplication.run(ChuliApplication.class, args);
}
@Bean
public LocaleResolver localeResolver() {
return new MyLocaleResolver();
}
}
8. 源码地址
https://github.com/ofoo/chuli