SpringBoot實(shí)現(xiàn)國(guó)際化的操作步驟
什么是國(guó)際化
國(guó)際化(Internationalization) 是指為了適應(yīng)不同語言、文化和地區(qū)的用戶,使軟件能夠方便地進(jìn)行本地化修改的過程。 國(guó)際化(Internationalization) 簡(jiǎn)稱i18n,其中 “i” 是Internationalization的首字母 ,“n” 是最后一個(gè)字母 , “18” 代表了中間省略的18個(gè)字母。
SpringBoot 國(guó)際化
SpringBoot也提供了國(guó)際化的功能,在Spring Boot中,國(guó)際化通常涉及以下幾個(gè)關(guān)鍵組件:
資源文件(Properties文件):這些文件包含了不同語言的文本消息,每個(gè)語言對(duì)應(yīng)一個(gè)資源文件。通常,資源文件的命名采用
messages_語言代碼.properties的格式,例如messages_en.properties(英語)、messages_zh_CN.properties(簡(jiǎn)體中文)等。MessageSource接口:這是Spring框架提供的一個(gè)核心接口,定義了獲取文本消息的方法。它的實(shí)現(xiàn)類負(fù)責(zé)加載并解析資源文件,并根據(jù)語言和代碼來返回相應(yīng)的消息。
LocaleResolver接口:這是Spring框架提供的另一個(gè)接口,用于解析用戶的語言偏好。根據(jù)用戶的設(shè)置,LocaleResolver可以確定要使用哪個(gè)語言。
組件中使用的文本消息:在應(yīng)用程序的界面和代碼中,您可以使用特定的消息代碼來引用資源文件中的文本消息。Spring Boot會(huì)根據(jù)用戶的語言偏好選擇合適的消息進(jìn)行顯示。
通過配置MessageSource和LocaleResolver,以及在應(yīng)用程序中使用相應(yīng)的消息代碼,就可以實(shí)現(xiàn)Spring Boot的國(guó)際化功能。
實(shí)踐出真知
話不多說,上代碼。
新建Properties文件
先Resource目錄下新建Properties文件
- 中文properties文件 messages_zh_CN.properties :
hello=你好
welcome=歡迎關(guān)注公眾號(hào), {0}!
- 英文properties文件 messages_en.properties:
hello=hi
welcome=Welcome to follow WeChat Public Number, {0}!
創(chuàng)建完文件idea會(huì)自動(dòng)將國(guó)際化文件歸類到Resource Bundle中

修改配置文件
application.properties:
測(cè)試
@Resource
private MessageSource messageSource;
@Test
void testMessageSource() {
Locale china = Locale.CHINA;
System.out.println("\n中文環(huán)境");
//中文語言
String hello_zh = messageSource.getMessage("hello", null, china);
System.out.println(hello_zh);
// 占位符替換
String welcome_zh = messageSource.getMessage("welcome", new String[]{"索碼理"}, china);
System.out.println(welcome_zh);
//英文語言
Locale english = Locale.ENGLISH;
System.out.println("\n英文環(huán)境");
String hello_en = messageSource.getMessage("hello", null, english);
System.out.println(hello_en);
String welcome_en = messageSource.getMessage("welcome", new String[]{"suncodernote"}, english);
System.out.println(welcome_en);
System.out.println("\n沒有對(duì)應(yīng)語言的國(guó)際化屬性,返回code");
//沒有對(duì)應(yīng)語言的國(guó)際化屬性,返回code
String hello_test = messageSource.getMessage("hello-test", null, china);
System.out.println(hello_test);
System.out.println("\n沒有對(duì)應(yīng)語言的國(guó)際化區(qū)域時(shí),返回默認(rèn)語言");
//沒有對(duì)應(yīng)語言的國(guó)際化區(qū)域時(shí),返回默認(rèn)
String hello_fr = messageSource.getMessage("hello", null, Locale.FRANCE);
System.out.println(hello_fr);
}
測(cè)試結(jié)果:
中文環(huán)境
你好
歡迎關(guān)注公眾號(hào), 索碼理!英文環(huán)境
你好
歡迎關(guān)注公眾號(hào), suncodernote!沒有對(duì)應(yīng)語言的國(guó)際化屬性,返回code
hello-test沒有對(duì)應(yīng)語言的國(guó)際化區(qū)域時(shí),返回默認(rèn)語言
你好
獲取所有國(guó)際化資源
上面的測(cè)試我們都是只能根據(jù)一個(gè)code獲取一個(gè)國(guó)際化信息,我們?cè)谇袚Q語言使用國(guó)際化時(shí),總不能每次進(jìn)行國(guó)際化的時(shí)候都調(diào)用一次接口吧,這肯定是不行的。 下面是獲取指定語言的所有的國(guó)際化信息的代碼示例。
定義一個(gè)I18nService 接口:
public interface I18nService {
/**
* 獲取指定語言所有國(guó)際化信息
* @param locale
* @return
*/
Map<String, String> getAllMessages(Locale locale);
}
I18nService 接口實(shí)現(xiàn)類
@Service
public class I18nServiceImpl implements I18nService{
@Autowired
private MessageSource messageSource;
@Override
public Map<String, String> getAllMessages(Locale locale) {
if (locale == null) {
locale = Locale.getDefault();
}
//存放所有message
Map<String, String> messages = new HashMap<>();
ResourceBundleMessageSource bundleMessageSource = (ResourceBundleMessageSource) messageSource;
String[] basenames = bundleMessageSource.getBasenameSet().toArray(new String[0]);
for (String basename : basenames) {
//從緩存中獲取資源文件
ResourceBundle resourceBundle = ResourceBundle.getBundle(basename, locale);
//獲取資源文件的所有code
Set<String> keys = resourceBundle.keySet();
for (String key : keys) {
//根據(jù)code獲取對(duì)應(yīng)的message
String message = messageSource.getMessage(key, null, locale);
messages.put(key, message);
}
}
return messages;
}
}
在getAllMessages方法中,拿到了指定資源文件的所有code,有了code再做一些操作就很方便了。比如獲取以xxx開頭的code。
I18nController:
@RequestMapping("/i18n")
@RestController
public class I18nController {
@Resource
private I18nService i18nService;
@RequestMapping("/messages")
public Map<String, String> getAllMessages(@RequestHeader(name = "Accept-Language" , required = false) Locale locale) {
return i18nService.getAllMessages(locale);
}
}
通過postman調(diào)用接口,將請(qǐng)求頭 Accept-Language 設(shè)置為 zh 獲取中文國(guó)際化環(huán)境,測(cè)試結(jié)果如下圖所示:

以上就是SpringBoot 國(guó)際化一個(gè)簡(jiǎn)單的實(shí)現(xiàn)的操作步驟,感興趣的可以動(dòng)手試試。
總結(jié)
本文介紹了SpringBoot 國(guó)際化功能的簡(jiǎn)單使用,通過在資源文件中配置國(guó)際化字段,然后獲取對(duì)應(yīng)區(qū)域的國(guó)際化信息。這些操作都是靜態(tài)的,要預(yù)先配置好國(guó)際化信息才能進(jìn)行一系列的操作,不夠靈活。
以上就是SpringBoot實(shí)現(xiàn)國(guó)際化的操作步驟的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot國(guó)際化的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
spring如何動(dòng)態(tài)指定具體實(shí)現(xiàn)類
這篇文章主要為大家詳細(xì)介紹了spring如何動(dòng)態(tài)指定具體實(shí)現(xiàn)類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
如何把char數(shù)組轉(zhuǎn)換成String
這篇文章主要介紹了如何把char數(shù)組轉(zhuǎn)換成String問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
SpringBoot中HTTP請(qǐng)求不通的原因級(jí)解決方法
HTTP 請(qǐng)求是指從客戶端到服務(wù)器的請(qǐng)求消息,對(duì)于一個(gè) Spring Boot 項(xiàng)目而言,服務(wù)器就是 Spring Boot,客戶端就是用戶本地的瀏覽器,但是會(huì)遇到SpringBoot HTTP請(qǐng)求不通的問題,本文介紹了一些常見問題及解決方法,需要的朋友可以參考下2025-02-02
在SpringBoot中使用ResponseBodyAdvice自定義響應(yīng)的代碼實(shí)現(xiàn)
ResponseBodyAdvice是Spring Framework中的一個(gè)接口,允許您在將響應(yīng)寫入客戶端之前自定義響應(yīng),它通常與@ControllerAdvice注釋結(jié)合使用,以跨多個(gè)控制器將全局更改應(yīng)用于響應(yīng)主體,本文介紹了如何使用ResponseBodyAdvice的基本概述,需要的朋友可以參考下2024-12-12
win10和win7下java開發(fā)環(huán)境配置教程
這篇文章主要為大家詳細(xì)介紹了win7下Java開發(fā)環(huán)境配置教程,win10下Java開發(fā)環(huán)境配置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Java使用selenium爬取b站動(dòng)態(tài)的實(shí)現(xiàn)方式
本文主要介紹了Java使用selenium爬取b站動(dòng)態(tài)的實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Java工具類實(shí)現(xiàn)高效編寫報(bào)表
對(duì)于報(bào)表數(shù)據(jù)大部分情況下使用寫sql的方式為大屏/報(bào)表提供數(shù)據(jù)來源,但是對(duì)于某些復(fù)雜情況下僅僅使用sql無法實(shí)現(xiàn),這篇文章主要介紹了Java工具類實(shí)現(xiàn)高效編寫報(bào)表2022-11-11
如何在Spring?Boot微服務(wù)使用ValueOperations操作Redis集群String字符串
這篇文章主要介紹了在Spring?Boot微服務(wù)使用ValueOperations操作Redis集群String字符串類型數(shù)據(jù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06

