史上最佳springboot Locale 國(guó)際化方案
使用IDEA創(chuàng)建資源組




application.yml 增加國(guó)際化目錄配置

增加配置類 從請(qǐng)求頭獲取多語(yǔ)言關(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)求頭 增加上下文語(yǔ)言參數(shù) content-language參數(shù)需與國(guó)際化配置文件后綴對(duì)應(yīng)如 zh_CN en_US 等

編寫(xiě)測(cè)試類
/** * 測(cè)試國(guó)際化 * * @author Lion Li */@RestController@RequestMapping('/demo/i18n')public class TestI18nController {@Autowiredprivate MessageSource messageSource;/** * 通過(guò)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. Java commons-httpclient如果實(shí)現(xiàn)get及post請(qǐng)求2. 一文帶你徹底理解Java序列化和反序列化3. JS中6個(gè)對(duì)象數(shù)組去重的方法4. Python基于requests庫(kù)爬取網(wǎng)站信息5. vscode運(yùn)行php報(bào)錯(cuò)php?not?found解決辦法6. python中文本字符處理的簡(jiǎn)單方法記錄7. PHP laravel實(shí)現(xiàn)導(dǎo)出PDF功能8. Python使用Selenium自動(dòng)進(jìn)行百度搜索的實(shí)現(xiàn)9. PHP利用curl發(fā)送HTTP請(qǐng)求的實(shí)例代碼10. 資深程序員:給Python軟件開(kāi)發(fā)測(cè)試的25個(gè)忠告!

網(wǎng)公網(wǎng)安備