Spring Boot 教程:國際化
阿新 • • 發佈:2020-09-10
【注】本文譯自: https://www.tutorialspoint.com/spring_boot/spring_boot_internationalization.htm
國際化是一種處理機制,使得你的應用可以適配不同的語言和區域而無需更改原始碼。換言之,國際化是為本地化而準備。
本文將帶你學習在Spring Boot 中如何實現國際化的細節。
依賴
在Spring Boot 中開發 web 應用,我們需要依賴Spring Boot Starter Web 和 Spring Boot Starter Thymeleaf 依賴項。Maven
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency> 5 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-thymeleaf</artifactId> 9 </dependency>Gradle
Gradle
1 compile('org.springframework.boot:spring-boot-starter-web') 2 compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
LocaleResolver
要確定你的應用的預設本地化,需要在 Spring Boot 應用中 增加LocaleResolver bean。1 @Bean 2 public LocaleResolver localeResolver() { 3 SessionLocaleResolver sessionLocaleResolver = newSessionLocaleResolver(); 4 sessionLocaleResolver.setDefaultLocale(Locale.US); 5 return sessionLocaleResolver; 6 }
LocaleChangeInterceptor
LocaleChangeInterceptor 用於根據請求所帶的語言引數來切換新的本地化。1 @Bean 2 public LocaleChangeInterceptor localeChangeInterceptor() { 3 LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); 4 localeChangeInterceptor.setParamName("language"); 5 return localeChangeInterceptor; 6 }要把LocaleChangeInterceptor 加到應用註冊攔截器中才能生效。這個配置類應當繼承 WebMvcConfigurerAdapter 類且過載 addInterceptors() 方法。 @Override
1 public void addInterceptors(InterceptorRegistry registry) { 2 registry.addInterceptor(localeChangeInterceptor()); 3 }
訊息資源
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
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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 3 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>com.tutorialspoint</groupId> 6 <artifactId>demo</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>jar</packaging> 9 <name>demo</name> 10 <description>Demo project for Spring Boot</description> 11 12 <parent> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-parent</artifactId> 15 <version>1.5.8.RELEASE</version> 16 <relativePath /> 17 </parent> 18 19 <properties> 20 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 21 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 22 <java.version>1.8</java.version> 23 </properties> 24 25 <dependencies> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-web</artifactId> 29 </dependency> 30 31 <dependency> 32 <groupId>org.springframework.boot</groupId> 33 <artifactId>spring-boot-starter-test</artifactId> 34 <scope>test</scope> 35 </dependency> 36 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-thymeleaf</artifactId> 40 </dependency> 41 </dependencies> 42 43 <build> 44 <plugins> 45 <plugin> 46 <groupId>org.springframework.boot</groupId> 47 <artifactId>spring-boot-maven-plugin</artifactId> 48 </plugin> 49 </plugins> 50 </build> 51 </project>
Gradle – build.gradle
1 buildscript { 2 ext { 3 springBootVersion = '1.5.8.RELEASE' 4 } 5 repositories { 6 mavenCentral() 7 } 8 dependencies { 9 classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 } 11 } 12 apply plugin: 'java' 13 apply plugin: 'eclipse' 14 apply plugin: 'org.springframework.boot' 15 16 group = 'com.tutorialspoint' 17 version = '0.0.1-SNAPSHOT' 18 sourceCompatibility = 1.8 19 20 repositories { 21 mavenCentral() 22 } 23 dependencies { 24 compile('org.springframework.boot:spring-boot-starter-web') 25 compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf' 26 testCompile('org.springframework.boot:spring-boot-starter-test') 27 }主 Spring Boot 應用類檔案如下:
1 package com.tutorialspoint.demo; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class DemoApplication { 8 public static void main(String[] args) { 9 SpringApplication.run(DemoApplication.class, args); 10 } 11 }控制類檔案如下:
1 package com.tutorialspoint.demo.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @Controller 7 public class ViewController { 8 @RequestMapping("/locale") 9 public String locale() { 10 return "locale"; 11 } 12 }支援國際化的配置類:
1 package com.tutorialspoint.demo; 2 3 import java.util.Locale; 4 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.web.servlet.LocaleResolver; 8 import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 9 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 10 import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; 11 import org.springframework.web.servlet.i18n.SessionLocaleResolver; 12 13 @Configuration 14 public class Internationalization extends WebMvcConfigurerAdapter { 15 @Bean 16 public LocaleResolver localeResolver() { 17 SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver(); 18 sessionLocaleResolver.setDefaultLocale(Locale.US); 19 return sessionLocaleResolver; 20 } 21 @Bean 22 public LocaleChangeInterceptor localeChangeInterceptor() { 23 LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); 24 localeChangeInterceptor.setParamName("language"); 25 return localeChangeInterceptor; 26 } 27 @Override 28 public void addInterceptors(InterceptorRegistry registry) { 29 registry.addInterceptor(localeChangeInterceptor()); 30 } 31 }訊息源 – messages.properties 如下:
welcome.text = Hi Welcome to Everyone訊息源 – message_fr.properties 檔案如下:
welcome.text = Salut Bienvenue à tousHTML 檔案 locale.html 應當放在 classpath 路徑下的 templates 目錄,如下所示:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset = "ISO-8859-1"/> 5 <title>Internationalization</title> 6 </head> 7 <body> 8 <h1 th:text = "#{welcome.text}"></h1> 9 </body> 10 </html>使用 Maven 或 Gradle 命令建立可執行JAR 檔案並執行 Spring Boot 應用: Maven 命令如下:
mvn clean install
在 “BUILD SUCCESS” 之後,你可以在 target 目錄下找到 JAR 檔案。
Gradle 可以使用以下命令:
gradle clean build在 “BUILD SUCCESSFUL” 之後,你可以在build/libs 目錄下找到JAR 檔案。 現在,使用以下命令執行 JAR 檔案: java –jar <JARFILE> 應用已在Tomcat 8080 埠啟動,如下圖所示: 在瀏覽器輸入 URLhttp://localhost:8080/locale可以看到如下輸出: URLhttp://localhost:8080/locale?language=fr輸出如下: