Internationalization is the process of making your Spring application ready for multilingual support. Once internationalized you can localize spring applications by translating it into other languages.

Getting started with Spring i18n: Preparing your view templates

The first step towards a localized application is to extract all static text from your templates into a localization file. Later, this is used to look up the required text bits according to the selected language.

Let’s have a look at our homepage template src/main/resources/templates/home.html

1

2

3

4

5

6

7

8

9

10

<!DOCTYPE HTML>

<html xmlns:th="http://www.thymeleaf.org">

<head>

<meta http-equiv="Content-Type"content="text/html; charset=UTF-8" />

</head>

<body>

<h1>Welcome to my site!</h1>

<p>This is my English intro.</p>

</body>

</html>

After replacing all static text with placeholders, it should look something like this:

Spring MessageSource resource bundles are the default file format used for localizing most Java apps. Spring also comes with great support for storing and retrieving translations from those files. The messages.properties file now holds your original copy. Many teams work with English as source language, but any other language works as well.

Adding a new language

Let’s now add a second language to our project. In this example we will add German as the second language. We create a copy of the initial messages.properties . Next, we append the locale name (i.e. messages_de.properties) and translate the content within the file:

1

2

headline = Willkommen auf meiner Seite!

intro_text = Dies ist meine deutsche Einleitung.

After restarting your application, you should still see the original English content. However, when you append the lang parameter to the URL, you will see the German translations. Congratulations, you’ve just completed your first localization!

Localize Spring Applications – Next level

This was all pretty straightforward. With time your application might grow and so will your resource bundle. Maybe it will become hard to maintain. Also, your translators might not be able to work with the .properties format. At this point a software like PhraseApp can help you organize your translations. PhraseApp can be integrated in a few minutes and makes translation content accessible through a web interface for translators.

After following the init wizard, you should be able to upload your existing translations to PhraseApp with a simple command:

1

$ phraseapp push

All your translations are now available for editing within PhraseApp’s Translation Center. Once the translators have finished their work, you can download the finished translations back into your project using the command:

1

$ phraseapp pull

With minimal effort you just provided your teammates or external translators with an easy way to work on texts used on your website.

Editing translations in-context: WYSIWYG

This is already a quite professional translation workflow. But we can go even one step further and add an in-page editor for the translations used in our Spring application.

After restarting your application you will now see the PhraseApp In-Context Editor. Sign in using your PhraseApp credentials to start editing your translations directly on your web application, like a pro.

To see how the In-Context editor looks like, you can also head over to our demo.

You can find all source code from this example on GitHub. Thanks to Szilard Bozoki (beerides.com) for suggesting this integration and providing example Spring code on it!