如何在springboot中实现页面的国际化

确保这个修改是正确的(否则将会出现乱码)

创建i18n文件夹(就是国际化的意思),然后在此文件加下创login.properties login_zh_CN,properties文件,然后他会自动合并,创建一个文件夹

然后就可以从这里创建文件了

创建方式如图所示,他会简单一点

配置properties文件

输入对应的中英文


这里就不全部列举了
我们看看源码(两下shift)


然后再把index.html改一下
去页面获取国际化的值,查看Thymeleaf的文档,找到message取值操作为:#{…}

这里一定要仔细,要敲错代码,否则会发生错误

我们可以看到,usingname和passname仍然是英文没有改变

这样就可以了

如何实现切换?

先在前端增加请求

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class MyLocaleResolver implements LocaleResolver {
    //解析请求
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String language = httpServletRequest.getParameter("l");
        Locale locale = Locale.getDefault(); // 如果没有获取到就使用系统默认的
        //如果请求链接不为空
        if (!StringUtils.isEmpty(language)){
            //分割请求参数
            String[] split = language.split("_");
            //国家,地区
            locale = new Locale(split[0],split[1]);
        }
        return locale;

    }

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

    }
}

到此这篇关于如何在springboot中实现页面的国际化的文章就介绍到这了,更多相关springboot页面国际化内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • SpringBoot 利用thymeleaf自定义错误页面

    导入thymeleaf <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 自定义异常类 建立监听异常类 MyException.class package com.example.demo.domain; public class My

  • SpringBoot参数校验与国际化使用教程

    一.参数校验 springboot 使用校验框架validation校验方法的入参 SpringBoot的Web组件内部集成了hibernate-validator,所以我们这里并不需要额外的为验证再导入其他的包. 1.bean 中添加标签 标签需要加在属性上,@NotEmpty标签String的参数不能为空 @Data public class DemoDto { @NotEmpty(message = "名称不能为空") private String name; @Length(m

  • Springboot 使用 JSR 303 对 Controller 控制层校验及 Service 服务层 AOP 校验 使用消息资源文件对消息国际化

    导包和配置 导入 JSR 303 的包.hibernate valid 的包 <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.5.Final</version> </dependency> <dependency> <

  • springboot返回modelandview页面的实例

    1.添加依赖 这个应该是web项目相关的jar <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- jstl JSP标准标签库 --> <dependency> <groupId>javax.servle

  • SpringBoot消息国际化配置实现过程解析

    一.目的 针对不同地区,设置不同的语言信息. SpringBoot国际化配置文件默认放在classpath:message.properties,如果自定义消息配置文件,需要application.properties或application.yml中设置spring.messages.basename的值. 二.步骤 在src/main/resources 下建i18n文件夹 在i18n文件夹中建立messages.properties 找不到语言配置时,使用此文件 hello=你好_默认 在

  • springboot+thymeleaf国际化之LocaleResolver接口的示例

    springboot中大部分有默认配置所以开发起项目来非常迅速,仅对需求项做单独配置覆盖即可 spring采用的默认区域解析器是AcceptHeaderLocaleResolver,根据request header中的accept-language值来解析locale,并且是不可变的. 那么想要实现国际化,就要使用SessionLocaleResolver或者CookieLocaleResolver.正如类的名字所示,是按session或cookie中储存的locale值来解析locale. 我

  • SpringBoot实现国际化过程详解

    这篇文章主要介绍了SpringBoot实现国际化过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 实现方法:thymeleaf模板引擎加上BootStrap 准备工作: 1.将准备好的Bootstrap模板放在templates下让SpringBoot进行自动配置 SpringBoot自动配置会自动到(idea的shif键连按两下进入全局搜索) 2.Bootstrp的引入(这里是maven以depency的方式引入) <!--引入boot

  • springboot+vue实现页面下载文件

    本文实例为大家分享了springboot+vue页面下载文件的具体代码,供大家参考,具体内容如下 1.前端代码: <template v-slot:operate="{ row }"> <vxe-button style="color: #409eff; font-weight: bolder" class="el-icon-download" title="成果下载" circle @click="

  • SpringBoot2.3定制错误页面的方法示例

    一. 问题背景 后台: SpringBoot 2.3.1(官方2.3版本修改了很多,抛弃了很多以前能用的方法) 前端: Layui(前端用哪个框架问题不大) 技术: SpringBoot+Thymeleaf+Layui 情况:我想将Layui提供好的错误页面作为SpringBoot默认的错误页面,而且Layui提供的错误页面位置并不是放在/静态资源文件夹/error,而是在如下: 二. SpringBoot的错误页面机制 错误页面机制的原理详情可以看Day41--错误处理原理&定制错误页面以及D

  • Springboot访问templates html页面过程详解

    springboot项目默认是不允许直接访问templates下的文件的,是受保护的. 如果要访问templates下的文件,推荐使用thymeleaf. 注:使用thymeleaf这一点要牢牢记住! 如何使用: 1.pom依赖 <!--thymeleaf 模板依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter

  • Springboot访问html页面步骤解析

    springboot中如果想访问html页面,不每访问一个页面就写一个Controller,可以统一写一个公共的controller方法 代码: (1)引入hutool工具依赖 <!-- hutool工具包--> <dependency> <groupId>com.xiaoleilu</groupId> <artifactId>hutool-all</artifactId> <version>3.3.0</versi

  • 详解如何配置springboot跳转html页面

    本文主要介绍了如何配置springboot跳转html页面,分享给大家,具体如下: 1.首先在pom文件中引入模板引擎jar包,即: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 2.在application.properties中

  • Springboot如何使用Map将错误提示输出到页面

    主要思路:在controller层我们将错误信息put进map中,然后通过视图解析器跳转到目标页面,在目标页面中在通过指定标签内的th:text将错误消息取出. 例: 1.编写controller代码 @PostMapping("/user/login") public String login(@RequestParam("username") String username, @RequestParam("password") String

随机推荐