共计 6144 个字符,预计需要花费 16 分钟才能阅读完成。
【注】本文译自:https://www.tutorialspoint.com/spring_boot/spring_boot_internationalization.htm
国际化是一种解决机制,使得你的利用能够适配不同的语言和区域而无需更改源代码。换言之,国际化是为本地化而筹备。
本文将带你学习在 Spring Boot 中如何实现国际化的细节。
依赖
在 Spring Boot 中开发 web 利用,咱们须要依赖 Spring Boot Starter Web 和 Spring Boot Starter Thymeleaf 依赖项。
Maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Gradle
compile('org.springframework.boot:spring-boot-starter-web')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
LocaleResolver
要确定你的利用的缺省本地化,须要在 Spring Boot 利用中 减少 LocaleResolver bean。
@Bean
public LocaleResolver localeResolver() {SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
sessionLocaleResolver.setDefaultLocale(Locale.US);
return sessionLocaleResolver;
}
LocaleChangeInterceptor
LocaleChangeInterceptor 用于依据申请所带的语言参数来切换新的本地化。
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
要把 LocaleChangeInterceptor 加到利用注册拦截器中能力失效。这个配置类该当继承 WebMvcConfigurerAdapter 类且重载 addInterceptors() 办法。
音讯资源
Spring Boot 利用默认从 classpath 门路下的 src/main/resources 文件夹获取音讯源。缺省本地化音讯文件名该当是 message.properties,每个本地化文件名该当相似 messages_XX.properties。“XX”代表本地化代号。
所有的音讯属性该当以键值形式表白。如果任意属性没有在本地化中找到,利用就从 messages.properties 文件中获取缺省值。
缺省 messages.properties 如下所示:
welcome.text=Hi Welcome to Everyone
法语 messages_cn.properties 如下所示:
welcome.text=Salut Bienvenue à tous
留神 :音讯源文件该当以“UTF-8”格局存储。
HTML 文件
在 HTML 文件中,应用 #{key} 语法来显示属性文件中获取的音讯。
<h1 th:text = "#{welcome.text}"></h1>
残缺的代码如下:
Maven – 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.tutorialspoint</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath />
</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-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-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Gradle – build.gradle
buildscript {
ext {springBootVersion = '1.5.8.RELEASE'}
repositories {mavenCentral()
}
dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {mavenCentral()
}
dependencies {compile('org.springframework.boot:spring-boot-starter-web')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
主 Spring Boot 利用类文件如下:
package com.tutorialspoint.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);
}
}
管制类文件如下:
package com.tutorialspoint.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ViewController {@RequestMapping("/locale")
public String locale() {return "locale";}
}
反对国际化的配置类:
package com.tutorialspoint.demo;
import java.util.Locale;
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.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
public class Internationalization extends WebMvcConfigurerAdapter {
@Bean
public LocaleResolver localeResolver() {SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
sessionLocaleResolver.setDefaultLocale(Locale.US);
return sessionLocaleResolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(localeChangeInterceptor());
}
}
音讯源 – messages.properties 如下:
welcome.text = Hi Welcome to Everyone
音讯源 – message_fr.properties 文件如下:
welcome.text = Salut Bienvenue à tous
HTML 文件 locale.html 该当放在 classpath 门路下的 templates 目录,如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset = "ISO-8859-1"/>
<title>Internationalization</title>
</head>
<body>
<h1 th:text = "#{welcome.text}"></h1>
</body>
</html>
应用 Maven 或 Gradle 命令创立可执行 JAR 文件并运行 Spring Boot 利用:Maven 命令如下:
mvn clean install
   在“BUILD SUCCESS”之后,你能够在 target 目录下找到 JAR 文件。  Gradle 能够应用以下命令:   利用已在 Tomcat 8080 端口启动,如下图所示:![](http://p1.pstatp.com/origin/pgc-image/bcb8bf9a47ad47c9aed0474e1cfeacb4)
   在浏览器输出 URL http://localhost:8080/locale 能够看到如下输入:![](http://p1.pstatp.com/origin/pgc-image/e8fe1f3808284242a9621c8af302b353)
URL http://localhost:8080/locale?language=fr 输入如下: