SpringBoot實現(xiàn)國際化的操作步驟
什么是國際化
國際化(Internationalization) 是指為了適應不同語言、文化和地區(qū)的用戶,使軟件能夠方便地進行本地化修改的過程。 國際化(Internationalization) 簡稱i18n,其中 “i” 是Internationalization的首字母 ,“n” 是最后一個字母 , “18” 代表了中間省略的18個字母。
SpringBoot 國際化
SpringBoot也提供了國際化的功能,在Spring Boot中,國際化通常涉及以下幾個關鍵組件:
資源文件(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=歡迎關注公眾號, {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)境
你好
歡迎關注公眾號, 索碼理!英文環(huá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)容,更多關于SpringBoot國際化的資料請關注腳本之家其它相關文章!
相關文章
Java將Word文檔轉(zhuǎn)換為PDF文件的幾種常用方法總結(jié)
這篇文章主要介紹了Java將Word文檔轉(zhuǎn)換為PDF文件的四種常用方法,分別使用ApachePOI+iText、Aspose.Words?for?Java、Docx4j和JODConverter,這些庫各有優(yōu)點,但在使用時需要注意庫與Java環(huán)境的兼容性、安裝所需依賴、轉(zhuǎn)換速度和資源消耗,需要的朋友可以參考下2024-10-10
SpringBoot運行時修改定時任務Cron表達式的實現(xiàn)方案
在項目開發(fā)中,定時任務是一個常見的需求,SpringBoot通過@Scheduled注解提供了簡便的定時任務實現(xiàn)方式,但默認情況下,一旦應用啟動,定時任務的Cron表達式就無法動態(tài)調(diào)整,本文將介紹如何在SpringBoot應用運行期間動態(tài)修改定時任務的Cron表達式,需要的朋友可以參考下2025-06-06
Java SpringSecurity+JWT實現(xiàn)登錄認證
這篇文章主要介紹了Java SpringSecurity+JWT實現(xiàn)登錄認證,首先通過給需要登錄認證的模塊添加mall-security依賴展開介紹,感興趣的朋友可以參考一下2022-06-06
如何為Spark Application指定不同的JDK版本詳解
這篇文章主要給大家介紹了關于如何為Spark Application指定不同的JDK版本的相關資料,文中通過示例代碼將解決的方法介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友下面來隨著小編一起學習學習吧。2017-11-11
Springboot詳解如何實現(xiàn)SQL注入過濾器過程
這篇文章主要介紹了基于springboot實現(xiàn)SQL注入過濾器,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2022-06-06

