1、application.properties中指明国际化文件所在路径和文件前缀

2、登录页面用#{}的形式从国际化配置文件中取值

3、编写国际化文件

----------------------------------若想实现中英文切换,继续下面部分----------------

4、点击中英文切换按钮时指定访问路径并传递参数

5、定义自己的LocaleResolver,用于根据传递的参数返回LocaleResolver

6、在@Configuration标注的配置类中以@Bean的形式将自己定义的LocaleResolver以组件的形式加入到容器中

具体实现代码如下:

1、application.properties中指明国际化文件所在路径和文件前缀

spring.messages.basename=i18n.login

2、登录页面用#{}的形式从国际化配置文件中取值

login.html中的代码如下
<!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 -->
<link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">
</head> <body class="text-center">
<form class="form-signin" action="dashboard.html">
<img class="mb-4" src="asserts/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>
<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>
<!--要写成th:href="@{/index(l='zh_CN')}",而不能写成th:href="@{/login.html(l='zh_CN')}",
因为直接定位到静态资源的话是不会走自己定义的区域解析器的,
另外thymeleaf模板引擎是用中括号中放key=value的形式传参数-->
<a class="btn btn-sm" th:href="@{/index(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index(l='en_US')}">English</a>
</form> </body> </html>

3、编写国际化文件

i18n/login.properties和i18n/login_zh_CN.properties配置的内容相同,内容如下:
login.username=用户名
login.password=密码
login.remember=记住我
login.btn=登录
login.tip=提示 i18n/login_en_US.properties文件的内容如下:
login.username=username
login.password=password
login.remember=remember me
login.btn=sign in
login.tip=tip

----------------------------------若想实现中英文切换,继续下面部分----------------

4、点击中英文切换按钮时指定访问路径并传递参数

login.html中涉及到中英文切换的代码如下:
!--要写成th:href="@{/index(l='zh_CN')}",而不能写成th:href="@{/login.html(l='zh_CN')}",
因为直接定位到静态资源的话是不会走自己定义的区域解析器的,
另外thymeleaf模板引擎是用中括号中放key=value的形式传参数-->
<a class="btn btn-sm" th:href="@{/index(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index(l='en_US')}">English</a>

5、定义自己的LocaleResolver,用于根据传递的参数返回LocaleResolver

package com.myself.component;

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) {
Locale locale = Locale.getDefault();
String l = request.getParameter("l");
if(!StringUtils.isEmpty(l)){
String[] split = l.split("_");
locale = new Locale(split[0],split[1]);
} return locale;
} @Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { }
}

6、在@Configuration标注的配置类中以@Bean的形式将自己定义的LocaleResolver以组件的形式加入到容器中

package com.myself.config;

import com.myself.component.MyLocaleResolver;
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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("login");
} //使用WebMvcConfigurerAdapter来扩展SpringMVC的功能
//所有的WebMvcConfigurerAdapter都会生效
//注意要写在标有@Configuration的类中,要在方法上标上@Bean注解
@Bean
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
WebMvcConfigurerAdapter webMvcConfigurerAdapter = new WebMvcConfigurerAdapter(){
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/helloIndex").setViewName("login");
registry.addViewController("/index").setViewName("login");
registry.addViewController("/").setViewName("login");
}
};
return webMvcConfigurerAdapter;
} //定义区域解析器,解析国际化中英文切换
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
} } 中文测试如下:

英文测试如下:

若有理解不到之处,望指正!

0013SpringBoot处理国际化问题的更多相关文章

  1. Hello Web API系列教程——Web API与国际化

    软件国际化是在软件设计和文档开发过程中,使得功能和代码设计能处理多种语言和文化习俗,在创建不同语言版本时,不需要重新设计源程序代码的软件工程方法.这在很多成熟的软件开发平台中非常常见.对于.net开发 ...

  2. JS魔法堂:不完全国际化&本地化手册 之 理論篇

    前言  最近加入到新项目组负责前端技术预研和选型,其中涉及到一个熟悉又陌生的需求--国际化&本地化.熟悉的是之前的项目也玩过,陌生的是之前的实现仅仅停留在"有"的阶段而已. ...

  3. Struts2入门(六)——国际化

    一.前言 1.1.国际化简介 国际化是指应用程序在运行的时候,根据客户端请求来自的国家地区.语言的不同而显示不同的界面(简单说就是根据你的地区显示相关地区的语言,如果你现在在英国,那么显示的语言就是英 ...

  4. JavaWeb的国际化

    国际化 1.国际化开发概述 1.1.软件的国际化 软件开发时,要使它能同时应对世界不同地区和国家的方法,并针对不同地区和国家的方法,提供相应的,符合来访者阅读习惯的页面或数据 国际化简称:i18n : ...

  5. struts2国际化

    struts2国际化 1:什么是国际化? 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有的与语言,国家/地区和文化相关的元素 ...

  6. 学习SpringMVC——国际化+上传+下载

    每个星期一道菜,这个星期也不例外~~~ 一个软件,一个产品,都是一点点开发并完善起来的,功能越来越多,性能越来越强,用户体验越来越好……这每个指标的提高都需要切切实实的做点东西出来,好比,你的这个产品 ...

  7. JS魔法堂:不完全国际化&本地化手册 之 实战篇

    前言  最近加入到新项目组负责前端技术预研和选型,其中涉及到一个熟悉又陌生的需求--国际化&本地化.熟悉的是之前的项目也玩过,陌生的是之前的实现仅仅停留在"有"的阶段而已. ...

  8. JS魔法堂:不完全国际化&本地化手册 之 拓展篇

    前言  最近加入到新项目组负责前端技术预研和选型,其中涉及到一个熟悉又陌生的需求--国际化&本地化.熟悉的是之前的项目也玩过,陌生的是之前的实现仅仅停留在"有"的阶段而已. ...

  9. C# WinForm国际化的简单实现

    软件行业发展到今天,国际化问题一直都占据非常重要的位置,而且应该越来越被重视.对于开发人员而言,在编写程序之前,国际化问题是首先要考虑的一个问题,也许有时候这个问题已经在设计者的考虑范围之内,但终归要 ...

随机推荐

  1. Wine 总结

    下诉描述有些问题,我用非root用户安装的软件有些也会安装到root用户的家目录里不知道为什么:[我知道了,貌似用了sudo的安装的都是在root目录里..] 经过测试,最好用root权限安装,否则会 ...

  2. 在CentOS7 安装 Redis数据库

    环境说明: 名称 版本 CentOS CentOS Linux release 7.4.1708 (Core) VMware Fusion 专业版 10.1.1 (7520154) SSH Shell ...

  3. 10分钟搭建一个小型网页(python django)(hello world!)

    10分钟搭建一个小型网页(python django)(hello world!) 1.安装django pip install django 安装成功后,在Scripts目录下存在django-ad ...

  4. 如何用Java实现条件编译

    在 C 或 C++ 中,可以通过预处理语句来实现条件编译.代码如下: #define DEBUG #IFDEF DEBUUG /* code block 1 */ #ELSE /* code bloc ...

  5. O(1) gcd 板子

    const int N = 2e5+10; const int M = 500; int cnt, p[N], _gcd[M][M]; int v[N][3],vis[N]; int gcd(int ...

  6. CentOS7 安装 Git

    环境: 系统版本:CentOS 7.5 Git 版本:2.20.1 一.安装 Git 1.下载编译工具 $ yum -y groupinstall "Development Tools&qu ...

  7. List、dictionary、hashtable、ArrayList集合

    集合的引用命名空间在 system.Collections下 1.为什么引入集合 因为数组长度是固定的,为了建立一个动态的"数组",所以引入了集合. 2.为什么引入ArrayLis ...

  8. 【转载】常见面试题:C#中String和string的区别分析

    在很多人面试C#开发工程师的时候,会遇到一个面试题,就是C#中String和string有啥区别.其实针对这个问题C#中String和string没有本质上的区别,两者在程序中都可使用,稍微的一个区别 ...

  9. 远程 Linux(Ubuntu 18)添加字体

    安装 xshell与xftp 连接xshell 点击 xshell上方工具栏中的xftp图标, 自动连接xftp linux下创建字体目录 su cd / cd usr/share/fonts mkd ...

  10. 前端之:传统的DOM是如何渲染的?

    a.纯后端渲染:页面发送请求,后端服务器中将数据拼成完整DOM树,并转换成一个字节流作为HTTP Response的body返回给浏览器.优点在于 返回的HTTP Response是包含着全部页面内容 ...