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. 【VS开发】从sockaddr中取得客户端或者数据源的Ip地址和端口号

    在socket编程中,服务器端accept()等待一个客户端的连接,当连接成功后,accept拷贝客户端的地址信息到sin_addr里面,我们如何从sin_addr取得此客户端的Ip地址和端口号呢? ...

  2. 【GStreamer开发】GStreamer播放教程02——字幕管理

    目标 这篇教程和上一篇非常相似,但不是切换音频流,而是字幕了.这次我们会展示: 如何选择选择字幕流 如何引入外部的字幕 如何客制化字幕使用的字体 介绍 我们都知道一个文件可以有多个音视频流并且可以使用 ...

  3. 安卓app和苹果app共用一个二维码

    应项目要求,现在安卓app和苹果app共用一个二维码,对外提供下载: <html> <head> <meta http-equiv="Content-Type& ...

  4. Ansible安装配置及命令使用详解

    Ansible和saltstack目前市面上一些其它的项目管理工具有很大的不同,它的设计初衷就是为了更方便.快捷的进行配置管理.它易于安装和使用.语法也非常简单易学.你可以用Ansible将平常复杂的 ...

  5. docker 通过overlay 构建跨主机联网通信

    初始化 swarm meiya@meiya:~$ docker swarm init 将当前节点作为manager节点加入swarm meiya@meiya:~$ docker swarm join- ...

  6. idea设置创建类的注释模板

    打开settings>>Editor>>File and Code Templates>>Includes>>File Header

  7. 通过noVNC和websockify连接到QEMU/KVM 转

    开源项目 QEMU.KVM.libvirt 实现了创建虚拟机,启动虚拟机,监控虚拟机.我们解决了从无到有的问题,这时就该考虑从有到优了.尽管我们能使用 SSH 的方式来登录使用虚拟机,但这种方式从感觉 ...

  8. 守护进程daemon

    # -*- coding: utf-8 -*- import sys, os, time, atexit from signal import SIGTERM class Daemon: def __ ...

  9. Spring之22:DefaultListableBeanFactory

    1. DefaultListableBeanFactory的作用: 默认实现了ListableBeanFactory和BeanDefinitionRegistry接口,基于bean definitio ...

  10. (二)spring Security 自定义登录页面与校验用户

    文章目录 配置 security 配置下 MVC 自定义登录页面 自定义一个登陆成功欢迎页面 效果图 小结: 使用 Spring Boot 的快速创建项目功能,勾选上本篇博客需要的功能:web,sec ...