史上最佳springboot Locale 國際化方案
使用IDEA創(chuàng)建資源組
application.yml 增加國際化目錄配置
增加配置類 從請求頭獲取多語言關(guān)鍵字
/** * 國際化配置 * * @author Lion Li */@Configurationpublic class I18nConfig {@Beanpublic LocaleResolver localeResolver() {return new I18nLocaleResolver();}/** * 獲取請求頭國際化信息 */static class I18nLocaleResolver implements LocaleResolver {@NotNull@Overridepublic Locale resolveLocale(HttpServletRequest httpServletRequest) {String language = httpServletRequest.getHeader('content-language');Locale locale = Locale.getDefault();if (StrUtil.isNotBlank(language)) {String[] split = language.split('_');locale = new Locale(split[0], split[1]);}return locale;}@Overridepublic void setLocale(@NotNull HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {}}}3、用法詳解
在 Header 請求頭 增加上下文語言參數(shù) content-language參數(shù)需與國際化配置文件后綴對應(yīng)如 zh_CN en_US 等
編寫測試類
/** * 測試國際化 * * @author Lion Li */@RestController@RequestMapping('/demo/i18n')public class TestI18nController {@Autowiredprivate MessageSource messageSource;/** * 通過code獲取國際化內(nèi)容 * code為 messages.properties 中的 key * * 測試使用 user.register.success */@GetMapping()public String get(String code) {return messageSource.getMessage(code, new Object[]{}, LocaleContextHolder.getLocale());}}
測試接口
到此這篇關(guān)于springboot Locale 國際化方案的文章就介紹到這了,更多相關(guān)springboot 國際化內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 用xslt+css讓RSS顯示的跟網(wǎng)頁一樣漂亮2. ASP.NET MVC把數(shù)據(jù)庫中枚舉項的數(shù)字轉(zhuǎn)換成文字3. 《CSS3實戰(zhàn)》筆記--漸變設(shè)計(一)4. 測試模式 - XSL教程 - 55. Ajax實現(xiàn)異步加載數(shù)據(jù)6. 教你JS更簡單的獲取表單中數(shù)據(jù)(formdata)7. ASP.NET Core自定義中間件的方式詳解8. html5手機觸屏touch事件介紹9. CSS3實現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效10. 讓chatgpt將html中的圖片轉(zhuǎn)為base64方法示例
