Spring boot thymeleaf 实现简单-页面国际化

2023-11-19

新建Spring boot项目

在这里插入图片描述

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ghgcn</groupId>
    <artifactId>boot-task03-thmeleaf</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-task03-thmeleaf</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <!--不使用spring-boot-starter-parent-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.8.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Controller与启动类

@Controller
public class LoginController {

    @RequestMapping("/toLoginPage")
    public String toLoginPage(Model model){

        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "login";
    }


}

@SpringBootApplication
public class BootTask03ThmeleafApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootTask03ThmeleafApplication.class, args);
    }

}

自定义LocaleResolver


@Configuration
public class MyLocaleResovel implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取语言参数 -自定义传的参数
        String language = request.getParameter("language");
        //Accept-Language: zh-CN,zh;q=0.9 浏览器请求头中的参数
        String header = request.getHeader("Accept-Language");

        Locale locale=null;

        if(!StringUtils.isEmpty(language)){
            String[] split = language.split("_");
            locale = new Locale(split[0],split[1] );
        }else {
            //Accept-Language: zh-CN,zh;q=0.9
            //zh-CN,zh;q=0.9
            String[] splits = header.split(",");

            //zh,CN,
            String[] split = splits[0].split("-");

            locale = new Locale(split[0],split[1] );
        }

        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

        System.out.println(locale);

    }

//用来覆盖Spring默认的
    @Bean
    public MyLocaleResovel localeResolver(){

        return new MyLocaleResovel();
    }
}
i18n文件

在resources目录下新建i18n文件夹
在这里插入图片描述

  • login.properties
login.tip=请登录
login.username=用户名
login.password=密码
login.rememberme=记住我
login.button=登录
login.chinese=中文
login.en=English
  • login_en_US.properties
login.tip=Please sign in
login.username=Username 
login.password=password
login.rememberme=Remember me
login.button=Login
login.chinese=Chinese
login.en=English
  • login_zh_CN.properties
login.tip=请登录
login.username=用户名
login.password=密码
login.rememberme=记住我
login.button=登录
login.chinese=中文
login.en=English

login.properties 是自定义的默认语言配置文件
login_en_US.properties 是英文
login_zh_CN.properties 是中文

Spring Boot默认识别的语言文件为类路径resources下的messages.properties,其他语言国际化文件的名称必须严格按照

文件名前缀名_语言代码_国家代码.properties
形式命名

上面是在resources下自定义了一个i18n文件夹下统一配置管理多语言配置文件,进行国际化文件文件基础名配置,才可以引用国际化文件

在application.properties全局配置文件中添加国际化文件基础名设置
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.mode=HTML
spring.thymeleaf.enabled=true
spring.thymeleaf.suffix=.html


server.port=8099
server.servlet.context-path=/

spring.messages.encoding=UTF-8
spring.messages.basename=i18n.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">
    <title>用户登录界面</title>
    <link th:href="@{/login/css/bootstrap.min.css}" rel="stylesheet">
    <link th:href="@{/login/css/signin.css}" rel="stylesheet">

</head>
<body class="text-center">
<!--  用户登录form表单 -->
<form class="form-signin">
   <!-- <img class="mb-4" th:src="@{/login/img/login.jpg}" width="72" height="72">-->
    <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}" >请登录</h1>

    <input type="text" class="form-control"
           th:placeholder="#{login.username}" required="" autofocus="">
    <input type="password" class="form-control"
           th:placeholder="#{login.password}" required="">
    <div class="checkbox mb-3">
        <label>
            <input type="checkbox" value="remember-me"> [[#{login.rememberme}]]
        </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.button}">登录</button>
    <p class="mt-5 mb-3 text-muted">© <span th:text="${currentYear}">2019</span>-<span th:text="${currentYear}+1">2020</span></p>
    <a class="btn btn-sm" th:href="@{/toLoginPage(language='zh_CN')}" th:text="#{login.chinese}">中文</a>
    <a class="btn btn-sm" th:href="@{/toLoginPage(language='en_US')}" th:text="#{login.en}">English</a>

</form>
</body>
</html>
启动项目

http://localhost:8099/toLoginPage
在这里插入图片描述
点击英文
在这里插入图片描述

  • 点中文
    在这里插入图片描述

到这里基本完成
https://gitee.com/null_631_9084/myhomework/tree/master/stage01-springboot/boot-task03-thmeleaf

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring boot thymeleaf 实现简单-页面国际化 的相关文章

随机推荐

  • C++ 实例化对象

    实例化对象 意味着一定有调用构造函数 实例化就是给 数据成员分配内存 构造对象 对象的成员函数和普通函数的区别就是 成员函数有个指向当前对象的this指针 可以访问对象的成员变量 其依赖于对象 静态函数就更像一个全局函数 没有this指针
  • 1031 查验号码

    一个号码由17位地区 日期编号和顺序编号加1位校验码组成 校验码的计算规则如下 首先对前17位数字加权求和 权重分配为 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 然后将计算的和对11取模得到值Z 最后按照以下关
  • 数字经济发展报告 附下载地址

    数字经济是以数字化的知识和信息作为关键生产要素 以数字技术为核心驱动力量 以现代信息网络为重要载体 通过数字技术与实体经济深度融合 不断提高经济社会的数字化 网络化 智能化水平 加速重构经济发展与治理模式的新型经济形态 关注公众号 互联互通
  • Java程序员编写代码的技巧

    这样说吧 系统学Java底层 是大多数Java初学者都会缴的智商税 为什么这样说呢 1 初级开发做的是增删改查 没必要了解底层 了解了对开发帮助也不大 2 中级开发要求的是熟悉业务 能排查大多数问题 这时也无需系统学习底层技能 3 架构师确
  • 那些年Google公开的大数据领域论文

    摘要 Google于2004年公布了MapReduce论文 为数据领域工作者开启了大数据算法之门 然而Google的大数据脚步显然不止于此 其后公布了Percolator Pregel Dremel Spanner等多篇论文 没有止步的不仅
  • K - Birthday Puzzle Gym - 102267K (遍历子集的位运算)

    Today is the Birthday of a beautiful girl she s happy and she s telling her friends loudly to bring her birthday gifts O
  • 递归方法相关题目

    目录 70 爬楼梯 70 爬楼梯 简介 这里是java的解法 描述 假设你正在爬楼梯 需要 n 阶你才能到达楼顶 每次你可以爬 1 或 2 个台阶 你有多少种不同的方法可以爬到楼顶呢 注意 给定 n 是一个正整数 示例 1 输入 2 输出
  • 使用 Java 操作 Git-验证相关

    背景 日常工作中 有时候需要用代码去操作gitlab 或者github实现自动化部署 持续集成 持续部署等功能 本文就 Java 操作 git 相关的权限验证进行实践总结 工具 使用的工具为eclipse的jgit pom为目前最新版本
  • Unity3d提升效率的高级技巧(二)

    11 在层次视图中选中某个游戏对象 按快捷键 Cmd Ctrl D 即可复制该对象 对于检视面板中的数组字段也可已同样的方式来复制元素 12 检视面板中所有的颜色字段都是支持复制和粘贴的 只需右键点击颜色字段既可选择操作 13 如果觉得在场
  • Java的基础(面向对象、字符串、数学相关的工具类)

    面向对象 面向过程 当需要实现一个功能的时候 每一具体的步骤都要亲力亲为 详细处理每一个细节 强调步骤 面向对象 当需要实现一个功能时 不关心具体步骤 而是找一个已经具有该功能的人 来帮我做事 强调对象 三大特征 封装性 继承性 多态 类
  • 题目 1048: [编程入门]自定义函数之字符串拷贝(三个方案)

    有一字符串 包含n个字符 写一函数 将此字符串中从第m个字符开始的全部字符复制成为另一个字符串 方案一 include
  • android插件化flutter,Android端Flutter插件开发

    一 简介 此文章主要记录本人的flutter插件开发过程以及遇到的问题等 如有错误请指正 二 开发准备 Windows 1 Android Studio 4 0以上 2 Flutter SDK 三 环境配置 1 安装flutter sdk
  • 扫地机器人朋友圈文案_扫地机器人的简单文案

    扫地机器人哪个牌子好 推荐两款性价比高的实用机型 现如今 很多消费者在购买扫地机器人时 越来越看重产品的性价比 基于用户的消费需求 本文精选了素有 性价比之王 之称的国际大品牌德国斐纳TOMEFON旗下的两款实用扫地机器人机型 供大家选择
  • 未来的智能制造,或许会往这些方向推进

    智能制造 源于人工智能的研究 一般认为智能是知识和智力的总和 前者是智能的基础 后者是指获取和运用知识求解的能力 很多人想要了解智能制造未来的推进方向 今天小编就带大家来了解一下 希望大家能对智能制造多一些认识 能给大家带来帮助 一 更高效
  • mingw qt5.14.2 编译 vtk9.1.0

    1 软件 qt版本 qt opensource windows x86 5 14 2 exe 安装mingw选项 cmake版本 cmake 3 21 2 windows x86 64 msi VTK源码版本 VTK 9 1 0 tar g
  • the type or namespace name ‘xxx‘ could not be found(vs for Mac)解决方法

    在visual studio for Mac中遇到 the type or namespace name xxx could not be found are you missing a using derective or an asse
  • Linux下 mysql8大小写敏感问题

    说明 已经在linux上安装过了mysql 通过以下步骤解决 1 先查看mysql的运行状态 service mysql status 如果为运行状态 则停止 service mysql stop 2 修改 etc my cnf文件 添加l
  • SpringBoot生成docker镜像,完成容器部署

    docker介绍 Docker 是一个开源的应用容器引擎 基于 Go语言 并遵从Apache2 0协议开源 Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级 可移植的容器中 然后发布到任何流行的 Linux 机器上 也可以实现
  • Go 字符串处理

    一 字符串处理函数 我们从文件中将数据读取出来以后 很多情况下并不是直接将数据打印出来 而是要做相应的处理 例如 去掉空格等一些特殊的符号 对一些内容进行替换等 这里就涉及到对一些字符串的处理 在对字符串进行处理时 需要借助于包 strin
  • Spring boot thymeleaf 实现简单-页面国际化

    新建Spring boot项目 pom xml文件