Guide to Internationalization in Spring Boot – Spring Boot中的国际化指南

最后修改: 2017年 3月 18日

1. Overview


In this quick tutorial, we’re going to take a look at how we can add internationalization to a Spring Boot application.

在这个快速教程中,我们将看看如何向Spring Boot应用程序添加国际化

2. Maven Dependencies


For development, we need the following dependency:



The latest version of spring-boot-starter-thymeleaf can be downloaded from Maven Central.

spring-boot-starter-thymeleaf 的最新版本可以从Maven中心下载。

3. LocaleResolver


In order for our application to be able to determine which locale is currently being used, we need to add a LocaleResolver bean:


public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    return slr;

The LocaleResolver interface has implementations that determine the current locale based on the session, cookies, the Accept-Language header, or a fixed value.


In our example, we have used the session based resolver SessionLocaleResolver and set a default locale with value US.


4. LocaleChangeInterceptor


Next, we need to add an interceptor bean that will switch to a new locale based on the value of the lang parameter appended to a request:


public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
    return lci;

In order to take effect, this bean needs to be added to the application’s interceptor registry.


To achieve this, our @Configuration class has to implement the WebMvcConfigurer interface and override the addInterceptors() method:


public void addInterceptors(InterceptorRegistry registry) {

5. Defining the Message Sources


By default, a Spring Boot application will look for message files containing internationalization keys and values in the src/main/resources folder.

默认情况下,Spring Boot应用程序将在src/main/resources文件夹中寻找包含国际化键和值的消息文件。

The file for the default locale will have the name, and files for each locale will be named, where XX is the locale code.


The keys for the values that will be localized have to be the same in every file, with values appropriate to the language they correspond to.


If a key does not exist in a certain requested locale, then the application will fall back to the default locale value.


Let’s define a default message file for the English language called


greeting=Hello! Welcome to our website!
lang.change=Change the language

Next, let’s create a file called for the French language with the same keys:


greeting=Bonjour! Bienvenue sur notre site!
lang.change=Changez la langue

6. Controller and HTML Page


Let’s create a controller mapping that will return a simple HTML page called international.html that we want to see in two different languages:

让我们创建一个控制器映射,它将返回一个简单的HTML页面,名为 international.html,我们希望看到两种不同语言的页面。

public class PageController {

    public String getInternationalPage() {
        return "international";

Since we are using thymeleaf to display the HTML page, the locale-specific values will be accessed using the keys with the syntax #{key}:


<h1 th:text="#{greeting}"></h1>

If using JSP files, the syntax is:


<h1><spring:message code="greeting" text="default"/></h1>

If we want to access the page with the two different locales we have to add the parameter lang to the URL in the form: /international?lang=fr

如果我们想用两种不同的语言访问页面,我们必须在URL中添加参数lang,其形式为: /international?lang=fr

If no lang parameter is present on the URL, the application will use the default locale, in our case US locale.


Let’s add a drop-down to our HTML page with the two locales whose names are also localized in our properties files:


<span th:text="#{lang.change}"></span>:
<select id="locales">
    <option value=""></option>
    <option value="en" th:text="#{lang.eng}"></option>
    <option value="fr" th:text="#{}"></option>

Then we can add a jQuery script that will call the /international URL with the respective lang parameter depending on which drop-down option is selected:

然后我们可以添加一个jQuery脚本,该脚本将根据所选的下拉选项调用 /international URL和相应的lang参数。

<script src="">
<script type="text/javascript">
$(document).ready(function() {
    $("#locales").change(function () {
        var selectedOption = $('#locales').val();
        if (selectedOption != ''){
            window.location.replace('international?lang=' + selectedOption);

7. Running the Application


In order to initialize our application, we have to add the main class annotated with @SpringBootApplication:


public class InternationalizationApp {
    public static void main(String[] args) {, args);

Depending on the selected locale, we will view the page in either English or French when running the application.


Let’s see the English version:


screen shot in English
And now let’s see the French version:
screen shot in French

8. Conclusion


In this tutorial, we have shown how we can use the support for internationalization in a Spring Boot application.

在本教程中,我们已经展示了如何在Spring Boot应用程序中使用对国际化的支持。

The full source code for the example can be found over on GitHub.