页面国际化

页面国际化

准备工作

先在IDEA中统一设置properties的编码问题

setting --> File Encodings
在这里插入图片描述

编写配置文件

1、我们在resources资源文件下新建一个i18n目录,存放国际化配置文件
在这里插入图片描述

2、建立一个login.properties文件,还有一个login_zh_CN.properties;发现IDEA自动识别了我们要做国际化操作;生成(Resource Bundle ‘login’)文件夹

3、在Resource Bundle ‘login’)文件夹右键,new --> Add Locales to Resource Bundle login --> 点击左侧+ --> 输入en_US --> 选中ok;也可手动创建login_en_US.properties
在这里插入图片描述

4、打开Resource Bundle视图进行配置,点击左上角+,(new Property Key),名字随意取,在右侧三个输入框编辑对应的内容
在这里插入图片描述

配置页面国际化值

使配置文件生效

我们真实的配置文件是放在了i18n目录下,所以我们要去application.properties中配置这个messages的路径;

#配置文件的真实位置
spring.messages.basename=i18n.login
使用方法

去页面获取国际化的值,查看Thymeleaf的文档,找到message取值操作为:#{…}。

<!--三种使用细节-->
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<input type="password" class="form-control" th:placeholder="#{login.password}" required="">
<input type="checkbox" value="remember-me" >[[#{login.remember}]]

配置国际化解析

在Spring中有一个国际化的Locale (区域信息对象);里面有一个叫做LocaleResolver (获取区域信息对象)的解析器

我们去我们webmvc自动配置文件,查看一下SpringBoot默认配置:

@Override
@Bean
@ConditionalOnMissingBean(name = DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME)
@SuppressWarnings("deprecation")
public LocaleResolver localeResolver() {
   if (this.webProperties.getLocaleResolver() == WebProperties.LocaleResolver.FIXED) {
      return new FixedLocaleResolver(this.webProperties.getLocale());
   }
     // 容器中没有就自己配,有的话就用用户配置的
   if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
      return new FixedLocaleResolver(this.mvcProperties.getLocale());
   }
     // 接收头国际化分解
   AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
   Locale locale = (this.webProperties.getLocale() != null) ? this.webProperties.getLocale()
         : this.mvcProperties.getLocale();
   localeResolver.setDefaultLocale(locale);
   return localeResolver;
}

AcceptHeaderLocaleResolver 类中有一个方法

@Override
public Locale resolveLocale(HttpServletRequest request) {
   Locale defaultLocale = getDefaultLocale();
    // 默认的就是根据请求头带来的区域信息获取Locale进行国际化
   if (defaultLocale != null && request.getHeader("Accept-Language") == null) {
      return defaultLocale;
   }
   Locale requestLocale = request.getLocale();
   List<Locale> supportedLocales = getSupportedLocales();
   if (supportedLocales.isEmpty() || supportedLocales.contains(requestLocale)) {
      return requestLocale;
   }
   Locale supportedLocale = findSupportedLocale(request, supportedLocales);
   if (supportedLocale != null) {
      return supportedLocale;
   }
   return (defaultLocale != null ? defaultLocale : requestLocale);
}

那假如我们现在想点击链接让我们的国际化资源生效,就需要让我们自己的Locale生效!

我们去自己写一个自己的LocaleResolver,可以在链接上携带区域信息!

修改一下前端页面的跳转连接
<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>
写一个处理的组件类
package com.yaya.config;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 *  * @author  Allein
 *  * @date  2021/7/22 14:54
 *  
 */
public class MyLocaleResolver implements LocaleResolver {

    //解析请求
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取请求中的语言参数
        String language = request.getParameter("l");
        //如果没有就是用默认的
        Locale locale = Locale.getDefault();
        //如果请求的链接携带了国际化的参数
        if (StringUtils.hasText(language)){
            //zh_CN
            String[] split = language.split("_");
            //国家,地区
            locale = new Locale(split[0],split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
注册Bean

为了让我们的区域化信息能够生效,我们需要再配置一下这个组件!在我们自己的MvcConofig下添加bean

//注册Bean使自定义的国际化组件生效
@Bean
public LocaleResolver localeResolver(){
    return new MyLocaleResolver();
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值