1. Write a multi-language internationalization configuration file
Create a folder named i18n under the project's classpath resources, and write the corresponding multi-language internationalization files login.properties and login_zh_CN in the folder as needed. properties and login_en_US.properties files
login.properties
login.tip=Please log in
login.username=username
login.password=password
login.rememberme=Remember me
login.button=Login
login_zh_CN.properties
login.tip=Please log in
login.username=User Name
login.password=Password
login.rememberme=Remember me
login.button=Login
login_en_US.properties
login. tip=Please sign in
login.username=Username
login.password=Password
login.rememberme=Remember me
login.button=Login
login.properties It is a custom default language configuration file, login_zh_CN.properties is a custom Chinese internationalization file, login_en_US.properties is a custom English internationalization file
It should be noted that the language configuration file recognized by Spring Boot by default is the class messages.properties under the path resources; the names of other language internationalization files must be named strictly in the form of "file prefix name language code country code.properties"
In this example, it is customized under the project class path resources An i18n package is created for unified configuration and management of multi-language configuration files, and the project default language configuration file name is customized as login.properties. Therefore, the internationalized file base name must be configured in the project global configuration file before it can be referenced. Customized internationalization files
2. Write the configuration file
Open the application.properties global configuration file of the project, and add the internationalized file base name setting in the file, with the content such as file
# Configure the internationalized file base name
spring.messages.basename=i18n.login
spring.messages.basename=i18n.login" sets the base name of the custom internationalized file. Among them, i18n means that the internationalized file is relative The location of the project class path resources, login represents the prefix name of the multi-language file. If the developer completely follows the Spring Boot default identification mechanism and writes internationalized files such as messages.properties under the project class path resources, the base name of the internationalized file can be omitted. Configuration
3. Customized regional information parser
After completing the writing and configuration of the multi-language internationalization file in the previous step, you can officially combine the relevant attributes of the Thymeleaf template in the front-end page for internationalization. language settings and display, but this implementation uses the language information in the request header (browser language information) to automatically switch languages by default. Some projects also provide the function of manual language switching, which requires customized regional parsing.
Create a package named com.lagou.config in the project, and create a custom configuration class MyLocalResovel
package com.lagou.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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; @Configuration public class MyLocalResolver implements LocaleResolver { // 完成自定义 区域解析方式 @Override public Locale resolveLocale(HttpServletRequest request) { // 获取页面手动传递的语言参数值 : zh_CN en_US "" String l = request.getParameter("l"); Locale locale = null; if (!StringUtils.isEmpty(l)) { // 如果参数不为空,就根据参数值,进行手动语言切换 String[] s = l.split("_"); locale = new Locale(s[0], s[1]); } else { // Accept-Language: zh-CN,zh;q=0.9 String header = request.getHeader("Accept-Language"); String[] split = header.split(","); String[] split1 = split[0].split("-"); locale = new Locale(split1[0], split1[1]); } return locale; } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { } // 将自定义的LocaleResolver重新注册成一个类型为LocaleResolver的Bean组件 @Bean public LocaleResolver localeResolver() { return new MyLocalResolver(); } }# under this package for customizing the internationalized functional area information parser. ##MyLocalResolver The custom regional resolver configuration class implements the LocaleResolver interface, and rewrites the resolveLocale() method for custom language resolution. Finally, the @Bean annotation is used to register the current configuration class as a LocaleResolver in the Spring container. Bean component, so that the default LocaleResolver component can be overridden. Among them, in the resolveLocale() method, the request parameter l and the request header parameter are obtained respectively according to different needs (manual switching of language information, browser request header automatic switching of language information) Accept-Language, and then when the request parameter l is not empty, the language carried by the l parameter will be used as the standard for language switching. Otherwise, automatic switching will be customized through the request header information. It should be noted that, When manually switching the language of the request parameter l, the underscore "_" is used for cutting, which is determined by the format of the multi-language configuration file (such as login_zh_CN.properties); while in the language of the request header parameter Accept-Language When automatically switching assembly, the dash "-" is used for cutting, which is determined by the request header information style sent by the browser (for example, Accept-Language: en-US,en;q=0.9,zh-CN ;q=0.8,zh;q=0.7)4. Page internationalization usesOpen the user login page login.html in the project templates template folder, and combine it with the Thymeleaf template engine to implement the internationalization function
<!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,shrinkto- fit=no"> <title>用户登录界面</title> <link th:href="@{/login/css/bootstrap.min.css}" rel="external nofollow" rel="stylesheet"> <link th:href="@{/login/css/signin.css}" rel="external nofollow" rel="stylesheet"> </head> <body class="text-center"> <!-- 用户登录form表单 --> <form class="form-signin"> <img class="mb-4 lazy" src="/static/imghwm/default1.png" data-src="@{/login/img/login.jpg}" th: style="max-width:90%" style="max-width:90%" alt="How to configure internationalization page in Spring boot Thymeleaf" > <h2 id="请登录">请登录</h2> <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(l='zh_CN')}" rel="external nofollow" >中文</a> <!--注意这个路径是我们controller访问页面的路径,不同的是这次携带了参数,可以帮我们用来区分locale--> <a class="btn btn-sm" th:href="@{/toLoginPage(l='en_US')}" rel="external nofollow" >English</a> </form> </body> </html>Use the #{} message expression of the Thymeleaf template Set up some information for international display. When setting the rememberme internationalization, the rememberme that needs to be internationalized is outside the input tag, so the inline expression [[#login.rememberme}]] is used here to dynamically obtain the login.rememberme information in the internationalization file. In addition, a functional link for manual language switching between Chinese and English is provided at the end of the form. When the link is clicked, the national language parameters will be carried to request a jump to the "/" path, and manual language switching will be performed through the customized regional parser in the background. Note: The controller control layer processing is omitted5. Integration effect test
When clicking the "English" link for language internationalization switching, the specified "l=zh_CN" parameter is carried. The parsing method in the background customized regional parser configuration class MyLocalResovel will be based on Customize rules for language switching, thereby achieving the effect of manual switching of international languages
The above is the detailed content of How to configure internationalization page in Spring boot Thymeleaf. For more information, please follow other related articles on the PHP Chinese website!

Canal工作原理Canal模拟MySQLslave的交互协议,伪装自己为MySQLslave,向MySQLmaster发送dump协议MySQLmaster收到dump请求,开始推送binarylog给slave(也就是Canal)Canal解析binarylog对象(原始为byte流)MySQL打开binlog模式在MySQL配置文件my.cnf设置如下信息:[mysqld]#打开binloglog-bin=mysql-bin#选择ROW(行)模式binlog-format=ROW#配置My

前言SSE简单的来说就是服务器主动向前端推送数据的一种技术,它是单向的,也就是说前端是不能向服务器发送数据的。SSE适用于消息推送,监控等只需要服务器推送数据的场景中,下面是使用SpringBoot来实现一个简单的模拟向前端推动进度数据,前端页面接受后展示进度条。服务端在SpringBoot中使用时需要注意,最好使用SpringWeb提供的SseEmitter这个类来进行操作,我在刚开始时使用网上说的将Content-Type设置为text-stream这种方式发现每次前端每次都会重新创建接。最

一、手机扫二维码登录的原理二维码扫码登录是一种基于OAuth3.0协议的授权登录方式。在这种方式下,应用程序不需要获取用户的用户名和密码,只需要获取用户的授权即可。二维码扫码登录主要有以下几个步骤:应用程序生成一个二维码,并将该二维码展示给用户。用户使用扫码工具扫描该二维码,并在授权页面中授权。用户授权后,应用程序会获取一个授权码。应用程序使用该授权码向授权服务器请求访问令牌。授权服务器返回一个访问令牌给应用程序。应用程序使用该访问令牌访问资源服务器。通过以上步骤,二维码扫码登录可以实现用户的快

1.springboot2.x及以上版本在SpringBoot2.xAOP中会默认使用Cglib来实现,但是Spring5中默认还是使用jdk动态代理。SpringAOP默认使用JDK动态代理,如果对象没有实现接口,则使用CGLIB代理。当然,也可以强制使用CGLIB代理。在SpringBoot中,通过AopAutoConfiguration来自动装配AOP.2.Springboot1.xSpringboot1.xAOP默认还是使用JDK动态代理的3.SpringBoot2.x为何默认使用Cgl

我们使用jasypt最新版本对敏感信息进行加解密。1.在项目pom文件中加入如下依赖:com.github.ulisesbocchiojasypt-spring-boot-starter3.0.32.创建加解密公用类:packagecom.myproject.common.utils;importorg.jasypt.encryption.pbe.PooledPBEStringEncryptor;importorg.jasypt.encryption.pbe.config.SimpleStrin

知识准备需要理解ApachePOI遵循的标准(OfficeOpenXML(OOXML)标准和微软的OLE2复合文档格式(OLE2)),这将对应着API的依赖包。什么是POIApachePOI是用Java编写的免费开源的跨平台的JavaAPI,ApachePOI提供API给Java程序对MicrosoftOffice格式档案读和写的功能。POI为“PoorObfuscationImplementation”的首字母缩写,意为“简洁版的模糊实现”。ApachePOI是创建和维护操作各种符合Offic

1.首先新建一个shiroConfigshiro的配置类,代码如下:@ConfigurationpublicclassSpringShiroConfig{/***@paramrealms这儿使用接口集合是为了实现多验证登录时使用的*@return*/@BeanpublicSecurityManagersecurityManager(Collectionrealms){DefaultWebSecurityManagersManager=newDefaultWebSecurityManager();

一、定义视频上传请求接口publicAjaxResultvideoUploadFile(MultipartFilefile){try{if(null==file||file.isEmpty()){returnAjaxResult.error("文件为空");}StringossFilePrefix=StringUtils.genUUID();StringfileName=ossFilePrefix+"-"+file.getOriginalFilename(


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
