国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

史上最佳springboot Locale 國際化方案

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

使用IDEA創(chuàng)建資源組

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

2、編寫配置

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

史上最佳springboot Locale 國際化方案

增加配置類 從請求頭獲取多語言關(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 等

史上最佳springboot Locale 國際化方案

4、測試

編寫測試類

/** * 測試國際化 * * @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());}}

測試接口

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

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 洪泽县| 泽普县| 钟祥市| 东乡族自治县| 额尔古纳市| 德清县| 潞西市| 鄂托克前旗| 南华县| 德江县| 五台县| 大田县| 南和县| 汤原县| 定边县| 右玉县| 西乡县| 五大连池市| 始兴县| 文山县| 博兴县| 托里县| 荃湾区| 康保县| 满城县| 岳阳市| 昌江| 文山县| 五寨县| 平湖市| 扶沟县| 连城县| 资中县| 岳西县| 大厂| 石门县| 察哈| 耒阳市| 行唐县| 望江县| 南溪县|