SpringBoot如何集成i18n(多語言)
更新時(shí)間:2024年04月03日 10:29:41 作者:@幻影忍者
這篇文章主要介紹了SpringBoot如何集成i18n(多語言)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
SpringBoot集成i18n(多語言)
配置文件
spring: messages: basename: il8n/messages # 配置國際化資源文件路徑 fallback-to-system-locale: true # 是否使用系統(tǒng)默認(rèn)的語言環(huán)境作為備選項(xiàng)
國際化配置
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.WebMvcConfigurer; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; /** * 國際化配置 */ @Configuration public class I18nlocaleConfig implements WebMvcConfigurer{ /** * 默認(rèn)解析器 其中l(wèi)ocale表示默認(rèn)語言 */ @Bean public LocaleResolver localeResolver() { return new MyLocaleResolver(); } @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); localeChangeInterceptor.setParamName("Accept-Language"); return localeChangeInterceptor; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); } }
參數(shù)解析
import org.apache.commons.lang3.StringUtils; import org.springframework.web.servlet.LocaleResolver; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Locale; /** * 參數(shù)解析 */ public class MyLocaleResolver implements LocaleResolver { @Override public Locale resolveLocale(HttpServletRequest request) { // 從 request 域中讀取傳過來的參數(shù) String l = request.getHeader("Accept-Language"); // 聲明 Locale 為默認(rèn)語言顯示 Locale locale = Locale.getDefault(); // 判斷傳入?yún)?shù)是否為空 if (!StringUtils.isEmpty(language) && StringUtils.contains(language,"_")){ // 將傳過來的參數(shù),通過下劃線分割,獲取到地區(qū)(zh)即代碼(CN) String[] split = l.split("_"); // 進(jìn)行賦值 locale = new Locale(split[0],split[1]); } // 返回 return locale; } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { } }
ApplicationEvent
import com.zzdy.recharge.il8n.utils.MessageUtils; import org.springframework.context.ApplicationListener; import org.springframework.context.MessageSource; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; import javax.annotation.Resource; @Component public class ApplicationEvent implements ApplicationListener<ContextRefreshedEvent> { @Resource protected MessageSource messageSource; @Override public void onApplicationEvent(ContextRefreshedEvent event) { MessageUtils.setMessageSource(messageSource); } }
MessageUtils
import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.support.ResourceBundleMessageSource; public class MessageUtils extends ResourceBundleMessageSource { private static MessageSource messageSource; public static void setMessageSource(MessageSource source){ messageSource=source; } public MessageUtils() { super(); } /** * 獲取單個(gè)國際化翻譯值 */ public static String get(String pvsKey) { try { return messageSource.getMessage(pvsKey, null, LocaleContextHolder.getLocale()); } catch (Exception e) { return pvsKey; } } /** * 獲取單個(gè)國際化翻譯值 */ public static String get(String pvsKey,Object ... pvParams) { try { return messageSource.getMessage(pvsKey, pvParams, LocaleContextHolder.getLocale()); } catch (Exception e) { return pvsKey; } } }
運(yùn)行
import com.zzdy.recharge.il8n.utils.MessageUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("recharge/i18n") public class GreetingController { @GetMapping("/greeting") public String greeting() { return MessageUtils.get("not.null"); } }
運(yùn)行截圖
- 中文
- 英文
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java8中Stream使用的一個(gè)注意事項(xiàng)
最近在工作中發(fā)現(xiàn)了對(duì)于集合操作轉(zhuǎn)換的神器,java8新特性 stream,但在使用中遇到了一個(gè)非常重要的注意點(diǎn),所以這篇文章主要給大家介紹了關(guān)于Java8中Stream使用過程中的一個(gè)注意事項(xiàng),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11SpringBoot整合SpringSecurity實(shí)現(xiàn)圖形驗(yàn)證碼功能
圖形驗(yàn)證碼是一種用于區(qū)分用戶是人類還是計(jì)算機(jī)程序的自動(dòng)化測(cè)試,它通常用于防止自動(dòng)化軟件進(jìn)行惡意操作,如濫用在線服務(wù)、暴力破?解密碼或進(jìn)行垃圾郵件發(fā)送等,下面將介紹?Spring?Boot?整合?Spring?Security?實(shí)現(xiàn)圖形驗(yàn)證碼功能,需要的朋友可以參考下2024-12-12Spring整合websocket整合應(yīng)用示例(下)
這篇文章主要介紹了Spring整合websocket整合應(yīng)用示例(下)的相關(guān)資料,需要的朋友可以參考下2016-04-04Java回調(diào)函數(shù)與觀察者模式實(shí)例代碼
這篇文章主要介紹了Java回調(diào)函數(shù)與觀察者模式實(shí)例代碼,簡(jiǎn)單介紹了使用觀察者模式的場(chǎng)景,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02Javabean基于xstream包實(shí)現(xiàn)轉(zhuǎn)XML文檔的方法
這篇文章主要介紹了Javabean基于xstream包實(shí)現(xiàn)轉(zhuǎn)XML文檔的方法,結(jié)合具體實(shí)例形式分析了xstream包用于轉(zhuǎn)換xml文件的具體使用技巧,需要的朋友可以參考下2017-05-05