search
HomeJavajavaTutorialHow 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.

How SpringBoot implements internationalization with front-end and back-end separation

The content of the message.properties file can be empty.

message.en_US.properties content example:

40001=Hello

##message.zh_CN.properties content example:

40001=Hello

2. 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 settings

Add the following content to application.properties

#i18n
spring.messages.basename=i18n.messages
spring.messages.cache-duration=3600
spring.messages.encoding=UTF-8

Add the following content to application.yml

spring: 
  messages:
    basename: i18n/messages

4. Controller example

The 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. Summary

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: 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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Java compilation error: How do package declaration and access permissions change after moving the class file?Java compilation error: How do package declaration and access permissions change after moving the class file?Apr 19, 2025 pm 07:12 PM

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

Is JWT suitable for dynamic permission change scenarios?Is JWT suitable for dynamic permission change scenarios?Apr 19, 2025 pm 07:06 PM

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

How to properly configure apple-app-site-association file in pagoda nginx to avoid 404 errors?How to properly configure apple-app-site-association file in pagoda nginx to avoid 404 errors?Apr 19, 2025 pm 07:03 PM

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

What are the differences in the classification and implementation methods of the two consistency consensus algorithms?What are the differences in the classification and implementation methods of the two consistency consensus algorithms?Apr 19, 2025 pm 07:00 PM

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

What is the difference between IS TRUE and =True query conditions in MySQL?What is the difference between IS TRUE and =True query conditions in MySQL?Apr 19, 2025 pm 06:54 PM

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?How to avoid data overwriting and style loss of merged cells when using EasyExcel for template filling?Apr 19, 2025 pm 06:51 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment