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

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

史上最佳springboot Locale 國際化方案

瀏覽:163日期: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
相關文章:
主站蜘蛛池模板: 苏尼特左旗| 荔波县| 左云县| 襄樊市| 辽中县| 麻城市| 洪洞县| 尚志市| 乳山市| 大英县| 墨竹工卡县| 禄丰县| 谢通门县| 白玉县| 双桥区| 洪湖市| 朝阳市| 富阳市| 桃园县| 黑水县| 鱼台县| 东乡| 安岳县| 瓦房店市| 青川县| 乌兰浩特市| 弥渡县| 哈密市| 彰武县| 贵溪市| 黔西县| 玉屏| 宿松县| 滨州市| 水城县| 麦盖提县| 东安县| 页游| 肃北| 大理市| 霍城县|