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

Spring Boot 3.0: Internationalization

Spring Boot 3.0: 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 19
  • Spring Boot 3.0 (RC)
  • 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

Getting started: Invite team members and translators

Getting started: Invite team members and translators

Kinga WojciechowskabyKinga Wojciechowska6 min read

Learn how to work together using SimpleLocalize! Invite team members, share access, and make localization easy.

Continue reading
How to share translation keys across multiple apps?

How to share translation keys across multiple apps?

Kinga WojciechowskabyKinga Wojciechowska4 min read

Easily manage translations across multiple apps with SimpleLocalize's key merging feature. Save time and maintain consistency!

Continue reading
How to auto-translate JSON files

How to auto-translate JSON files

Kinga WojciechowskabyKinga Wojciechowska5 min read

Simplify your localization process by learning how to auto-translate JSON files with SimpleLocalize's translation editor and auto-translation feature.

Continue reading