Spring Boot 简明教程
Spring Boot - Internationalization
国际化是一个过程,它让你的应用程序能够适应不同的语言和地区,而无需对源代码进行工程更改。换句话说,国际化是本地化的准备就绪。
Internationalization is a process that makes your application adaptable to different languages and regions without engineering changes on the source code. In ither words, Internationalization is a readiness of Localization.
在本章中,我们将详细了解如何在 Spring Boot 中实现国际化。
In this chapter, we are going to learn in detail about How to implement the Internationalization in Spring Boot.
Dependencies
我们需要 Spring Boot Starter Web 和 Spring Boot Starter Thymeleaf 依赖项才能在 Spring Boot 中开发 Web 应用程序。
We need the Spring Boot Starter Web and Spring Boot Starter Thymeleaf dependency to develop a web application in Spring Boot.
LocaleResolver
我们需要确定应用程序的默认语言环境。我们需要在 Spring Boot 应用程序中添加 LocaleResolver bean。
We need to determine default Locale of your application. We need to add the LocaleResolver bean in our Spring Boot application.
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
sessionLocaleResolver.setDefaultLocale(Locale.US);
return sessionLocaleResolver;
}
LocaleChangeInterceptor
LocaleChangeInterceptor 用于根据添加到请求的语言参数的值来更改新语言环境。
LocaleChangeInterceptor is a used to change the new Locale based on the value of the language parameter added to a request.
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
为了达到此效果,我们需要将 LocaleChangeInterceptor 添加到应用程序的注册拦截器中。配置类应该扩展 WebMvcConfigurerAdapter 类并覆盖 addInterceptors() 方法。
To take this effect, we need to add the LocaleChangeInterceptor into the application’s registry interceptor. The configuration class should extend the WebMvcConfigurerAdapter class and override the addInterceptors() method.
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
Messages Sources
Spring Boot 应用程序默认情况下从 classpath 下的 src/main/resources 文件夹获取消息源。默认区域消息文件名应该是 message.properties ,每个区域的文件都应该命名为 messages_XX.properties 。“XX” 表示区域代码。
Spring Boot application by default takes the message sources from src/main/resources folder under the classpath. The default locale message file name should be message.properties and files for each locale should name as messages_XX.properties. The “XX” represents the locale code.
所有消息属性都应该用作键值对。如果区域中找不到任何属性,应用程序将使用 messages.properties 文件中的默认属性。
All the message properties should be used as key pair values. If any properties are not found on the locale, the application uses the default property from messages.properties file.
默认的 messages.properties 将如下所示 −
The default messages.properties will be as shown −
welcome.text=Hi Welcome to Everyone
法语 messages_fr.properties 将如下所示 −
The French language messages_fr.properties will be as shown −
welcome.text=Salut Bienvenue à tous
Note − 消息源文件应该保存为 “UTF-8” 文件格式。
Note − Messages source file should be saved as “UTF-8” file format.
HTML file
在 HTML 文件中,使用语法 #{key} 从属性文件显示消息。
In the HTML file, use the syntax #{key} to display the messages from the properties file.
<h1 th:text = "#{welcome.text}"></h1>
完整的代码如下所示
The complete code is given below
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 应用程序主类文件如下所示 −
The main Spring Boot application class file is given below −
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);
}
}
控制器类文件如下 −
The controller class file is given below −
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";
}
}
国际化配置类
Configuration class to support the Internationalization
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 如下所示 −
The Message sources – messages.properties is as shown −
welcome.text = Hi Welcome to Everyone
消息来源—— message_fr.properties 如下所示 −
The Message sources – message_fr.properties is as shown −
welcome.text = Salut Bienvenue à tous
HTML 文件 locale.html 应放置在类路径模板目录下,如下所示 −
The HTML file locale.html should be placed under the templates directory on the classpath as shown −
<!DOCTYPE html>
<html>
<head>
<meta charset = "ISO-8859-1"/>
<title>Internationalization</title>
</head>
<body>
<h1 th:text = "#{welcome.text}"></h1>
</body>
</html>
你可以创建可执行 JAR 文件,并使用以下 Maven 或 Gradle 命令运行 Spring Boot 应用程序 −
You can create an executable JAR file, and run the Spring boot application by using the following Maven or Gradle commands −
对于 Maven,使用以下命令 −
For Maven, use the following command −
mvn clean install
“BUILD SUCCESS”之后,您可以在目标目录中找到 JAR 文件。
After “BUILD SUCCESS”, you can find the JAR file under the target directory.
对于 Gradle,使用以下命令 −
For Gradle, use the following command −
gradle clean build
“BUILD SUCCESSFUL”之后,您可以在 build/libs 目录中找到 JAR 文件。
After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory.
现在,使用命令运行 JAR 文件,如下所示 −
Now, run the JAR file by using the command as shown −
java –jar <JARFILE>
你将发现应用程序已在 Tomcat 端口 8080 上启动。
You will find that the application has started on the Tomcat port 8080.
现在在你的 Web 浏览器中点击 URL http://localhost:8080/locale ,你将看到以下输出 −
Now hit the URL http://localhost:8080/locale in your web browser and you can see the following output −
URL http://localhost:8080/locale?language=fr 将为你提供以下输出 −
The URL http://localhost:8080/locale?language=fr will give you the output as shown −