11.06 SpringBoot 快速支持國際化i18n

學習目標

  • 快速學會如何在工程中支持國際化語言。

— Hey Man,Don't forget to Star or Fork . —

項目結構:

SpringBoot 快速支持國際化i18n

使用教程

一、後臺國際化

1、配置國際化參數

默認解析器:LocaleResolver 用於設置當前會話的默認的國際化語言。

默認攔截器:LocaleChangeInterceptor 指定切換國際化語言的參數名。例如?lang=zh_CN 表示讀取國際化文件messages_zh_CN.properties。

/**
* 配置國際化語言
*/
@Configuration
public class LocaleConfig {
/**
* 默認解析器 其中locale表示默認語言
*/
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.US);
return localeResolver;
}
/**
* 默認攔截器 其中lang表示切換語言的參數名
*/
@Bean
public WebMvcConfigurer localeInterceptor() {
return new WebMvcConfigurer() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
localeInterceptor.setParamName("lang");
registry.addInterceptor(localeInterceptor);
}
};

}
}

2、添加國際化文件

首先在配置文件 application.yml 填寫國際化文件的相對路徑,表示讀取classpath:/static/i18n/messages_language_country.properties 。例如:

spring:
messages:
basename: static/i18n/messages #相對路徑 開頭請勿添加斜槓

然後在 classpath:/static/i18n 目錄中添加如下國際化文件:

默認文件:messages.properties

#這裡填寫默認翻譯,內容可以留空,但文件必須存在。

美式英語:messages_en_US.properties

#這裡填寫英語翻譯。
user.title=User Login
user.welcome=Welcome
user.username=Username
user.password=Password
user.login=Sign In

中文簡體:messages_zh_CN.properties

#這裡填寫中文翻譯
user.title=用戶登陸
user.welcome=歡迎
user.username=登陸用戶
user.password=登陸密碼

user.login=登陸

中文繁體:messages_zh_TW.properties

#這裡填寫繁體翻譯
user.title=用戶登陸
user.welcome=歡迎
user.username=登陸用戶
user.password=登陸密碼
user.login=登陸

3、代碼國際化

通過工具類的靜態方法MessageUtils.get("user.title") 快速獲取當前國際化的翻譯值。



/**
* 國際化工具類
*/
@Component
public class MessageUtils{
private static MessageSource messageSource;
public MessageUtils(MessageSource messageSource) {
FastLocale.messageSource = messageSource;
}
/**
* 獲取單個國際化翻譯值
*/
public static String get(String msgKey) {
try {
return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
} catch (Exception e) {
return msgKey;
}
}

二、頁面國際化

首先在pom文件引入Thymeleaf和Web依賴,然後在頁面中只需通過th:xx="#{x.label}"即可獲取對應的國際化翻譯值。

 <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>

例如:

<title>用戶登陸/<title>

三、JS國際化

首先在pom文件引入jQuery、jquery-properties-i18n等依賴,然後在初始化後即可通過JS函數獲取對應國際化文件的內容。

 <dependency>
<groupid>org.webjars/<groupid>
<artifactid>webjars-locator-core/<artifactid>
/<dependency>
<dependency>
<groupid>org.webjars/<groupid>
<artifactid>jquery/<artifactid>
<version>3.3.1/<version>
/<dependency>
<dependency>

<groupid>org.webjars.bower/<groupid>
<artifactid>jquery-i18n-properties/<artifactid>
<version>1.2.7/<version>
/<dependency>

例如:為了提高可用性 這裡提供了獲取當前國際化語言和獲取國際化翻譯的方法。




<title>用戶登陸/<title>





<select>
<option>中文簡體/<option>
<option>中文繁體/<option>
<option>English/<option>
/<select>

歡迎登陸






關於i18n插件的更多配置項請查閱 jquery-properties-i18n 官方文檔。

四、語言切換

很多新人配置好之後不懂得如何切換國際化語言,其實很簡單,由於在前面已經配置了攔截器LocaleChangeInterceptor ,此時我們只需在任意請求中附上語言參數lang即可,當然也通過AJAX來快速切換。

例如:

默認英語:http://http://127.0.0.1:8080?lang=en_US

中文簡體:http://http://127.0.0.1:8080?lang=zh_CN

中文繁體:http://http://127.0.0.1:8080?lang=zh_TW

五、工程演示

英文界面

SpringBoot 快速支持國際化i18n

中文界面

SpringBoot 快速支持國際化i18n


分享到:


相關文章: