解决使用LocaleResover做国际化遇到的一个小问题

问题:
  项目是SpringBoot的,在登陆页面要求实现国际化,但是点击中文/英文没有效果

原始实现步骤:
1)、编写国际化配置文件,抽取页面需要显示的国际化消息
2)、SpringBoot自动配置好了管理国际化资源文件的组件;只需要在配置文件中配置

spring.messages.basename=i18n.login

告诉springboot国际化在这里
3)、去页面获取国际化的值;

<body class="text-center">
   <form class="form-signin" action="dashboard.html">
      <img class="mb-4" src="asserts/img/bootstrap-solid.svg" th:src="@{asserts/img/bootstrap-solid.svg}" alt="" width="72" height="72">
      <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.signin}">Please sign in</h1>
      <label class="sr-only" th:text="#{login.username}">Username</label>
      <input type="text" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
      <label class="sr-only" th:text="#{login.password}">Password</label>
      <input type="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}" required="">
      <div class="checkbox mb-3">
         <label>
         <input type="checkbox" value="remember-me"> [[#{login.remember}]]
       </label>
      </div>
      <button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>
      <p class="mt-5 mb-3 text-muted">© 2017-2018</p>
      <a class="btn btn-sm">中文</a>
      <a class="btn btn-sm">English</a>
   </form>
</body>

效果:根据浏览器语言设置的信息切换了国际化;
4)、实现点击链接切换国际化
首先让按钮带上超链接

<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>

其次定义自己的LocaleResolver

//自定义的获取区域信息对象,可以在连接上携带区域信息
public class MyLocaleResolver implements LocaleResolver {

    //Locale(区域信息对象)
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String l = httpServletRequest.getParameter("l");
        Locale locale = Locale.getDefault();
        if (!StringUtils.isEmpty(l)){
            String[] split = l.split("_");
            locale = new Locale(split[0],split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}

最后加入容器中

@Bean
public MyLocaleResolver myLocaleResolver(){
    return new MyLocaleResolver();
}

解决:
问题就出在,最后加入容器的那一步,修改为

@Bean
public LocaleResolver localeResolver(){
    return new MyLocaleResolver();
}l

就可以了。

原理:
在webmvcAutoConfig中:

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
public LocaleResolver localeResolver() {
   // 如果配置文件中配置了获取区域信息对象,就使用
   if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
      return new FixedLocaleResolver(this.mvcProperties.getLocale());
   }
   //如果没有配置,就由AcceptHeaderLocaleResolver 来获取区域信息对象
   AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
   localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
   return localeResolver;
}

//默认的就是根据请求头带来的区域信息获取Locale进行国际化
public class AcceptHeaderLocaleResolver implements LocaleResolver {
   public Locale resolveLocale(HttpServletRequest request) {
        Locale requestLocale = request.getLocale();
    }

这里有一个注解:@ConditionalOnMissingBean
也就是说,如果容器中没有LocaleResolver 才从这里获取。
而我们自定义了LocaleResolver,名为MyLocaleResolver,目的是使用自定义的来替代。
而上面在注入容器时,bean的名称是myLocaleResolver,它不是LocaleResolver。所以并不会使用自定义的,所以无效。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值