This chapter will show you how to support page template internationalization in Spring Boot and Thymeleaf, automatically reading text in different environments depending on the language in the system locale or session.

Automatic internationalization configuration

Internationalization is automatically configured in Spring Boot.

Internationalization auto-configuration classes:

org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration

View automatic configuration source code has the following main parameters:

private String basename = "messages";

private Charset encoding = Charset.forName("UTF-8");

private int cacheSeconds = -1;

private boolean fallbackToSystemLocale = true;
Copy the code

Basename: The default internationalized file to be scanned is messages, that is, create messages_xx.properties file in resources.

Encoding: The default encoding is UTF-8.

CacheSeconds: Indicates the cache time for loading international files, in seconds. The default value is permanent cache.

FallbackToSystemLocale: If the value is true, the system searches for the resource file corresponding to the current language, such as messages_zh_CN. Properties. If the value is false, the system loads the default resource file, such as messages.properties.

International practice

1. Internationalization configuration

spring:
    messages:
        fallbackToSystemLocale: false
        basename: i18n/common, i18n/login, i18n/index
Copy the code

2. Create the following files in i18n

Properties,index_zh_CN. Properties,index_zh_CN. Properties as the default configuration file when the resource file for the defining language cannot be found.

Create the corresponding key/value, for example:

index_zh_CN.properties

Index. Welcome = welcomeCopy the code

index.properties

index.welcome=welcome
Copy the code

3. Add a language parser and set the default language to US English

The LocaleResolver interface has many implementations, such as the ability to determine the locale from a session, cookie, Accept-language header, or a fixed value.

@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
    sessionLocaleResolver.setDefaultLocale(Locale.US);
    return sessionLocaleResolver;
}
Copy the code

4. Add toggle language filter

private LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
    localeChangeInterceptor.setParamName("lang");
    return localeChangeInterceptor;
}
Copy the code

Add the above filters and register them with Spring MVC

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
}
Copy the code

Then the page accesses the specified URL? Lang =zh_CN for switching.

Use #{} to read the resource file

As in the Thymeleaf template file:

<label th:text="#{index.welcome}"></label>
Copy the code

By default, the resource file in English is read and displays: welcome

Recommended reading

Dry goods: Free 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

Tools: Recommended an online creation flow chart, mind mapping software

Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.