Wednesday, 18 December 2019

Spring boot Internationalization Thymeleaf | Internationalization in Spring Boot

🚀 Level Up Your Coding Skills!

Join the Ram N Java community for the best Spring Boot and Java tutorials.

SUBSCRIBE FOR FREE

Going Global with Spring Boot i18n

Ever wondered how websites like Google or Facebook change their language based on where you are? This is called Internationalization, or i18n. In this guide, we’ll show you how to use Spring Boot and Thymeleaf to make your application speak any language!

What is Internationalization (i18n)?

i18n is the process of designing your app so it can support multiple languages without changing the actual code. Instead of writing "Welcome" directly in your HTML, you use a Key that points to a specific translation file.

Step 1: The Message Bundle

Think of a Message Bundle as a dictionary for your app. You create different .properties files for different languages:

  • messages.properties: Your default language (usually English).
  • messages_hi.properties: For Hindi translations.
  • messages_fr.properties: For French translations.

Step 2: How Spring Boot Knows the Language

Spring Boot uses two smart tools to handle this:

  1. LocaleResolver: This decides which language to show. It can check the user's browser settings or a cookie.
  2. LocaleChangeInterceptor: This looks at the URL. If a user clicks a link like ?lang=hi, this tool tells the app to switch to Hindi instantly!

Step 3: Magic in Thymeleaf

In your HTML, instead of hardcoding text, you use the special #{...} syntax.

<h1 th:text="#{welcome.title}">Welcome!</h1>

Thymeleaf will automatically look into your property files and pick the right translation for the user!

More Awesome Tutorials

If you found this helpful, check out these related videos from my channel:

No comments:

Post a Comment

Tutorials