本周介绍SpringBoot项目的国际化是如何处理的,阅读本章前请阅读【SpringBoot】SpringBoot与Thymeleaf模版(六)的相关内容

国际化原理

  1、在Spring中有国际化Locale(区域信息对象);LocaleResolver(获取区域信息对象,区域信息解析器),2个重要的对象,通过区域信息解析器就能自动给页面绑定不同的数据内容。

  2、查看SpringBoot项目中,WebMvcAutoConfiguration类中有注入区域信息解析器AcceptHeaderLocaleResolver

 1 // 注入区域信息解析器
 2 @Bean
 3 @ConditionalOnMissingBean
 4 @ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
 5 public LocaleResolver localeResolver() {
 6     if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
 7         return new FixedLocaleResolver(this.mvcProperties.getLocale());
 8     }
 9     // 默认注入AcceptHeaderLocaleResolver区域信息解析器
10     AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
11     localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
12     return localeResolver;
13 }

  3、查看AcceptHeaderLocaleResolver.java源码,它返回的区域信息对象,是从请求头获取 语言信息

 1 @Override
 2 public Locale resolveLocale(HttpServletRequest request) {
 3     Locale defaultLocale = getDefaultLocale();
 4     if (defaultLocale != null && request.getHeader("Accept-Language") == null) {
 5         return defaultLocale;
 6     }
 7     // 从请求中获取 语言类型
 8     Locale requestLocale = request.getLocale();
 9     List<Locale> supportedLocales = getSupportedLocales();
10     if (supportedLocales.isEmpty() || supportedLocales.contains(requestLocale)) {
11         return requestLocale;
12     }
13     Locale supportedLocale = findSupportedLocale(request, supportedLocales);
14     if (supportedLocale != null) {
15         return supportedLocale;
16     }
17     return (defaultLocale != null ? defaultLocale : requestLocale);
18 }

  4、F12查看浏览器请求,发现所有请求都会带上语言信息

    【SpringBoot】SpringBoot 国际化(七)

国际化开发

  1、新建一个SpringBoot的web项目。pom.xml文件如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.test</groupId>
 8     <artifactId>test-springboot-i18n</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>2.1.8.RELEASE</version>
15     </parent>
16 
17     <properties>
18 
19         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21         <java.version>1.8</java.version>
22     </properties>
23 
24     <dependencies>
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-web</artifactId>
28         </dependency>
29 
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter-thymeleaf</artifactId>
33         </dependency>
34 
35         <dependency>
36             <groupId>org.springframework.boot</groupId>
37             <artifactId>spring-boot-starter-test</artifactId>
38             <scope>test</scope>
39         </dependency>
40 
41     </dependencies>
42 
43 
44     <!-- SpringBoot打包插件,可以将代码打包成一个可执行的jar包 -->
45     <build>
46         <plugins>
47             <plugin>
48                 <groupId>org.springframework.boot</groupId>
49                 <artifactId>spring-boot-maven-plugin</artifactId>
50             </plugin>
51         </plugins>
52     </build>
53 </project>
View Code 

相关文章: