史上最佳springboot Locale 國(guó)際化方案
使用IDEA創(chuàng)建資源組
application.yml 增加國(guó)際化目錄配置
增加配置類 從請(qǐng)求頭獲取多語言關(guān)鍵字
/** * 國(guó)際化配置 * * @author Lion Li */@Configurationpublic class I18nConfig {@Beanpublic LocaleResolver localeResolver() {return new I18nLocaleResolver();}/** * 獲取請(qǐng)求頭國(guó)際化信息 */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 請(qǐng)求頭 增加上下文語言參數(shù) content-language參數(shù)需與國(guó)際化配置文件后綴對(duì)應(yīng)如 zh_CN en_US 等
編寫測(cè)試類
/** * 測(cè)試國(guó)際化 * * @author Lion Li */@RestController@RequestMapping('/demo/i18n')public class TestI18nController {@Autowiredprivate MessageSource messageSource;/** * 通過code獲取國(guó)際化內(nèi)容 * code為 messages.properties 中的 key * * 測(cè)試使用 user.register.success */@GetMapping()public String get(String code) {return messageSource.getMessage(code, new Object[]{}, LocaleContextHolder.getLocale());}}
測(cè)試接口
到此這篇關(guān)于springboot Locale 國(guó)際化方案的文章就介紹到這了,更多相關(guān)springboot 國(guó)際化內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. div的offsetLeft與style.left區(qū)別2. 不要在HTML中濫用div3. html清除浮動(dòng)的6種方法示例4. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂代碼5. CSS代碼檢查工具stylelint的使用方法詳解6. 詳解CSS偽元素的妙用單標(biāo)簽之美7. WML語言的基本情況8. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)9. 使用css實(shí)現(xiàn)全兼容tooltip提示框10. 利用CSS3新特性創(chuàng)建透明邊框三角
