前言
这是我的第一篇博客,也是复制大神刘东的代码。大神说:在项目开发中,可能遇到需要国际化,而支持国际化确是一件很头疼的事,但是spring boot给出了一个非常理想和方便的方案。
一、准备工作
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.example</groupId> 7 <artifactId>spring-boot-14</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>spring-boot-14</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>1.5.3.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 </properties> 26 27 <dependencies> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-thymeleaf</artifactId> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-web</artifactId> 35 </dependency> 36 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-devtools</artifactId> 40 <scope>runtime</scope> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-starter-test</artifactId> 45 <scope>test</scope> 46 </dependency> 47 </dependencies> 48 49 <build> 50 <plugins> 51 <plugin> 52 <groupId>org.springframework.boot</groupId> 53 <artifactId>spring-boot-maven-plugin</artifactId> 54 </plugin> 55 </plugins> 56 </build> 57 58 59 </project>
application.properties
spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.cache=false spring.messages.basename=i18n/messages
SpringBootLanguageApplication.java
package com.language; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootLanguageApplication { public static void main(String[] args) { SpringApplication.run(SpringBootLanguageApplication.class, args); } }
创建国际化配置文件:LocaleConfig.java
package com.language.config; 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.CHINESE); 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
@GetMapping("/index") public String index() { System.out.println("============国际化============="); return "index"; }
在resources目录增加两个properties文件,分别为:
messages_en_US.properties:
welcome= welcome to login to alibabawebsite(English)
messages_zh_CN.properties:
welcome = \u6B22\u8FCE\u4F60\u767B\u5F55\u5230 \u963F\u91CC\u5DF4\u5DF4\u7F51\u7AD9\uFF08\u4E2D\u6587\uFF09
二、前端调用
在thymeleaf模板引擎中使用#{}的标签就能调用messages中的内容了
index.html:
<!DOCTYPE html><html><head><meta 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 /> <p><label th:text="#{welcome}"></label></p> </body></html>
三、附上项目结构目录
时间: 2024-10-12 13:54:32