前言
在项目开发中,可能遇到国际化的问题,而支持国际化却是一件很头疼的事。但spring boot给出了一个非常理想和方便的方案。
一、准备工作
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>com.example</groupId>
<artifactId>spring-boot-14</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>spring-boot-14</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
pom.xml
App.java:
package com.example; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class App { public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
App.java
创建国际化配置文件:LocaleConfig.java
package com.example; import java.util.Locale; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver; @Configuration
@EnableAutoConfiguration
@ComponentScan
public class LocaleConfig extends WebMvcConfigurerAdapter { @Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
// 默认语言
slr.setDefaultLocale(Locale.US);
return slr;
} @Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
// 参数名
lci.setParamName("lang");
return lci;
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
MainController.java:
package com.example; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; /**
* 控制器 博客出处:http://www.cnblogs.com/GoodHelper/
*
*/
@Controller
public class MainController { @GetMapping("/")
public String index() {
return "index";
} }
在resources目录增加两个properties文件,分别为:
messages_en_US.properties:
hello=hello
messages_zh_CN.properties:
hello=\u4F60\u597D
二、前端调用
在thymeleaf模板引擎中使用#{}的标签就能调用messages中的内容
index.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>玩转spring boot——国际化</title>
</head>
<body>
<h1>玩转spring boot——国际化</h1>
<h4>
<a href="http://www.cnblogs.com/GoodHelper/">from 刘冬的博客</a>
</h4>
<br />
<br />
<a href="/?lang=en_US">English(US)</a>
<a href="/?lang=zh_CN">简体中文</a>
<br /> <h3 th:text="#{hello}"></h3>
<br />
<br />
<a href="http://www.cnblogs.com/GoodHelper/">点击访问原版博客(www.cnblogs.com/GoodHelper)</a>
</body>
</html>
项目结构如下:
运行效果:
三、后端渲染
修改MainController类:
package com.example; import java.util.Locale; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; /**
* 控制器 博客出处:http://www.cnblogs.com/GoodHelper/
*
*/
@Controller
public class MainController { @Autowired
private MessageSource messageSource; @GetMapping("/")
public String index(Model model) {
Locale locale = LocaleContextHolder.getLocale();
model.addAttribute("world", messageSource.getMessage("world", null, locale));
return "index";
} }
其中,MessageSource类可以获取messages的内容。
index.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>玩转spring boot——国际化</title>
</head>
<body>
<h1>玩转spring boot——国际化</h1>
<h4>
<a href="http://www.cnblogs.com/GoodHelper/">from 刘冬的博客</a>
</h4>
<br />
<br />
<a href="/?lang=en_US">English(US)</a>
<a href="/?lang=zh_CN">简体中文</a>
<br /> <h3 th:text="#{hello} + ' , ' + ${world}"></h3>
<br />
<br />
<a href="http://www.cnblogs.com/GoodHelper/">点击访问原版博客(www.cnblogs.com/GoodHelper)</a>
</body>
</html>
一个使用了#{}标签直接调用messages的内容,另一个使用了${}标签来获取后台的值
运行效果:
总结
国际化就已经实现了,然而更完美的做法是当第一次请求时,在后台通过Request获取到一个初始的语言。当获取到的语言和用户所需要的语言不一直时,才需要在前端UI再去设置哪个语言是用户所需要的。
代码下载:https://github.com/carter659/spring-boot-14.git
如果你觉得我的博客对你有帮助,可以给我点儿打赏,左侧微信,右侧支付宝。
有可能就是你的一点打赏会让我的博客写的更好:)