


How SpringBoot implements internationalization with front-end and back-end separation
Preface
Springboot internationalization can help users build applications in different language environments, so that applications can effectively adapt to user needs in different language and cultural backgrounds.
In addition, Springboot internationalization can also facilitate the reuse and maintenance of multi-language applications, thereby reducing the time cost of system deployment and maintenance costs.
To implement Springboot international application, there are three main steps.
1. Set international property files
Define international resource files, use properties format files, put different multi-language text resources in different files, and name each file Use [locale] [messages] method, such as zh_CN.properties, en_US.properties, etc.
The content of the message.properties file can be empty.
message.en_US.properties content example:
##message.zh_CN.properties content example:40001=Hello
40001=Hello2. Create parser and interceptor
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
@Configuration
public class LocaleConfig {
@Bean
public SessionLocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.CHINA);
return localeResolver;
}
@Bean
public WebMvcConfigurer localeInterceptor() {
return new WebMvcConfigurer() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
localeInterceptor.setParamName("lang");
registry.addInterceptor(localeInterceptor);
}
};
}
}
3. Start configuration file settingsAdd the following content to application.properties#i18n spring.messages.basename=i18n.messages spring.messages.cache-duration=3600 spring.messages.encoding=UTF-8Add the following content to application.yml
spring: messages: basename: i18n/messages4. Controller exampleThe internationalization of Springboot is implemented through a bean called "messageSource".
import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class TestControler { @Autowired private MessageSource messageSource; @GetMapping("/hello") public Map<Object, Object> test() { Map<Object, Object> result = new HashMap<Object, Object>(); result.put("code", 40001); result.put("msg", messageSource.getMessage("40001", null, LocaleContextHolder.getLocale())); return result; } }5. SummarySpringboot internationalization can help users build applications in different language environments, so that applications can effectively adapt to user needs in different language and cultural backgrounds. In addition, Springboot internationalization can also facilitate the reuse and maintenance of multi-language applications, thereby reducing the time cost of system deployment and maintenance costs. To implement Springboot international application, there are three main steps. 1. Set international property files: To implement Springboot internationalization, you must first prepare a series of international property files, including language and region information. 2. Register internationalized message resources: Register the language and region information in the properties file into the Springboot application, and specify default values so that you can quickly and efficiently access related property resources when expanding to multiple languages. . 3. Define multi-language bundle files: Combine the defined internationalization property files with the application to form a multi-language bundle file so that the application language version can be adjusted in time when the user selects a different language.
The above is the detailed content of How SpringBoot implements internationalization with front-end and back-end separation. For more information, please follow other related articles on the PHP Chinese website!

Packages and Directories in Java: The logic behind compiler errors In Java development, you often encounter problems with packages and directories. This article will explore Java in depth...

Leetcode ...

JWT and Session Choice: Tradeoffs under Dynamic Permission Changes Many Beginners on JWT and Session...

How to correctly configure apple-app-site-association file in Baota nginx? Recently, the company's iOS department sent an apple-app-site-association file and...

How to understand the classification and implementation methods of two consistency consensus algorithms? At the protocol level, there has been no new members in the selection of consistency algorithms for many years. ...

mybatis-plus...

The difference between ISTRUE and =True query conditions in MySQL In MySQL database, when processing Boolean values (Booleans), ISTRUE and =TRUE...

How to avoid data overwriting and style loss of merged cells when using EasyExcel for template filling? Using EasyExcel for Excel...


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

Notepad++7.3.1
Easy-to-use and free code editor

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

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment