springboot中大部分有默認配置所以開發(fā)起項目來非常迅速,僅對需求項做單獨配置覆蓋即可
spring采用的默認區(qū)域解析器是AcceptHeaderLocaleResolver,根據(jù)request header中的accept-language值來解析locale,并且是不可變的。
那么想要實現(xiàn)國際化,就要使用SessionLocaleResolver或者CookieLocaleResolver。正如類的名字所示,是按session或cookie中儲存的locale值來解析locale。
我就以SessionLocaleResolver舉例:
1.建立一個配置類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.example.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.i18n.SessionLocaleResolver; /** * Created by wq on 2016/8/15. */ @Configuration public class SpringMVC_config { @Bean (name= "localeResolver" ) public LocaleResolver localeResolverBean() { return new SessionLocaleResolver(); } // @Bean(name="messageSource") // public ResourceBundleMessageSource resourceBundleMessageSource(){ // ResourceBundleMessageSource source=new ResourceBundleMessageSource(); // source.setBasename("messages"); // return source; // } } |
注意 name="localeResolver" 是必須的
優(yōu)先級如下:
session中對應(yīng)屬性(3中有說明)有值則按session來
如果沒有但是SessionLocaleResolver設(shè)置了默認的locale則按默認值來
1
2
|
// SessionLocaleResolver localeResolver=new SessionLocaleResolver(); // localeResolver.setDefaultLocale(Locale.ENGLISH); |
再然后就還是按request header中的accept-language值來
2.建立對應(yīng)的messages.properties
messages.properties
messages_en.properties
messages_zh_CN.properties
前面注釋的代碼則可以修改properties的前綴部分,name="messageSource" 同樣是必須的
比如 setBasename("msg"); 對應(yīng)properties則為
msg.properties
msg_en.properties
msg_zh_CN.properties
格式上sys.test=hello、sys.test=你好,應(yīng)該無需贅述(可能轉(zhuǎn)碼會避免一些問題,我這里直接放的中文)
3.controller中切換locale
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.example.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.LocaleResolver; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.util.Locale; import static org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME; /** * Created by Administrator on 2016/6/11. */ @Controller public class DemoController { @Autowired LocaleResolver localeResolver; @RequestMapping ( "test" ) public String test(HttpServletRequest request, HttpServletResponse response) { HttpSession session=request.getSession(); localeResolver.setLocale(request,response,Locale.ENGLISH); System.out.println(session.getAttribute(LOCALE_SESSION_ATTRIBUTE_NAME)); return "messages" ; } } |
這里org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME這個字符串常量則是session中默認屬性名
可以看一下SessionLocaleResolver的部分源碼
1
2
3
|
public class SessionLocaleResolver extends AbstractLocaleContextResolver { public static final String LOCALE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver. class .getName() + ".LOCALE" ; public static final String TIME_ZONE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver. class .getName() + ".TIME_ZONE" ; |
locale默認屬性名為
org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE
setLocale是抽象類AbstractLocaleContextResolver中方法
1
2
3
|
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { this .setLocaleContext(request, response, locale != null ? new SimpleLocaleContext(locale): null ); } |
然后看SessionLocaleResolver中setLocaleContext
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) { Locale locale = null ; TimeZone timeZone = null ; if (localeContext != null ) { locale = localeContext.getLocale(); if (localeContext instanceof TimeZoneAwareLocaleContext) { timeZone = ((TimeZoneAwareLocaleContext)localeContext).getTimeZone(); } } WebUtils.setSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME, locale); WebUtils.setSessionAttribute(request, TIME_ZONE_SESSION_ATTRIBUTE_NAME, timeZone); } |
本質(zhì)上就是一些非空判斷取默認,最終給session中的對應(yīng)屬性賦值
4.thymeleaf頁面中調(diào)用
1
2
3
4
5
6
7
8
9
10
11
|
<!DOCTYPE html> < html lang = "zh_CN" xmlns:th = "http://www.thymeleaf.org" > < head > < title >msg</ title > </ head > < body > < h1 th:text = "${#locale}" ></ h1 > < h1 th:text = "#{sys.test}" ></ h1 > </ body > </ html > |
則顯示en hello
能用注解的,盡量不用xml,看著xml就煩!!!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/wqbill/p/5773338.html