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)類負責加載并解析資源文件,并根據語言和代碼來返回相應的消息。
LocaleResolver接口:這是Spring框架提供的另一個接口,用于解析用戶的語言偏好。根據用戶的設置,LocaleResolver可以確定要使用哪個語言。
組件中使用的文本消息:在應用程序的界面和代碼中,您可以使用特定的消息代碼來引用資源文件中的文本消息。Spring Boot會根據用戶的語言偏好選擇合適的消息進行顯示。
通過配置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); }
測試結果:
中文環(huán)境
你好
歡迎關注公眾號, 索碼理!英文環(huán)境
你好
歡迎關注公眾號, suncodernote!沒有對應語言的國際化屬性,返回code
hello-test沒有對應語言的國際化區(qū)域時,返回默認語言
你好
獲取所有國際化資源
上面的測試我們都是只能根據一個code獲取一個國際化信息,我們在切換語言使用國際化時,總不能每次進行國際化的時候都調用一次接口吧,這肯定是不行的。 下面是獲取指定語言的所有的國際化信息的代碼示例。
定義一個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) { //根據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調用接口,將請求頭 Accept-Language 設置為 zh 獲取中文國際化環(huán)境,測試結果如下圖所示:
以上就是SpringBoot 國際化一個簡單的實現(xiàn)的操作步驟,感興趣的可以動手試試。
總結
本文介紹了SpringBoot 國際化功能的簡單使用,通過在資源文件中配置國際化字段,然后獲取對應區(qū)域的國際化信息。這些操作都是靜態(tài)的,要預先配置好國際化信息才能進行一系列的操作,不夠靈活。
以上就是SpringBoot實現(xiàn)國際化的操作步驟的詳細內容,更多關于SpringBoot國際化的資料請關注腳本之家其它相關文章!
相關文章
Java使用Flyway實現(xiàn)數(shù)據庫版本控制的技術指南
在現(xiàn)代應用開發(fā)中,數(shù)據庫結構經常隨著業(yè)務需求不斷演變,使用手動SQL腳本管理數(shù)據庫版本,不僅容易出現(xiàn)錯誤,還難以跟蹤和回滾,Flyway是一個強大的數(shù)據庫遷移工具,能夠幫助開發(fā)者高效管理和自動化數(shù)據庫的版本控制,本文將介紹Flyway的基本功能及其在SpringBoot項目中的實踐2025-02-02關于IDEA2020.1新建項目maven PKIX 報錯問題解決方法
這篇文章主要介紹了關于IDEA2020.1新建項目maven PKIX 報錯問題解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06spring源碼學習之bean的初始化以及循環(huán)引用
這篇文章主要給大家介紹了關于spring源碼學習之bean的初始化以及循環(huán)引用的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10springboot集成redis實現(xiàn)簡單秒殺系統(tǒng)
這篇文章主要為大家詳細介紹了springboot集成redis實現(xiàn)簡單秒殺系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12