惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
宝玉的分享
宝玉的分享
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Exploit Database - CXSecurity.com
Help Net Security
Help Net Security
腾讯CDC
Project Zero
Project Zero
C
CXSECURITY Database RSS Feed - CXSecurity.com
IT之家
IT之家
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Threatpost
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
SegmentFault 最新的问题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
B
Blog RSS Feed
Forbes - Security
Forbes - Security
P
Privacy & Cybersecurity Law Blog
Attack and Defense Labs
Attack and Defense Labs
V2EX - 技术
V2EX - 技术
P
Proofpoint News Feed
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
阮一峰的网络日志
阮一峰的网络日志
aimingoo的专栏
aimingoo的专栏
T
Tenable Blog
MyScale Blog
MyScale Blog
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学
W
WeLiveSecurity
D
DataBreaches.Net
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
罗磊的独立博客
The Last Watchdog
The Last Watchdog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Vulnerabilities – Threatpost
美团技术团队
Microsoft Security Blog
Microsoft Security Blog

博客园 - 李小加

rapidocr_api 在windows下安装 MYSQL 创建用户 配置logging.properties解决浏览器访问tomcat日志文件出现中文乱码问题 linux 安装字体解决JAVA图形中文乱码问题 linux 系统上图形生成错误 java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment MySQL 按时间段统计SQL maven 本地库添加jar包 数据接口开发管理平台(starAPI) oracle 11g https://localhost:1158/em 无法访问 & 设置自增id 第三方开源小工具 静态代码检查 mysql 数据库集群连接配置 tomcat server.xml 配置优化 使用PowerDesigner16.5 逆向数据库生产表结构或导出word文档 解决 SpringBoot 跨域请求问题 在 centos7 系统中用 docker 创建 tomcat 服务镜像 JAVA 调用 com.google.protobuf mysql 删除多个字段重复的数据, 只保留最新的一条 Git linux服务端安装、windows 用户端使用
SpringBoot、thymeleaf 国际化配置
李小加 · 2021-09-24 · via 博客园 - 李小加

1、pom.xml 配置

<properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
</properties>
<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、application.properties 配置

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
# set to false for hot refresh
spring.thymeleaf.cache=false 

#指定国际化文件位置
spring.messages.basename=i18n.msg

3、resources 目录下创建 i18n 目录并存放国际化内容文件:

3.1 msg.properties

3.2 msg_zh_CN.properties

3.3 msg_en_US.properties

home.welcome=welcome good!

4、创建解析类  MyLocaleResolver.java

package com.xrh.demo;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        // 接收语言的参数 - 传进来的就是形如'zh_CN'这样的参数
        String lan = request.getParameter("lan");
        // 使用默认的语言 - 在文中就是login.properties文件里配置的
        Locale locale = Locale.getDefault();
        // 判断接收的参数是否为空,不为空就设置为该语言
        if(!StringUtils.isEmpty(lan)){
            // 将参数分隔 - 假设传进来的是'zh_CN'
            String[] s = lan.split("_");
            // 语言编码:zh   地区编码:CN
            locale = new Locale(s[0],s[1]);
        }
        return locale;
    }

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

    }
}

5、加入容器配置中:

package com.xrh.demo;

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.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}

6、controller类方法:

@GetMapping("/success/{name}")
    public String sucTest(@PathVariable("name") String name,Model model){
        Map<String ,Object> map = new HashMap<>();
        map.put("name", name);
        model.addAttribute("map", map);
        
        return "success";
    }

7、模版页面  templates\success.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>thymeleaf</title>
</head>
<body> 
    <h1>成功</h1>
    <div th:text="${name}" th:id="${name}" >这是没有引擎展示的数据</div>
    <div th:text="#{home.welcome}" >国际化内容</div>
</body>
</html>

8、运行效果: