【注】本文译自: 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

&emsp;&emsp;在 “BUILD SUCCESS” 之后,你可以在 target 目录下找到 JAR 文件。
&emsp;&emsp;Gradle 可以使用以下命令:
&emsp;&emsp;应用已在 Tomcat 8080 端口启动,如下图所示:
![](http://p1.pstatp.com/origin/pgc-image/bcb8bf9a47ad47c9aed0474e1cfeacb4)
&emsp;&emsp;在浏览器输入 URL http://localhost:8080/locale 可以看到如下输出:
![](http://p1.pstatp.com/origin/pgc-image/e8fe1f3808284242a9621c8af302b353)
URL http://localhost:8080/locale?language=fr 输出如下:

信码由缰
65 声望8 粉丝

“码”界老兵,分享程序人生。