1.编写国际化资源属性文件 2.添加配置文件内容,引入资源属性文件 3.重写localeResolver方法配置语言区域选择 4.创建控制器类I18nTestController 5.创建视图页面,并获得国际化信息 6.运行
src/main/resources目录下创建i18n/admin文件夹,并在该文件夹下创建adminMessages.properties、adminMessages_en_US.properties和adminMessages_zh_CN.properties资源属性文件。adminMessages.properties表示默认加载的信息;adminMessages_en_US.properties表示英文信息(en代表语言代码,US代表国家地区);adminMessages_zh_CN.properties表示中文信息。
src/main/resources目录下创建i18n/before文件夹,并在该文件夹下创建beforeMessages.properties、beforeMessages_en_US.properties和beforeMessages_zh_CN.properties资源属性文件。
src/main/resources目录下创建i18n/common文件夹,并在该文件夹下创建commonMessages.properties、commonMessages_en_US.properties和commonMessages_zh_CN.properties资源属性文件。
应用的配置文件中,添加如下内容,引入资源属性文件。 spring.messages.basename=i18n/admin/adminMessages,i18n/before/beforeMessages, i18n/common/commonMessages
重写localeResolver方法配置语言区域选择 创建配置类LocaleConfig,该配置类实现WebMvcConfigurer接口,并配置语言区域选择。 /** *根据用户本次会话过程中的语义设定语言区域 *(如用户进入首页时选择的语言种类) * @return */ @Bean public LocaleResolver localeResolver() { SessionLocaleResolver slr = new SessionLocaleResolver(); //默认语言 slr.setDefaultLocale(Locale.CHINA); return slr; }
/** * 使用SessionLocaleResolver存储语言区域时, * 必须配置localeChangeInterceptor拦截器 * @return */ @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); //选择语言的参数名 lci.setParamName("locale"); return lci; } /** * 注册拦截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); }
创建控制器类I18nTestController @Controller @RequestMapping("/i18n") public class I18nTestController { @RequestMapping("/first") public String testI18n(){ return "/i18n/first"; } @RequestMapping("/admin") public String admin(){ return "/i18n/admin"; } @RequestMapping("/before") public String before(){ return "/i18n/before"; } }
创建视图页面,并获得国际化信息
应用的src/main/resources/templates目录下,创建文件夹i18n。并在该文件夹中创建admin.html、before.html和first.html视图页面,并在这些视图页面中使用th:text="#{xxx}"获得国际化信息。
<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.dkdkd</groupId> <artifactId>SpringBoot-inter</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <!-- 声明项目配置依赖编码格式为 utf-8 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <fastjson.version>1.2.24</fastjson.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> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
server.servlet.context-path=/ch5_1
spring.messages.basename=i18n/admin/adminMessages,i18n/before/beforeMessages,i18n/common/commonMessages
test.admin=test admin
admin=admin
test.admin=\u6D4B\u8BD5\u540E\u53F0
admin=\u540E\u53F0\u9875\u9762
test.admin=\u6D4B\u8BD5\u540E\u53F0
admin=\u540E\u53F0\u9875\u9762
test.before=test before
before=before
test.before=\u6D4B\u8BD5\u524D\u53F0
before=\u524D\u53F0\u9875\u9762
test.before=\u6D4B\u8BD5\u524D\u53F0
before=\u524D\u53F0\u9875\u9762
chinese.key=chinese
english.key=english
return=return
chinese.key=\u4E2D\u6587\u7248
english.key=\u82F1\u6587\u7248
return=\u8FD4\u56DE\u9996\u9875
chinese.key=\u4E2D\u6587\u7248
english.key=\u82F1\u6587\u7248
return=\u8FD4\u56DE\u9996\u9875
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <a th:href="@{/i18n/first(locale='zh_CN')}" th:text="#{chinese.key}"></a> <a th:href="@{/i18n/first(locale='en_US')}" th:text="#{english.key}"></a> <br> <a th:href="@{/i18n/admin}" th:text="#{test.admin}"></a><br> <a th:href="@{/i18n/before}" th:text="#{test.before}"></a><br> </body> </html>
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <span th:text="#{before}"></span><br> <a th:href="@{/i18n/first}" th:text="#{return}"></a> </body> </html>
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <span th:text="#{admin}"></span><br> <a th:href="@{/i18n/first}" th:text="#{return}"></a> </body> </html>
package com.ch.ch5_1; import java.util.Locale; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Bean; 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.WebMvcConfigurer; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver; @Configuration @EnableAutoConfiguration public class LocaleConfig implements WebMvcConfigurer { /** * 根据用户本次会话过程中的语义设定语言区域 (如用户进入首页时选择的语言种类) * * @return */ @Bean public LocaleResolver localeResolver() { SessionLocaleResolver slr = new SessionLocaleResolver(); // 默认语言 slr.setDefaultLocale(Locale.CHINA); return slr; } /** * 使用SessionLocaleResolver存储语言区域时, 必须配置localeChangeInterceptor拦截器 * * @return */ @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); // 选择语言的参数名 lci.setParamName("locale"); return lci; } /** * 注册拦截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); } }
package com.ch.ch5_1; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Ch51Application { public static void main(String[] args) { SpringApplication.run(Ch51Application.class, args); } }