Open In App

Spring MVC – Internationalization (i18n) and Localization (L10n)

Spring MVC is a Model-View-Controller (MVC) framework that is used to build web applications in Java. The Spring MVC pattern has three parts:

Prerequisites of Topic

Note: Here I’m Using STS as a IDE.



Spring MVC Internationalization (i18n)

Internationalization is the process of developing software so that it can be easily adapted to different languages and regions without any code changes.

Core Components of Spring MVC i18n:

Note: The MessageSource interface in Spring allows for internationalization support.



Localization (L10n) with Spring MVC

Spring MVC application must be localized to work in many languages and regions. This allows building applications that can be seamlessly localized for global users without code changes.

Core Components of Spring MVC L10n:

Example of Internationalization and Localization

Step 1: Spring MVC Project setup

Project Structure:

Step 2: Create Message Controller: MessageController.Java




package com.demo.controller;
  
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
  
import com.demo.model.Message;
  
@Controller
public class MessageController {
  
     @GetMapping("/message")
    public String message(Model model) {
        Message message = new Message("data.message");
        model.addAttribute("message", message);
        return "message";
    }
}

Step 3: Create Message Model: Message.java




package com.demo.model;
public class Message {
  
    private String key;
  
    public Message(String key)
    
      this.key = key; 
    }
  
    public String getKey() 
    
      return key; 
    }
}

Step 4: Create Views: message.html




<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>GeeksforGeeks</title>
</head>
<body>
    <h1 th:text="#{${message.key}}" style="color: green;"></h1>
</body>
</html>

Step 5: Configure Internationalization: WebConfig.java




package com.demo.config;
  
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
  
@Configuration
public class WebConfig implements WebMvcConfigurer {
  
    @Bean public LocaleResolver localeResolver()
    {
        return new CookieLocaleResolver();
    }
  
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor()
    {
        LocaleChangeInterceptor localeChangeInterceptor
            = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("lang");
        return localeChangeInterceptor;
    }
  
    @Bean public ResourceBundleMessageSource messageSource()
    {
        ResourceBundleMessageSource messageSource
            = new ResourceBundleMessageSource();
        messageSource.setBasename("messages/messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }
  
    @Override
    public void
    addInterceptors(InterceptorRegistry registry)
    {
        registry.addInterceptor(localeChangeInterceptor());
    }
}

Step 6: Messages Properties

  1. messages.properties:
    data.message=Hello and goodbye!



  2. messages_fr.properties:
    data.message=Bonjour et au revoir!



Step 7: Run the Application

mvn spring-boot:run



Output of an Application:

Output: http://localhost:8080/message

Output : http://localhost:8080/message?lang=en

Output: http://localhost:8080/message?lang=fr


Article Tags :