Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize internationalization of SpringBoot

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to realize the internationalization of SpringBoot related knowledge, detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone will have a harvest after reading this article on how to realize the internationalization of SpringBoot, let's take a look at it.

I. Foreword

Internationalization may not be a common feature, but it is still necessary where there is a need. Today, let's take a look at how to configure internationalization in our web development, so that our website can display different forms according to language.

Second, international configuration 2.1 automatic configuration in springboot

Springboot has automatically configured the components that manage internationalized resource files:

@ConfigurationProperties(prefix = "spring.messages")public class MessageSourceAutoConfiguration { /** * Comma-separated list of basenames (essentially a fully-qualified classpath * location), each following the ResourceBundle convention with relaxed support for * slash based locations. If it doesn't contain a package qualifier (such as * "org.mypackage"), it will be resolved from the classpath root. */ private String basename = "messages"; //our configuration file can be placed directly under the classpath called messages.properties; @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); if (StringUtils.hasText(this.basename)) { //Set the base name of the internationalized resource file (remove the language country code) messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray( StringUtils.trimAllWhitespace(this.basename))); } if (this.encoding != null) { messageSource.setDefaultEncoding(this.encoding.name()); } messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale); messageSource.setCacheSeconds(this.cacheSeconds); messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat); return messageSource; }

From the above source code we can see that our internationalized resource file can be directly named messages.properties. Springboot will automatically recognize it. In fact, it is equivalent to adding a spring.messages.basename=messages to the configuration file. If we specify a xxx.properties as an internationalized file, then we will specify

spring.messages.basename=xxx, springboot will automatically find properties starting with xxx, according to the language and country code, find the corresponding xxx_zh_CN.properties (Chinese), xxx_en_US.properties (English_US) to select the resource value as the value to be rendered in the current page.

2.2 Add Resource Files

We create a new folder named i18n, and add three configuration files in it, namely login.properties(default configuration without language selection), login_zh_CN.properties (Chinese language configuration), login_en_US.properties (English language configuration). The default format is: filename_region_language.properties; When we name the generated file this way, IDEA will also help us identify that this is an international configuration package, automatically converted to the following mode, with a Resource Bundle 'login' folder, right click on this folder, you can easily add other language configurations:

Write the configuration in three configuration files:

login.properties:

login.btn= login ~login.password= password ~login.remember= remember me ~login.tip= please login ~login.username= username ~

login_zh_CN.properties:

login.tip= login.username= username login.password= password login.btn= login.remember= remember me

login_en_US.properties:

login.tip=Please sign inlogin.username=Usernamelogin.password=Passwordlogin.btn=Sign inlogin.remember = Remember Me

Then add the following configuration to our main configuration file application.properties to enable our custom resource files:

spring.messages.basename=i18n.login2.3 Add login page

Here we test this by adding a login page using the thymeleaf syntax:

Signin Template for Bootstrap

Please sign in

Username Password [[#{login.remember}]] Sign in © 2017-2018

Chinese English

#{login.username} will get the value we configured directly from the internationalized resource file. It will switch to different language configurations according to different languages. The default is determined according to the browser language. We can set the browser language to view the effect in the following way. Search for languages in Google browser settings, and then add an English language. Set the preferred language in the order of setting languages, as follows:

We can also check the header of the request sent by the browser to determine if the language was set successfully:

2.4 Custom Language Culture Parser

From the above we can see that the default is to parse the language culture from the request header, we can set our own parser, such as the query parameters of the visit page to parse the language, below is our login page, we can add two buttons, when clicking the button, re-jump to the index.html page, and attach a lang parameter:

You can view the source code of the web page, we generated links:

html key code, generated with thymeleaf, can also be handwritten:

Sign in© 2017-2018

Chinese English

Add our own parser MyLocaleResolver, get lang from the request parameters, and set different Locales depending on the value of lang:

package com.example.demo.component;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;public class MyLocaleResolver implements LocaleResolver { @Override public Locale resolveLocale(HttpServletRequest httpServletRequest) { String lang = httpServletRequest.getParameter("lang"); Locale locale = Locale.getDefault(); if(! StringUtils.isEmpty(lang)){ String[] parts = lang.split("_"); locale = new Locale(parts[0],parts[1]); } return locale; } @Override public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { }}

Add the following code to WebConfig, add this MyLocaleResolver to the container, springboot finds a new instance of LocaleResolver, it will use this parser:

Recompile and start it, click on different languages to jump to different links, showing different languages:

The content of this article on "How to internationalize SpringBoot" is introduced here. Thank you for reading it! I believe everyone has a certain understanding of "how to internationalize SpringBoot" knowledge. If you still want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report