SpringBoot配置页面国际化功能

若要让网站以不同语言呈现,可使用国际化(internationalization)功能。

基本使用

准备工作

首先得在IDEA中设置统一编码,settings->Editor->file Encoding

当然还要导入坐标,可以根据自己需求进行更改

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

编写配置文件

1、在resources目录下(注意这里的resources目录是项目的根目录下)新建一个i18n目录,用来存放国际化配置文件

2、建立一个国际化配置文件,有两种方式。

方式一:直接创建properties文件
在这里插入图片描述
这里我们发现会自动合并,并放置于Resouce Bundle 'login’目录下

方式二、直接通过Resource Bundle建立


3、接下来编写国际化文件,也有两种方式。默认的login.properties我们使用中文。

第一种:Text框里直接写

第二种:使用国际化视窗编写

点击进入视图窗,直接点击“+”号添加对应的属性,在右边编写对应的语言即可。

简单分析下源码

那么Springboot是如何让国际化配置文件生效的呢?这里有一个MessageSourceAutoConfiguration类,里面有一个方法messageSource自动配置了管理国际化资源的组件ResourceBundleMessageSource

@Bean
public MessageSource messageSource(MessageSourceProperties properties) {
   ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    //设置国际化文件的基础名,去除国家和语言
   if (StringUtils.hasText(properties.getBasename())) {
      messageSource.setBasenames(StringUtils
            .commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
   }
    //设置编码
   if (properties.getEncoding() != null) {
      messageSource.setDefaultEncoding(properties.getEncoding().name());
   }
   messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
   Duration cacheDuration = properties.getCacheDuration();
   if (cacheDuration != null) {
      messageSource.setCacheMillis(cacheDuration.toMillis());
   }
   messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
   messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
   return messageSource;
}

点进配置文件MessageSourceProperties看看:

public class MessageSourceProperties {
   private String basename = "messages";
   ...
   }

发现默认的名字是messages,但是我们写的文件基础名不是这个,所以需要配置。

回到MessageSourceAutoConfiguration类中:

	@Bean
	@ConfigurationProperties(prefix = "spring.messages")
	public MessageSourceProperties messageSourceProperties() {
		return new MessageSourceProperties();
	}

所以,Springboot是读取配置文件中spring.messages.basename

 spring.messages.basename=i18n.login

配置页面上的国际化值

thymeleaf模板是使用#{}的形式获取国际化值的,配置如下:

此时从页面发送请求就发现页面变成了中文

但是我们希望能够根据用户的需求,自由地转换语言,该怎么做呢?

配置国际化解析器

Spring中有一个区域信息对象Locale,有一个LocaleResolver用来解析区域信息对象,也叫作国际化解析器。类似视图解析器,我们去webmvc的配置文件中去寻找。

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
public LocaleResolver localeResolver() {
    //有用户自定义的就用用户的,没有就设置为默认的private LocaleResolver localeResolver = LocaleResolver.ACCEPT_HEADER;
   if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
       //自定义的解析器
      return new FixedLocaleResolver(this.mvcProperties.getLocale());
   }
    //接收头国际化解析器
   AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
   localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
   return localeResolver;

AcceptHeaderLocaleResolver中有一个方法:

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);
}

如果我们想要自己的国际化资源生效,就是要让我们的区域信息生效。

需要我们自己来处理这个区域信息,那么就要让Springboot使用我们自定义的解析器。

1、修改前端页面的请求链接

<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>

2、自定义区域资源解析器

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 MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取到请求中的l变量值
        String language = request.getParameter("l");
        //设置默认的区域信息
        Locale defaultLocale = Locale.getDefault();
        if(!StringUtils.isEmpty(language)){
            //获取到国家和语言信息
            String[] split = language.split("_");
            //生成用于解析的区域信息
            Locale requestLocale = new Locale(split[0],split[1]);
            return requestLocale;
        }
        return defaultLocale;
    }

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

    }
}

3、将组件添加到Spring容器中,使其生效(MyMvcConfiguration)

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

现在重新启动服务,访问登录页并点击English按钮:

成功转换为英语。

附上登录页源代码:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<meta name="description" content="">
		<meta name="author" content="">
		<title>Signin Template for Bootstrap</title>
		<!-- Bootstrap core CSS -->
		<!--利用webjars的方式导入公共静态资源-->
		<link href="asserts/css/bootstrap.min.css" th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet">
		<!-- Custom styles for this template -->
		<link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">

		<link rel="shortcut icon" href="/favicon.ico"/>
		<link rel="bookmark" href="/favicon.ico"/>

	</head>

	<body class="text-center">
		<form class="form-signin" th:action="@{/user/login}" method="post">
			<img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72">
			<h1 class="h3 mb-3 font-weight-normal">[[#{login.tip}]]</h1>
			<p style="color: red" th:text="${msg}" th:if="${!#strings.isEmpty(msg)}"></p>
			<input type="text" name="username" class="form-control" th:placeholder="#{login.username}"  required="" autofocus="">
			<input type="password" name="password" class="form-control" th:placeholder="#{login.password}"  required="">
			<div class="checkbox mb-3">
				<label>
					<input type="checkbox">[[#{login.remember}]]</input>
        		</label>
			</div>
			<button class="btn btn-lg btn-primary btn-block" type="submit">[[#{login.btn}]]</button>
			<p class="mt-5 mb-3 text-muted">© 2019-2020</p>
			<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>
		</form>

	</body>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值