SpringBoot如何集成i18n(多語言)
更新時間:2024年04月03日 10:29:41 作者:@幻影忍者
這篇文章主要介紹了SpringBoot如何集成i18n(多語言)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
SpringBoot集成i18n(多語言)
配置文件
spring:
messages:
basename: il8n/messages # 配置國際化資源文件路徑
fallback-to-system-locale: true # 是否使用系統(tǒng)默認的語言環(huán)境作為備選項國際化配置
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{
/**
* 默認解析器 其中l(wèi)ocale表示默認語言
*/
@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 為默認語言顯示
Locale locale = Locale.getDefault();
// 判斷傳入?yún)?shù)是否為空
if (!StringUtils.isEmpty(language) && StringUtils.contains(language,"_")){
// 將傳過來的參數(shù),通過下劃線分割,獲取到地區(qū)(zh)即代碼(CN)
String[] split = l.split("_");
// 進行賦值
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();
}
/**
* 獲取單個國際化翻譯值
*/
public static String get(String pvsKey) {
try {
return messageSource.getMessage(pvsKey, null, LocaleContextHolder.getLocale());
} catch (Exception e) {
return pvsKey;
}
}
/**
* 獲取單個國際化翻譯值
*/
public static String get(String pvsKey,Object ... pvParams) {
try {
return messageSource.getMessage(pvsKey, pvParams, LocaleContextHolder.getLocale());
} catch (Exception e) {
return pvsKey;
}
}
}運行
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");
}
}運行截圖
- 中文

- 英文

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用EasyPOI實現(xiàn)多sheet動態(tài)列導(dǎo)出
這篇文章主要為大家詳細介紹了如何使用EasyPOI根據(jù)指定時間范圍創(chuàng)建動態(tài)列,以及如何將數(shù)據(jù)組織成符合要求的格式并導(dǎo)出,感興趣的可以了解下2025-03-03
解決window.location.href之后session丟失的問題
今天小編就為大家分享一篇關(guān)于解決window.location.href之后session丟失的問題,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
springboot啟動報錯Failed to load class [java
這篇文章主要介紹了springboot啟動報錯Failed to load class [javax.servlet.Filter]的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2026-03-03

