Spring Boot tutorial: Internationalization

Internationalization is a processing mechanism that allows your application to adapt to different languages and regions without changing the source code. In other words, internationalization is preparation for localization.
This article will take you to learn the details of how to realize internationalization in Spring Boot.

rely on

To develop web applications in Spring Boot, we need to rely on Spring Boot Starter Web and Spring Boot Starter Thymeleaf dependencies.

Maven

1 <dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-web</artifactId>
4 </dependency>
5 
6 <dependency>
7    <groupId>org.springframework.boot</groupId>
8    <artifactId>spring-boot-starter-thymeleaf</artifactId>
9 </dependency>Gradle

 

Gradle

1 compile('org.springframework.boot:spring-boot-starter-web')
2 compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'

LocaleResolver

To determine the default localization of your application, you need to add a LocaleResolver bean to the Spring Boot application.
1 @Bean
2 public LocaleResolver localeResolver() {
3    SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
4    sessionLocaleResolver.setDefaultLocale(Locale.US);
5    return sessionLocaleResolver;
6 }

LocaleChangeInterceptor

LocaleChangeInterceptor is used to switch the new localization according to the language parameter of the request.
1 @Bean
2 public LocaleChangeInterceptor localeChangeInterceptor() {
3    LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
4    localeChangeInterceptor.setParamName("language");
5    return localeChangeInterceptor;
6 }
To take effect, you need to add LocaleChangeInterceptor to the application registration interceptor. This configuration class should inherit the WebMvcConfigurerAdapter class and overload the addInterceptors() method.
@Override
1 public void addInterceptors(InterceptorRegistry registry) {
2    registry.addInterceptor(localeChangeInterceptor());
3 }

Message resources

The Spring Boot application obtains the message source from the src/main/resources folder under the "classpath" path by default. The default localized message file name should be message Properties, each localized file name should be similar to {messages_XX.properties. "XX" stands for localization code.
All message properties should be expressed as key values. If any attribute is not found in localization, the application starts from messages Get the default value in the properties file.
The default is "messages" Properties are as follows:
welcome.text=Hi Welcome to Everyone
French {messages_cn.properties are as follows:
welcome.text=Salut Bienvenue à tous
Note: the message source file should be stored in "UTF-8" format.

HTML file

In the HTML file, use #{key} syntax to display the message obtained in the attribute file.
<h1 th:text = "#{welcome.text}"></h1>
The complete code is as follows:

Maven – pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
 3 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4    <modelVersion>4.0.0</modelVersion>
 5    <groupId>com.tutorialspoint</groupId>
 6    <artifactId>demo</artifactId>
 7    <version>0.0.1-SNAPSHOT</version>
 8    <packaging>jar</packaging>
 9    <name>demo</name>
10    <description>Demo project for Spring Boot</description>
11  
12    <parent>
13       <groupId>org.springframework.boot</groupId>
14       <artifactId>spring-boot-starter-parent</artifactId>
15       <version>1.5.8.RELEASE</version>
16       <relativePath />
17    </parent>
18  
19    <properties>
20       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21       <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22       <java.version>1.8</java.version>
23    </properties>
24  
25    <dependencies>
26       <dependency>
27          <groupId>org.springframework.boot</groupId>
28          <artifactId>spring-boot-starter-web</artifactId>
29       </dependency>
30  
31       <dependency>
32          <groupId>org.springframework.boot</groupId>
33          <artifactId>spring-boot-starter-test</artifactId>
34          <scope>test</scope>
35       </dependency>
36  
37       <dependency>
38          <groupId>org.springframework.boot</groupId>
39          <artifactId>spring-boot-starter-thymeleaf</artifactId>
40       </dependency>
41    </dependencies>
42  
43    <build>
44       <plugins>
45          <plugin>
46             <groupId>org.springframework.boot</groupId>
47             <artifactId>spring-boot-maven-plugin</artifactId>
48          </plugin>
49       </plugins>
50    </build>
51 </project>

Gradle – build.gradle

 1 buildscript {
 2    ext {
 3       springBootVersion = '1.5.8.RELEASE'
 4    }
 5    repositories {
 6       mavenCentral()
 7    }
 8    dependencies {
 9       classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
10    }
11 }
12 apply plugin: 'java'
13 apply plugin: 'eclipse'
14 apply plugin: 'org.springframework.boot'
15  
16 group = 'com.tutorialspoint'
17 version = '0.0.1-SNAPSHOT'
18 sourceCompatibility = 1.8
19  
20 repositories {
21    mavenCentral()
22 }
23 dependencies {
24    compile('org.springframework.boot:spring-boot-starter-web')
25    compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
26    testCompile('org.springframework.boot:spring-boot-starter-test')
27 }
The main Spring Boot application class file is as follows:
 1 package com.tutorialspoint.demo;
 2  
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5  
 6 @SpringBootApplication
 7 public class DemoApplication {
 8    public static void main(String[] args) {
 9       SpringApplication.run(DemoApplication.class, args);
10    }
11 }
Control documents are as follows:
 1 package com.tutorialspoint.demo.controller;
 2  
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5  
 6 @Controller
 7 public class ViewController {
 8    @RequestMapping("/locale")
 9    public String locale() {
10       return "locale";
11    }
12 }
Configuration classes supporting internationalization:
 1 package com.tutorialspoint.demo;
 2  
 3 import java.util.Locale;
 4  
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.context.annotation.Configuration;
 7 import org.springframework.web.servlet.LocaleResolver;
 8 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 9 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10 import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
11 import org.springframework.web.servlet.i18n.SessionLocaleResolver;
12  
13 @Configuration
14 public class Internationalization extends WebMvcConfigurerAdapter {
15    @Bean
16    public LocaleResolver localeResolver() {
17       SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
18       sessionLocaleResolver.setDefaultLocale(Locale.US);
19       return sessionLocaleResolver;
20    }
21    @Bean
22    public LocaleChangeInterceptor localeChangeInterceptor() {
23       LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
24       localeChangeInterceptor.setParamName("language");
25       return localeChangeInterceptor;
26    }
27    @Override
28    public void addInterceptors(InterceptorRegistry registry) {
29       registry.addInterceptor(localeChangeInterceptor());
30    }
31 }
Message source – messages Properties are as follows:
welcome.text = Hi Welcome to Everyone
Message source – message_ The fr.properties file is as follows:
welcome.text = Salut Bienvenue à tous
HTML file locale HTML should be placed in the templates directory under the classpath path, as shown below:
 1 <!DOCTYPE html>
 2 <html>
 3    <head>
 4       <meta charset = "ISO-8859-1"/>
 5       <title>Internationalization</title>
 6    </head>
 7    <body>
 8       <h1 th:text = "#{welcome.text}"></h1>
 9    </body>
10 </html>
Use Maven or Gradle commands to create executable} JAR files and run Spring Boot applications:
Maven commands are as follows:
mvn clean install
After "BUILD SUCCESS", you can find the JAR file in the target directory.
Gradle can use the following commands:
gradle clean build
You can find the file "build / build" in the "build / build" directory.
Now run the JAR file with the following command:
java –jar <JARFILE>
The application has been started on Tomcat 8080 port, as shown in the following figure:
Enter the URL in the browser http://localhost:8080/locale You can see the following output:
    URL http://localhost:8080/locale?language=fr The output is as follows:
 

Tags: Spring Boot

Posted by glansing on Wed, 18 May 2022 14:52:44 +0300