自定义LocaleResolver实现页面中英文切换
生活随笔
收集整理的這篇文章主要介紹了
自定义LocaleResolver实现页面中英文切换
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1.配置i18n(國(guó)際化)文件
- 2.頁(yè)面設(shè)置值
- 3.編寫(xiě)自定義的LocaleResolver組件
- 4.注意點(diǎn)
1.配置i18n(國(guó)際化)文件
建立login和login_en_US時(shí)會(huì)自動(dòng)生成Bundle
配置默認(rèn)值,英文,中文
2.頁(yè)面設(shè)置值
這里用了thymeleaf模板,前面的文章有提到用法
<img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72"><h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}" >Please sign in</h1><input type="text" class="form-control" th:placeholder="#{login.username}" required="" autofocus=""><input type="password" class="form-control" th:placeholder="#{login.password}" required="" ><div class="checkbox mb-3"><label><input type="checkbox" value="remember-me" >[[#{login.remenber}]]測(cè)試一下值是否顯示
兩個(gè)超鏈接標(biāo)簽點(diǎn)擊傳值
3.編寫(xiě)自定義的LocaleResolver組件
public class MyLocalResolver implements LocaleResolver {@Overridepublic Locale resolveLocale(HttpServletRequest httpServletRequest) {//獲取請(qǐng)求中的語(yǔ)言參數(shù)String language = httpServletRequest.getParameter("l");//如果沒(méi)有返回值就用默認(rèn)的Locale locale = Locale.getDefault();if (!StringUtils.isEmpty(language)){//zh_CN or en_USString[] split = language.split("_");//國(guó)家,地區(qū)locale = new Locale(split[0],split[1]);}return locale;}@Overridepublic void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {} }將組件配置到spring容器
@Configuration public class MyMvcConfig implements WebMvcConfigurer {@Beanpublic LocaleResolver localeResolver(){return new MyLocalResolver();} }測(cè)試
4.注意點(diǎn)
一定要把所有的編碼都改為utf-8格式,否則可能會(huì)出現(xiàn)亂碼
總結(jié)
以上是生活随笔為你收集整理的自定义LocaleResolver实现页面中英文切换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Ajax+jquery实现异步验证用户名
- 下一篇: JVM面试速记