增加国际化i18n语言配置:

# src/main/resources/i18n/login.properties

login.btn=登录
# src/main/resources/i18n/login_zh_CN.properties

login.btn=登录
# src/main/resources/i18n/login_en_US.properties

login.btn=sign in

在模板中使用国际化语言输出及增加切换语言的链接:

# src/main/resources/templates/index.html

<h1 ... th:text="#{login.tip}"></h1>
<input type="checkbox" value="remember-me"> [[#{login.remember}]]
<button type="submit"> [[#{login.btn}]] </button> <a th:href="@{/index.html(l='zh_CN')}">中文</a>
<a th:href="@{/index.html(l='en_US')}">English</a>
# src/main/resources/application.properties

# 国际化文件的目录位置
spring.messages.basename=i18n

实现本地化解析器:

# src/main/java/com/wu/config/MyLocaleResolver.java

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

将语言解析器加入到容器中:

# src/main/java/com/wu/config/MyMvcConfig.java

@Configuration // 标注这个类是一个配置类
public class MyMvcConfig implements WebMvcConfigurer {
// 配置视图跳转
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
} // 将本地化解析器配置到容器中
@Bean
public LocaleResolver localeResolver() {
return new MyLocaleResolver();
} // 将视图解析器配置到容器中
@Bean
public ViewResolver myViewResolver() {
return new MyViewResolver();
} // 自定义一个视图解析器
public static class MyViewResolver implements ViewResolver {
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
return null;
}
}
}

SpringBoot 之 国际化的更多相关文章

  1. SpringBoot 国际化配置,SpringBoot Locale 国际化

    SpringBoot 国际化配置,SpringBoot Locale 国际化 ================================ ©Copyright 蕃薯耀 2018年3月27日 ht ...

  2. 配置和修改springboot默认国际化文件

    SpringBoot默认国际化文件为:classpath:message.properties,如果放在其它文件夹中,则需要在application.properties配置属性spring.mess ...

  3. SpringBoot的国际化使用

    在项目中,很多时候需要国际化的支持,这篇文章要介绍一下springboot项目中国际化的使用. 在这个项目中前端页面使用的thymeleaf,另外加入了nekohtml去掉html严格校验,如果不了解 ...

  4. SpringBoot整合国际化功能

    (1).编写国际化配置文件 在resources下新建i18n文件夹,并新建以下文件 ①index.properties   username=username ②index_en_US.proper ...

  5. SpringBoot日记——国际化篇

    听起来高大上的国际化,起始就是在利用浏览器语言,或者页面中的中英文切换,将页面的文字在其他语言和中文进行切换,比如: 我们想让这个功能实现,点击中文,页面就是中文的,点击英文就是英文的. 国际化配置 ...

  6. SpringBoot资源国际化

    Springboot根据浏览器实现网站资源国际化 根据浏览器地区主动选择资源 1.创建资源化文件 resource目录下创建messages目录 创建messages_en_US.properties ...

  7. SpringBoot配置国际化

    1).国际化 1).编写国际化配置文件: 2).使用ResourceBundleMessageSource管理国际化资源文件 3).在页面使用fmt:message取出国际化内容 步骤: 1).编写国 ...

  8. SpringBoot 消息国际化配置

    一.目的 针对不同地区,设置不同的语言信息. SpringBoot国际化配置文件默认放在classpath:message.properties,如果自定义消息配置文件,需要application.p ...

  9. springboot实现国际化

    1.编写配置文件 2.在application.properties中添加 i18n指的是国际化所在包名 3.实现国际化的接口 4.在配置类中

  10. SpringBoot整合国际化I18n

    本文主要实现的功能: 从文件夹中直接加载多个国际化文件 后台设置前端页面显示国际化信息的文件 实现 国际化项目初始化,简单看下项目的目录和文件 在resource下创建国际化文件 messages.p ...

随机推荐

  1. DOM解析xml学习笔记

    一.dom解析xml的优缺点 由于DOM的解析方式是将整个xml文件加载到内存中,转化为DOM树,因此程序可以访问DOM树的任何数据. 优点:灵活性强,速度快. 缺点:如果xml文件比较大比较复杂会占 ...

  2. Apache Log4j 2 报高危漏洞,CODING 联手腾讯安全护卫软件安全

    导语 12 月 9 日晚间,Apache Log4j 2 发现了远程代码执行漏洞,恶意使用者可以通过该漏洞在目标服务器上执行任意代码,危害极大. 腾讯安全第一时间将该漏洞收录至腾讯安全漏洞特征库中,C ...

  3. 控制 Python 类的序列化过程

    问题 有的类是不支持在多进程间传递的,如果非要这么做,可能会引发奇怪的现象.比如下面这段代码: from concurrent.futures import ProcessPoolExecutor, ...

  4. LuoguB2078 含 k 个 3 的数 题解

    Content 给定一个数 \(n\),判断其数位中是否恰好有 \(k\) 个 \(3\). 数据范围:\(1<n\leqslant 10^{15}\),\(1<k\leqslant 15 ...

  5. office2007(word2007)另存为pdf文档

    默认office2007(word2007)是没有另存为pdf文档的功能的,需要安装插件 插件地址:https://yvioo.lanzous.com/iO5myedoceh 下载之后直接安装,安装成 ...

  6. JAVA获取当前请求的URL地址,包含请求链接中的参数

    /** * 获得当前访问的URL路径 * @param request * @return */ public static String getLocation(HttpServletRequest ...

  7. 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  8. 【LeetCode】419. Battleships in a Board 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

  10. codeforces626D . Jerry's Protest

    Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds ...