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

修改配置文件
application.properties:
測試
@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沒有對應語言的國際化屬性,返回code");
//沒有對應語言的國際化屬性,返回code
String hello_test = messageSource.getMessage("hello-test", null, china);
System.out.println(hello_test);
System.out.println("\n沒有對應語言的國際化區(qū)域時,返回默認語言");
//沒有對應語言的國際化區(qū)域時,返回默認
String hello_fr = messageSource.getMessage("hello", null, Locale.FRANCE);
System.out.println(hello_fr);
}
測試結(jié)果:
中文環(huán)境
你好
歡迎關(guān)注公眾號, 索碼理!英文環(huán)境
你好
歡迎關(guān)注公眾號, suncodernote!沒有對應語言的國際化屬性,返回code
hello-test沒有對應語言的國際化區(qū)域時,返回默認語言
你好
獲取所有國際化資源
上面的測試我們都是只能根據(jù)一個code獲取一個國際化信息,我們在切換語言使用國際化時,總不能每次進行國際化的時候都調(diào)用一次接口吧,這肯定是不行的。 下面是獲取指定語言的所有的國際化信息的代碼示例。
定義一個I18nService 接口:
public interface I18nService {
/**
* 獲取指定語言所有國際化信息
* @param locale
* @return
*/
Map<String, String> getAllMessages(Locale locale);
}
I18nService 接口實現(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獲取對應的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)用接口,將請求頭 Accept-Language 設置為 zh 獲取中文國際化環(huán)境,測試結(jié)果如下圖所示:

以上就是SpringBoot 國際化一個簡單的實現(xiàn)的操作步驟,感興趣的可以動手試試。
總結(jié)
本文介紹了SpringBoot 國際化功能的簡單使用,通過在資源文件中配置國際化字段,然后獲取對應區(qū)域的國際化信息。這些操作都是靜態(tài)的,要預先配置好國際化信息才能進行一系列的操作,不夠靈活。
以上就是SpringBoot實現(xiàn)國際化的操作步驟的詳細內(nèi)容,更多關(guān)于SpringBoot國際化的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
如何把char數(shù)組轉(zhuǎn)換成String
這篇文章主要介紹了如何把char數(shù)組轉(zhuǎn)換成String問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
在SpringBoot中使用ResponseBodyAdvice自定義響應的代碼實現(xiàn)
ResponseBodyAdvice是Spring Framework中的一個接口,允許您在將響應寫入客戶端之前自定義響應,它通常與@ControllerAdvice注釋結(jié)合使用,以跨多個控制器將全局更改應用于響應主體,本文介紹了如何使用ResponseBodyAdvice的基本概述,需要的朋友可以參考下2024-12-12
win10和win7下java開發(fā)環(huán)境配置教程
這篇文章主要為大家詳細介紹了win7下Java開發(fā)環(huán)境配置教程,win10下Java開發(fā)環(huán)境配置,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
Java使用selenium爬取b站動態(tài)的實現(xiàn)方式
本文主要介紹了Java使用selenium爬取b站動態(tài)的實現(xiàn)方式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
如何在Spring?Boot微服務使用ValueOperations操作Redis集群String字符串
這篇文章主要介紹了在Spring?Boot微服務使用ValueOperations操作Redis集群String字符串類型數(shù)據(jù),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06

