Translation Editor
Save time on handling localization files and translation strings.
Try SimpleLocalize
Share article

Spring Boot 3.2: Internationalization

Spring Boot 3.2: Internationalization

Table of contents


Internationalization (i18n) is the process of designing your application to be adapted to various languages, regional peculiarities, and technical requirements of a target market. Internationalization is a crucial step in the process of localizing your product. In this tutorial, we will show you how to use Spring Boot to create a simple internationalized application, how to get translated messages from files, render translated HTML from HTML template using Thymeleaf and how to use LocaleResolver to change the language of the application.

Used technologies

  • Java 21
  • Spring Boot 3.2
  • Thymeleaf 3
  • Maven
  • SimpleLocalize Editor

translated spring boot page


1. Create files

The messages are stored in the files. The XX is the language code. For example, is the Polish version of the messages.

footerText=© 2023 SimpleLocalize. Wszelkie prawa zastrzeżone.
linkText=Utwórz konto SimpleLocalize
message=Dziękujemy za wypróbowanie naszego demo SimpleLocalize dla Spring Boot!
title=Hej {0}!

If you are using SimpleLocalize, you can download the files providing your apiKey in simplelocalize.yml file and invoking simplelocalize download command.

Downloading files via SimpleLocalize CLI

2. Configure location

The default location for messages is src/main/resources/messages. You can change this by setting the spring.messages.basename property in your file or by providing your ResourceBundleMessageSource bean.

public ResourceBundleMessageSource messageSource() {
    var resourceBundleMessageSource=new ResourceBundleMessageSource();
    resourceBundleMessageSource.setBasenames("i18n/messages"); // directory with
    return resourceBundleMessageSource;
} in IDE

3. Configure resolving locale from requests

The default locale resolver is AcceptHeaderLocaleResolver which resolves the locale from the Accept-Language header. You can change this by setting the spring.mvc.locale-resolver property in your file or by providing your LocaleResolver bean, creating LocaleChangeInterceptor and registering it via addInterceptors method (see WebMvcConfigurer class).

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

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

public void addInterceptors(InterceptorRegistry registry) {

4. Create HTML template with Thymeleaf

If you want to get an HTML document with translated messages, for example, to send it via email, you can use Thymeleaf. If you are not familiar with Thymeleaf, you can read more about it here. In short, Thymeleaf is a modern server-side Java template engine for both web and standalone environments.

<!doctype html>
<html xmlns="" th:attr="lang=${lang}">

  <title>Spring Boot Email Example</title>

  <h1 th:utext="#{title(${userName})}"></h1>

  <p th:text="#{message}"></p>
  <a th:href="${url}" th:text="#{linkText}"></a>
  <p th:text="#{footerText}"></p>


Quick Thymeleaf guide:

  • th:attr="lang=${lang}" - sets the language of the document
  • th:utext="#{title(${userName})}" - gets a message with title key and inserts the value of the userName variable
  • th:text="#{message}" - gets a message with message key
  • th:href="${url}" - inserts a value of the url variable
  • You can use th:utext instead of th:text to avoid escaping HTML characters.
  • You can use th:attr to set the lang attribute on the html tag.

You can use to create responsive HTML emails for free.

Localized texts

Use MessageSource to get translated messages. This is the default way to get translated messages in Spring Boot.

import org.springframework.context.MessageSource;

private MessageSource messageSource;

void shouldGetTranslatedTextFromLocalFileAndLocale() {
    Locale locale=Locale.of("pl","PL");

    String titleTextWithArgument=messageSource.getMessage("title",new Object[]{"Foo Bar"},locale);

    assert titleTextWithArgument.equals("Hej Foo Bar!");

Localized API exceptions

You can also return translated API exceptions by using standard Spring Boot @ControllerAdvice. For this purposes, I created ErrorController with two methods annotated with @ExceptionHandler to catch exceptions.

  • @ExceptionHandler(Exception.class) - catches all exceptions not caught by other methods
  • @ExceptionHandler(IllegalArgumentException.class) - catches IllegalArgumentException exceptions

Both methods gets Locale from LocaleContextHolder and returns translated message from file using MessageSource bean.

public class ErrorController
  private MessageSource messageSource;

  @ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
  public ResponseEntity<ApiError> exception()
    String message = getLocalizedMessage("exception.internalServerError");
    HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
    return ResponseEntity
            .body(new ApiError(message, status));

  @ResponseStatus(code = HttpStatus.BAD_REQUEST)
  public ResponseEntity<ApiError> badRequest()
    String message = getLocalizedMessage("exception.badRequest");
    HttpStatus status = HttpStatus.BAD_REQUEST;
    return ResponseEntity
            .body(new ApiError(message, status));

  private String getLocalizedMessage(String translationKey)
    Locale locale = LocaleContextHolder.getLocale();
    return messageSource.getMessage(translationKey, null, locale);

By default, Spring Boot uses Accept-Language header to resolve user locale, but in this tutorial we changed this behavior by registering custom LocaleChangeInterceptor bean . To change the language of exception, you need to use lang parameter, e.g. /api/my-health?lang=pl_PL.

Translated Spring Boot exceptions

The number of ways to return translated messages is endless, but the solution above might be one of the best ones as we keep all the logic and translation keys for exceptions in one place.

Localized HTML content (Thymeleaf)

Use ThymeleafEngine bean to render HTML with translated messages. This is probably the most popular way to render HTML with translated messages in Spring Boot.

private TemplateEngine templateEngine;

public String renderHtmlFromTemplate(Locale locale,String userName)
    Context context=new Context();
    return templateEngine.process("my-html-template",context);

render custom HTML with translated texts

Localized web pages (Thymeleaf)

You can also return translated web pages (HTML) by using standard Spring Boot @Controller. Spring Boot will automatically resolve user locale and render HTML from my-html-template.html template with translated messages.

This is the default way to return translated web pages in Spring Boot.

public class WelcomeController
  public String renderHtmlFromTemplate(Model model)
    model.addAttribute("userName", "Jakub");
    return "my-html-template";

Run the application and open http://localhost:8080/welcome in your browser. You can change the language by adding ?lang=pl_PL to the URL.

Changing the 'lang' query parameter in Spring Boot

Relevant posts

Check out our latest blog posts and stay up to date with SimpleLocalize

Java 21: Internationalization

Jakub Pomykałaby8 min readMarch 12, 2024

Learn about Java 21 internationalization and localization. We will learn how to use formatting instances to format dates and numbers. We will take a look at the `java.text` package and its classes. We will also learn how to use the `java.time` package to work with dates and times. Finally, we will learn how to use the `ResourceBundle` object to retrieve localized messages from a properties file.

Continue reading

SimpleLocalize: Tips & Tricks

Kinga Wojciechowskaby5 min readMarch 16, 2023

Learn our tips for using SimpleLocalize. Take advantage of the i18n editor's capabilities, automate the translation process and localize your software.

Continue reading

Localization: Tips & Tricks

Jakub Pomykałaby3 min readNovember 27, 2021

Avoid common problems with localization by following these simple tips. These resources will help you save time and effort with internationalization and localization.

Continue reading