Internationalization (i18n) in Spring Boot: A Complete Guide

In today’s globalized web applications, it’s crucial to support multiple languages to reach a wider audience. Spring Boot makes it easy to implement internationalization (i18n), allowing you to externalize messages and tailor them based on user locale.

In this guide, you’ll learn how to implement internationalization in Spring Boot with full working examples using the package com.kscodes.springboot.

Internationalization (i18n) in Spring Boot: A Complete Guide

🌍 What is Internationalization (i18n)?

Internationalization (abbreviated as i18n) is the process of designing an application to support various languages and regional formats without changing the codebase. This is often implemented using:

  • Message resource bundles (messages_en.properties, messages_fr.properties, etc.)
  • Locale resolvers to determine user locale
  • Spring MVC to display locale-based content

πŸ›  Step-by-Step Guide to Internationalization (i18n) in Spring Boot

🧱 Step 1: Create Message Resource Files

Create src/main/resources/messages_*.properties files for each language you want to support.

messages_en.properties (Default – English)

messages_fr.properties (French)

πŸ”§ Step 2: Configure MessageSource Bean

🌐 Step 3: Configure LocaleResolver

Use AcceptHeaderLocaleResolver to pick locale from request header (e.g., Accept-Language).

πŸ“ž Step 4: Create a REST Controller to Use i18n

πŸ“¦ Step 5: Make a Test Call

Using curl or Postman:

Response

Without header:

🌐 Optional: Using Query Params for Locale (SessionLocaleResolver)

If you prefer users to change language via query param (e.g., ?lang=fr), use:

Also register the interceptor:

Now you can call:

πŸ” Security and Performance Tips

  • Use UTF-8 encoding for all .properties files.
  • Avoid loading too many language files unnecessarily.
  • Use caching for messages when using ReloadableResourceBundleMessageSource.

πŸ’‘ Best Practices for Internationalization in Spring Boot

  • Use consistent keys across languages (welcome.message, error.notfound).
  • Keep messages short and readable.
  • Validate translated .properties files with native speakers.
  • Handle fallback when the language is missing.

βœ… Summary

  • Use @MessageSource to load language-specific property files.
  • Use LocaleResolver to detect user locale (via headers or query).
  • Implement Accept-Language or ?lang= based language switching.
  • Property files should be named messages_{lang}.properties.