springboot-springboot thymeleaf and i18N example

1. Introduction

This post would demo how to do internationalization with springboot and thymeleaf:

2. Environments

  • SpringBoot 2
  • Thymeleaf 3

The dependency in maven’s pom.xml

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- hot swapping, disable cache for template, enable live reload -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <addResources>true</addResources>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

3. The example code

3.1 Add configuration bean

You must have a configuration bean to set some configs in springboot:

@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    slr.setDefaultLocale(Locale.ENGLISH); //set the default locale to en
    return slr;
}

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:message"); //specify the message file prefix
    messageSource.setCacheSeconds(60*60); //reload messages every 1 hour
    return messageSource;
}

3.2 Add message properties file

Add these files to src/main/resources: i18n-message-properties-files

Pay attention to the file name prefix, it must be the same as the basename defined in the above messageSource code.

And define a message in the message.properties

title.site=My Great Site

3.3 Use the message in thymeleaf code

<title th:text="#{title.site}">mysite</title>

Then everything is done.

4. Summary

Implement the Internationalization with springboot and thymeleaf is very easy, just be careful with the message file name and the basename of the messageSource definition.