Home > Java > javaTutorial > How SpringBoot implements internationalization with front-end and back-end separation

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

王林
Release: 2023-05-14 22:49:04
forward
1710 people have browsed it

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);
			}
		};
	}
}
Copy after login

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
Copy after login

Add the following content to application.yml

spring: 
  messages:
    basename: i18n/messages
Copy after login

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;
	}

}
Copy after login
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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template