Spring Boot學習筆記(8)——國際化信息

SprintBoot國際化步驟

1、編寫國際化配置文件,將需要國際化顯示的內容寫在配置文件中
1)、在類路徑下面,創建一個文件夾:i18n
2)、在文件夾 i18n 中創建一個默認的國際化文件:login.properties(文件名可以任意,但是必須是properties文件),我的內容如下:

#默認的國際化配置文件
login.username=用戶名_
login.password=密碼
login.remember=記住我
login.reset=重置
login.submit=登錄

3)、創建國際化的配置文件,命名方式爲:默認配置文件名_語言代碼_國家代碼.properties,如中文配置文件:login_zh_CH.properties;內容於默認配置文件一直,但是,參數值要是配置的語言,如英文的配置文件名:login_en_US.properties,文件內容爲:

#英文的國際化配置文件
login.username=Username
login.password=Password
login.remember=Remember
login.reset=Reset
login.submit=Sign

2、Spring Boot已經自動配置了管理國際化資源文件的組件:MessageSourceAutoConfiguration,源碼如下:

public class MessageSourceAutoConfiguration {
	@Bean
	@ConfigurationProperties(prefix = "spring.messages")
	public MessageSourceProperties messageSourceProperties() {
		return new MessageSourceProperties();
	} 
	@Bean
	public MessageSource messageSource() {
		// 國際化資源相關屬性
		MessageSourceProperties properties = this.messageSourceProperties();
		//管理國際化資源的組件
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
}
public class MessageSourceProperties { 
	private String basename = "messages"; 
	//默認國際化資源文件的基礎名(就是去掉 語言_國家代碼 之後的名稱,上面自定義的是login)
	//即 如果我們定義爲 messages.properties 就可以放在類路徑下,就可不做任何配置,
	//就會被直接被加載
	if (StringUtils.hasText(properties.getBasename())) {
		//設置國際化資源文件的基礎名(就是去掉 語言_國家代碼 之後的名稱,自定義的就是login)
		messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(StringUt
			ils.trimAllWhitespace(properties.getBasename())));
}

通過對源碼的粗略分析:如果國際化配置文件的基礎名(也就是默認語言配置文件,這裏是login.properties)是 message,則可以將國際化配置文件放在類路徑下面,不需要做任何配置;如果基礎名不是 message,則可以將配置文件放在類路徑下的任意位置,但是需要在全局配置文件application.properties 中聲明,例如:

spring.messages.basename=i18n.login

3、修改頁面內容,使用 #{} 符號來獲取國際化配置文件中的國際化值,這裏是 springboot 通過瀏覽器的url 請求頭(header)中的 Accept-Language 的值,來決定使用的是哪個國際化配置文件中的值
在這裏插入圖片描述
頁面修改如下:
在這裏插入圖片描述

自定義區域解析器動態切換國際化語言

1、給頁面中的 標籤修改:

<div style="margin-left: 100px;">
	<!-- 使用 @{} 來表示url時,傳參數不能用 ? 號,在url後面使用 () ,參數放在括號裏面,如果有多個參數,用逗號隔開,會自動轉換成 ?xx=xx&xx=xx -->
	<a href="#" th:href="@{/index.html(l='zh_CN')}">中文</a>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 	<a href="#" th:href="@{/index.html(l='en_US')}">English</a>
</div>

2、自定義一個區域解析器

package com.dss.springboot.component;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

/**
 * 自定義區域解析器來切換國際化信息
 * @author asong
 *
 */
public class MyLocaleResolver implements LocaleResolver{

	/**
	 * 解析區域信息
	 */
	@Override
	public Locale resolveLocale(HttpServletRequest request) {
		//獲取請求頭中的 l 參數
		String l = request.getParameter("l");
		
		//獲取瀏覽器上發送來的請求頭中的區域信息,也就是獲取請求頭中的 Accept-Language 的值,默認是 zh-CN,zh;q=0.9,表示中文
		Locale locale = request.getLocale();
		
		//當傳來的 l 有值時,表示用根據傳來的參數來切換國際化信息
		if(!StringUtils.isEmpty(l)) {
			String[] split = l.split("_");
			//創建一個區域信息,參數一:語言代碼,參數二:國家代碼
			locale = new Locale(split[0], split[1]);
		}
		
		return locale;
	}

	@Override
	public void setLocale(HttpServletRequest arg0, HttpServletResponse arg1, Locale arg2) {
		
	}

}

3、將自定義的區域解析器,在配置類中 ,通過 @Bean註解的方法,向spring容器中注入組件

/**
* 需要通過配置類,來往 spring 容器中注入一個 LocaleResolver 組件,在springboot中配置了一個默認的 LocaleResolver 組件,但是如果自定義了一個,就會使用自定義的這個組件
 * @return
 */
@Bean
public LocaleResolver localeResolver() {
	return new MyLocaleResolver();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章