Multilingual Thymeleaf: A Guide to Internationalization
Building applications with internationalization support is crucial for reaching a wider audience. Thymeleaf, a popular templating engine for Java, provides an elegant way to create dynamic and multilingual web pages. This article will guide you through the process of implementing multi-language support in your Thymeleaf application.
The Challenge: Displaying Content in Multiple Languages
Imagine you're building an e-commerce website. You want to cater to users around the globe, each with their preferred language. How can you dynamically switch between English, Spanish, French, or any other language without rewriting your entire HTML template for each language? This is where Thymeleaf's powerful internationalization features shine.
Scenario and Code Snippet:
Let's assume your website has a simple message "Welcome to our website" that needs to be localized. Here's how you might start with your Thymeleaf template:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Multilingual Website</title>
</head>
<body>
<p th:text="#{welcome.message}">Welcome to our website</p>
</body>
</html>
Breaking Down the Code:
th:text="#{welcome.message}"
: This line utilizes Thymeleaf's internationalization feature. It retrieves the text from a resource bundle based on the key "welcome.message".
Adding Internationalization Support:
-
Resource Bundles:
- Create separate
.properties
files for each language. For example:messages_en.properties
(for English)messages_es.properties
(for Spanish)messages_fr.properties
(for French)
- Create separate
-
Populate Resource Bundles:
- Add the translations to each language file:
- messages_en.properties:
welcome.message=Welcome to our website
- messages_es.properties:
welcome.message=Bienvenido a nuestro sitio web
- messages_fr.properties:
welcome.message=Bienvenue sur notre site web
- messages_en.properties:
- Add the translations to each language file:
-
Configure Thymeleaf:
-
Use the
spring.messages.basename
property in yourapplication.properties
file to define the base name of your resource bundles:spring.messages.basename=messages
-
-
Setting the Locale:
-
In your Spring controller, set the locale based on user preference or browser language settings:
@GetMapping("/") public String index(Locale locale, Model model) { model.addAttribute("locale", locale); // Pass the locale to the template return "index"; }
-
-
Displaying Localized Content in the Thymeleaf Template:
-
Use the
th:text="#{key}"
syntax with the locale set in the controller:<p th:text="#{welcome.message}">Welcome to our website</p>
-
Illustrative Example:
Let's say you have a user who accesses your website from Spain. The browser sends a language preference (es-ES). Your Spring application detects the language, sets the locale to Spanish, and Thymeleaf will load the messages_es.properties
file. Consequently, the webpage will display "Bienvenido a nuestro sitio web".
Additional Tips:
- Contextualization: Thymeleaf supports placeholders in resource bundles. You can use them to inject dynamic values, like usernames or dates, for more personalized content.
- Date and Number Formatting: You can leverage Thymeleaf's built-in date and number formatting functions to display localized dates, times, and numbers correctly.
- Advanced Internationalization: Thymeleaf offers support for pluralization, currency formatting, and more. Explore the official documentation for advanced usage.
Conclusion:
By leveraging Thymeleaf's powerful internationalization features, you can seamlessly create a website that caters to users from diverse language backgrounds. This approach ensures a localized user experience, enhancing your application's accessibility and global appeal. Remember to test your website with different language settings to ensure a smooth user journey.