With the development of globalization, more and more websites and applications need to provide multi-language support and internationalization functions. For developers, implementing these functions is not an easy task because it requires consideration of many aspects, such as language translation, date, time and currency formats, etc. However, using the Spring Boot framework, we can easily implement multi-language support and international applications.
First, let us understand the LocaleResolver interface provided by Spring Boot. The LocaleResolver interface is an interface used to parse and return Locale objects. The Locale object represents the language, region and related information used by the user. In Spring Boot, the LocaleResolver interface has two implementations: CookieLocaleResolver and AcceptHeaderLocaleResolver.
CookieLocaleResolver parses Locale objects through browser cookies. When the user selects a language, the Locale object will be set to the browser and saved in a cookie. Therefore, the next time the user visits the website, CookieLocaleResolver is able to read the Locale object from the cookie.
AcceptHeaderLocaleResolver parses the Locale object through the Accept-Language field in the HTTP request header. When the user sends a request with the Accept-Language header, AcceptHeaderLocaleResolver will parse out the Locale object and return it.
In addition to the LocaleResolver interface, Spring Boot also provides the MessageSource interface. The MessageSource interface is used to parse and process multi-language messages. In Spring Boot, the MessageSource interface has two implementations: ResourceBundleMessageSource and ReloadableResourceBundleMessageSource.
ResourceBundleMessageSource is a simple implementation that parses multilingual messages from a provided properties file. The properties file contains key-value pairs, where the key is the message identifier and the value is the message itself. For example, for English and Chinese messages, the properties file may look like this:
# messages_en_US.properties hello=Hello world! # messages_zh_CN.properties hello=你好,世界!
When we need to display a message to the user, we can use the MessageSource interface to obtain the corresponding message, and then display it to the user .
ReloadableResourceBundleMessageSource is very similar to ResourceBundleMessageSource, but it allows us to reload the properties file while the application is running. This is useful for applications that require frequently updated messages.
When implementing multi-language support and international applications, we usually need to consider date, time and currency format issues. Spring Boot provides a FormattingConversionServiceFactoryBean class to solve these problems. We can use it to define date, time, and currency formats and make them valid in multilingual applications.
The following is an example of using the FormattingConversionServiceFactoryBean class to define date, time and currency formats:
@Configuration public class AppConfig { @Bean public FormattingConversionServiceFactoryBean formattingConversionServiceFactoryBean() { FormattingConversionServiceFactoryBean factoryBean = new FormattingConversionServiceFactoryBean(); factoryBean.setRegisterDefaultFormatters(false); factoryBean.setFormatters(Set.of(dateTimeFormatter(), dateFormatter(), numberFormatter())); return factoryBean; } private DateTimeFormatter dateTimeFormatter() { return DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withLocale(LocaleContextHolder.getLocale()); } private DateTimeFormatter dateFormatter() { return DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(LocaleContextHolder.getLocale()); } private NumberFormatter numberFormatter() { return new NumberFormatter(NumberFormat.getCurrencyInstance(LocaleContextHolder.getLocale())); } }
In the above code, we create a FormattingConversionServiceFactoryBean to define date, time and currency formats. We use DateTimeFormatter and NumberFormatter to define date time and currency formats, and use the LocaleContextHolder.getLocale() method to obtain the current user's Locale object.
Finally, we need to set up the LocaleResolver and MessageSource in our application to support multiple languages. We can achieve this using the @EnableWebMvc and @Configuration annotations provided by Spring Boot. The following is a sample configuration class:
@Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Bean public LocaleResolver localeResolver() { AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver(); resolver.setDefaultLocale(Locale.US); return resolver; } @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasenames("messages"); messageSource.setFallbackToSystemLocale(false); return messageSource; } @Override public void addInterceptors(InterceptorRegistry registry) { LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); localeChangeInterceptor.setParamName("lang"); registry.addInterceptor(localeChangeInterceptor); } }
In the above code, we create a WebConfig configuration class and enable the Spring MVC feature using the @EnableWebMvc annotation. We use localeResolver() Bean and messageSource() Bean to set LocaleResolver and MessageSource. We also added a LocaleChangeInterceptor using the addInterceptors() method, and in this interceptor we used the "lang" parameter to switch the user's language.
To sum up, we can easily implement multi-language support and international applications through the LocaleResolver interface, MessageSource interface and FormattingConversionServiceFactoryBean class provided by Spring Boot. Using the above methods, we can more easily and conveniently build a global application that meets the different language, culture, and regional needs of users.
The above is the detailed content of Achieve multi-language support and international applications through Spring Boot. For more information, please follow other related articles on the PHP Chinese website!