SpringBoot實(shí)現(xiàn)國(guó)際化(多語言)配置的詳細(xì)步驟
核心原理
SpringBoot國(guó)際化的核心是 MessageSource 接口,其默認(rèn)實(shí)現(xiàn)是 ResourceBundleMessageSource。它會(huì)根據(jù)當(dāng)前用戶的區(qū)域(Locale)信息,從配置的資源文件(.properties 文件)中加載對(duì)應(yīng)的文本信息。
整個(gè)過程可以分解為:
- 識(shí)別區(qū)域(Locale): 通過攔截器解析請(qǐng)求,獲取用戶的語言環(huán)境(如從請(qǐng)求頭、會(huì)話、參數(shù)等)。
- 加載資源: 根據(jù)識(shí)別到的 Locale,找到對(duì)應(yīng)的 .properties 文件。
- 獲取文本: 在代碼或模板中,通過唯一的消息鍵(key) 獲取對(duì)應(yīng)的本地化文本。
詳細(xì)實(shí)現(xiàn)步驟
1.在resources文件下新建i18n文件,并新建國(guó)際化資源文件

輸入messages,添加簡(jiǎn)體中文、繁體中文、英文

出現(xiàn)以下文件,表示創(chuàng)建成功。

messages.properties文件中添加多語言信息
中文轉(zhuǎn)為UNICODE編碼,可以找一個(gè)在線 Unicode 編碼轉(zhuǎn)換
messages.properties
10001=\u7cfb\u7edf\u7e41\u5fd9\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\uff01 10002=\u8bf7\u52ff\u91cd\u590d\u63d0\u4ea4\uff01
messages_en_US.properties
10001=The system is busy, please try again later! 10002=Please do not resubmit!
messages_zh_CN.properties
10001=\u7cfb\u7edf\u7e41\u5fd9\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\uff01 10002=\u8bf7\u52ff\u91cd\u590d\u63d0\u4ea4\uff01
messages_zh_TW.properties
10001=\u7cfb\u7d71\u7e41\u5fd9\uff0c\u8acb\u7a0d\u5f8c\u518d\u8a66\uff01 10002=\u8acb\u52ff\u91cd\u8907\u63d0\u4ea4\uff01
2.相關(guān)配置
application.yml
spring:
messages:
basename: i18n/messages
encoding: utf-8
國(guó)際化參數(shù)解析器
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
/**
* 國(guó)際化參數(shù)解析器
*
* @author qf
* @date 2025-05-08 20:58:18
*/
public class HeaderLocalResolver implements LocaleResolver {
private static final String ACCEPT_LANGUAGE = "lang";
/**
* 國(guó)際化參數(shù)解析方法
*/
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
Locale locale = Locale.getDefault();
String acceptLanguageHeader = httpServletRequest.getHeader(ACCEPT_LANGUAGE);
if (StringUtils.isEmpty(acceptLanguageHeader)) {
//前端未傳lang請(qǐng)求頭 默認(rèn)使用簡(jiǎn)體中文
String[] params = LanguageEnum.CN.getLang().split("-");
return new Locale(params[0], params[1]);
}
String lang = acceptLanguageHeader.split(";")[0];
String[] params = lang.split("-");
if (params.length == 1) {
return locale;
}
locale = new Locale(params[0], params[1]);
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
/**
* 多語言枚舉
* @author qf
* @date 2025-05-08 20:58:18
*/
public enum LanguageEnum {
// 簡(jiǎn)體中文
CN("zh-CN"),
//繁體中文
TW("zh-TW"),
//英文
US("en-US");
private String lang;
LanguageEnum(String lang) {
this.lang = lang;
}
public String getLang(){
return this.lang;
}
}
國(guó)際化配置類
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
/**
* 國(guó)際化配置類
* @author qf
* @date 2025-05-08 20:58:18
*/
@Configuration
public class I18nConfig {
@Value("${spring.messages.basename:i18n.messages}")
private String baseName;
@Value("${spring.messages.encoding:UTF-8}")
private String encoding;
/**
* 注入文本解析
*/
@Bean(name = "messageSource")
public ResourceBundleMessageSource getMessageResource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename(baseName);
messageSource.setDefaultEncoding(encoding);
return messageSource;
}
/**
* 注入自定義國(guó)際化解析器
*/
@Bean(name = "localeResolver")
public LocaleResolver localeResolver(){
return new HeaderLocalResolver();
}
}
測(cè)試效果
@RestController
@RequestMapping("api")
public class TestController {
/**
* 通過code拿多語言下的內(nèi)容
* @param code
* @return
*/
@GetMapping("/test")
public String test(String code) {
String message = LanguageUtil.get(code);
return message;
}
}
請(qǐng)求頭中不添加lang
之前在HeaderLocalResolver.resolveLocale()中進(jìn)行了配置,默認(rèn)使用簡(jiǎn)體中文

請(qǐng)求頭中添加lang
在請(qǐng)求頭中添加lang,value值傳需要的簡(jiǎn)體中文、繁體中文或英文,即可進(jìn)行切換。
lang = en-US zh-TW zh-CN

以上就是SpringBoot實(shí)現(xiàn)國(guó)際化(多語言)配置的詳細(xì)步驟的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot國(guó)際化(多語言)配置的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決SpringBoot使用devtools導(dǎo)致的類型轉(zhuǎn)換異常問題
這篇文章主要介紹了解決SpringBoot使用devtools導(dǎo)致的類型轉(zhuǎn)換異常問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。 一起跟隨小編過來看看吧2020-08-08
MyBatis如何通過xml方式實(shí)現(xiàn)SaveOrUpdate
這篇文章主要講如何通過xml方式實(shí)現(xiàn)SaveOrUpdate,但是仍然建議在Service中實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-06-06
Java實(shí)現(xiàn)圖片上文字內(nèi)容的動(dòng)態(tài)修改的操作步驟
在數(shù)字圖像處理領(lǐng)域,Java提供了強(qiáng)大的庫(kù)來處理圖片,包括讀取、修改和寫入圖片,如果你需要在Java應(yīng)用程序中修改圖片上的文字內(nèi)容,可以通過圖像處理技術(shù)來實(shí)現(xiàn),這篇博文將介紹如何使用Java實(shí)現(xiàn)圖片上文字內(nèi)容的動(dòng)態(tài)修改,需要的朋友可以參考下2024-07-07
SpringSecurity Oauth2訪問令牌續(xù)期問題

