久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

史上最佳springboot Locale 國際化方案

瀏覽:173日期:2023-02-18 14:34:05
目錄1、國際化資源2、編寫配置3、用法詳解4、測試1、國際化資源

使用IDEA創建資源組

史上最佳springboot Locale 國際化方案史上最佳springboot Locale 國際化方案史上最佳springboot Locale 國際化方案史上最佳springboot Locale 國際化方案

2、編寫配置

application.yml 增加國際化目錄配置

史上最佳springboot Locale 國際化方案

增加配置類 從請求頭獲取多語言關鍵字

/** * 國際化配置 * * @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 請求頭 增加上下文語言參數 content-language參數需與國際化配置文件后綴對應如 zh_CN en_US 等

史上最佳springboot Locale 國際化方案

4、測試

編寫測試類

/** * 測試國際化 * * @author Lion Li */@RestController@RequestMapping('/demo/i18n')public class TestI18nController {@Autowiredprivate MessageSource messageSource;/** * 通過code獲取國際化內容 * code為 messages.properties 中的 key * * 測試使用 user.register.success */@GetMapping()public String get(String code) {return messageSource.getMessage(code, new Object[]{}, LocaleContextHolder.getLocale());}}

測試接口

史上最佳springboot Locale 國際化方案史上最佳springboot Locale 國際化方案

到此這篇關于springboot Locale 國際化方案的文章就介紹到這了,更多相關springboot 國際化內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 仁化县| 上高县| 大荔县| 息烽县| 铜山县| 左云县| 临海市| 东台市| 南阳市| 玛曲县| 日照市| 金塔县| 井冈山市| 台北县| 濉溪县| 永平县| 巴楚县| 五河县| 苍山县| 昌都县| 抚宁县| 五寨县| 绥棱县| 定襄县| 东海县| 浮梁县| 长葛市| 乡城县| 张家界市| 汉寿县| 枞阳县| 北票市| 车险| 合阳县| 恩施市| 红桥区| 陆河县| 金寨县| 张掖市| 海门市| 荔波县|