软件实现国际化,需具备以下两个特征
- 对于程序中固定使用的文本元素,例如菜单栏、导航条等中使用的文本元素、或错误提示信息,状态信息等,需要根据来访者的地区和国家,选择不同语言的文本为之服务。
- 对于程序动态产生的数据,例如(日期,货币等),软件应能根据当前所在的国家或地区的文化习惯进行显示
步骤
- 确定IDEA中File Encodings中都是utf-8的编码方式
- 创建不同地区的文件
- 在页面中修改中英文的属性
- 创建MyLocaleResolver 继承LocaleResolver解析国际化
- 在WebMvcConfigurer中使自定义国际化生效
使用IDEA来实现i18n
当点击English的时候,页面显示的是英文页面
- 要先确定IDEA中File Encodings中都是utf-8的编码方式,不然会出现乱码的状况
- 在resorces文件中创建i18n的文件夹,在i18n中创建三个properties文件,分别是login.properties、login_en_US.properties(英文)、login_zh_CN.properties(中文)
在页面中显示的中英文分别有五个
- 登录按钮
- 密码
- 记住我
- 请登录
- 用户名
这五个部分要实现中英转换,在IDEA中有一个Resource Bundle,这个非常方便
login_en_US.properties
login_zh_CN.properties
- 在页面中修改中英文代表的属性
<!doctype html>
<html lang="en-US" xmlns:th="http://www.thymeleaf.org">
<head>
<meta 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="">
<link rel="icon" th:href="@{/img/favicon.ico}">
<title>Signin Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link th:href="@{/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" th:action="@{/user/login}">
<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>
<!--如果msg为空则不显示-->
<p style="color: red" th:text="${msg}" th:if="${not #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" value="remember-me"> [[#{login.remember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">[[#{login.btn}]]</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<a class="btn btn-sm" th:href="@{/index.html(language='zh_CH')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(language='en_US')}">English</a>
</form>
</body>
</html>
- 创建MyLocaleResolver 继承LocaleResolver解析国际化
package com.an.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;
//国际化
public class MyLocaleResolver implements LocaleResolver {
//解析国际化请求
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
String language = httpServletRequest.getParameter("language");
Locale locale = Locale.getDefault();//如果没有设置国际化及使用默认
//如果请求的连接携带了国际化的参数,先判断是否为空,如果没有将地区和国家分开
if (!StringUtils.isEmpty(language)){
String[] split = language.split("_");
locale = new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
- 在WebMvcConfigurer中使自定义国际化生效,创建MyWebMvcConfigurer,继承WebMvcConfigurer
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.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//自定义国际化生效
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}