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.
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); } }; } }
#i18n spring.messages.basename=i18n.messages spring.messages.cache-duration=3600 spring.messages.encoding=UTF-8
spring: messages: basename: i18n/messages
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; } }
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!