SpringBoot国际化的两种实现

国际化组件其实就是获取请求头里面的Accept-Language,也可以通过手动设置国际化组件来达到实现手动选择语言的目的。

1、创建国际化的配置文件

在resource文件夹下面创建i18n,并且创建HelloWorld.properties和Helloworld_zh_CN.properties。此时的界面会发生变化。
在这里插入图片描述
在这里插入图片描述

2、修改国际化配置文件

在这里插入图片描述

3、在application.properties中配置国际化组件

#i18n是文件名字,HelloWorld是默认版本的文件名字
spring.messages.basename=i18n.HelloWorld

4、编写前端的Html文件

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--Thymeleaf的空间-->
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="#{Hello}"></div>
<div th:text="#{World}"></div>
</body>
</html>

到这里的时候已经能够根据浏览器的语言设置来显示自己想要的
在这里插入图片描述

5、想要通过点击事件来改变页面的国际化信息

1、发起改变语言的请求

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--Thymeleaf的空间-->
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="#{Hello}"></div>
<div th:text="#{World}"></div>
<a th:href="@{/HelloWorld(language='zh_CN')}">中文</a>
<a th:href="@{/HelloWorld(language='en_US')}">英文</a>
</body>
</html>

2、创建自己的国际化组件

package org.xjj.demo96.component;

import org.apache.tomcat.jni.Local;
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;

public class MyLocaleResovler implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        Locale locale=null;
        String language=httpServletRequest.getParameter("language");
        if(!StringUtils.isEmpty(language)){ //此时应该request中带的是zh_CN、en_US这种格式的文字版本
            String[] list=language.split("_");//分为数组,0为语言,1为国家
            locale=new Locale(list[0],list[1]);//重新组合并创建Locale组件,而不是使用浏览器的设置
        }else{//说明应该使用浏览器的默认版本
            //用来获取浏览器默认的请求头文字设置,此时的文字版本应该是zh-Cn、en-US和上面略有不同
            language=httpServletRequest.getHeader("Accept-Language").split(",")[0];
            String[] list=language.split("-");
            locale=new Locale(list[0],list[1]);
        }
        return locale;
    }

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

    }
}

3、将该组件注入到容器之中(这里没有采用xml的方式注入,而是使用了注解)

package org.xjj.demo96.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.xjj.demo96.component.MyLocaleResovler;

@Configuration
public class Config implements WebMvcConfigurer {
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResovler();
    }
}

到这里的时候已经可以实现点击页面的超链接来国际化的操作了
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值