Localization in Hybris

We know that, in any web application its common to display messages in UI


Message could be a text label or info text or error messages etc

Generally, we keep all these messages in a properties file specific to locale

Example :

English specific messages will be kept in base_en.properties
German specific messages will be kept in base_de.properties
French specific messages will be kept in base_fr.properties, and so on

Each of these files are specific to specific locale or language.

In our application, we need to load the messages from these properties file

Messages will be kept in these files in the form of key value pairs

We can use key to load the value dynamically in our application wherever we want to load it.

Example :


base_en.properties

  1. address.country.invalid = Please select a country
  2. address.phone.invalid = Incorrect phone number
  3. product.code.invalid=Please provide valid product code
address.country.invalid = Please select a country
address.phone.invalid = Incorrect phone number
product.code.invalid=Please provide valid product code


Now we can use these messages in our application

How to load these locale specific properties ?

Mainly there are 2 places where we load these properties
1) JSP/tag files
2) Java class

Let’s see how to load these messages in both the places

1) In JSP or tag file, we can load these messages using spring provided tag as below


We just need to specify key in the below tags

  1. <spring:theme code="address.country.invalid "/>
  2.  
  3. <spring:theme code="address.phone.invalid "/>
  4.  
  5. <spring:theme code="product.code.invalid "/>
<spring:theme code="address.country.invalid "/>

<spring:theme code="address.phone.invalid "/>

<spring:theme code="product.code.invalid "/>


This will automatically pick up the value corresponding to the key from the current locale.

2) Sometime, We need to load these messages in Java class conditionally based on some business logic and add these messages in the model attribute to send it to front end


In such case, we can use MessageSource and I18NService in our class

In Hybris, these 2 dependencies are already defined in the AbstractPageController

So, any controller extending this AbstractPageController can directly use it

Its injected as below

  1. public abstract class AbstractPageController extends AbstractController
  2. {
  3. @Resource(name = "messageSource")
  4. private MessageSource messageSource;
  5.  
  6. @Resource(name = "i18nService")
  7. private I18NService i18nService;
  8.  
  9. protected MessageSource getMessageSource()
  10.     {
  11.         return messageSource;
  12.     }
  13.  
  14. protected I18NService getI18nService()
  15.     {
  16.         return i18nService;
  17.     }
  18.  
  19. //further code
  20.  
  21. }
public abstract class AbstractPageController extends AbstractController
{
@Resource(name = "messageSource")
private MessageSource messageSource;

@Resource(name = "i18nService")
private I18NService i18nService;

protected MessageSource getMessageSource()
	{
		return messageSource;
	}

protected I18NService getI18nService()
	{
		return i18nService;
	}

//further code

}


We can use these dependencies and access messages as below

  1. String productInvalidMessage = getMessageSource().getMessage("product.code.invalid", null, getI18nService().getCurrentLocale());
  2. model.addAttribute(“productInvalidMessage”, productInvalidMessage);
String productInvalidMessage = getMessageSource().getMessage("product.code.invalid", null, getI18nService().getCurrentLocale());
model.addAttribute(“productInvalidMessage”, productInvalidMessage);


We can add this in the model attribute and can access in the jsp or tag file as below

  1. ${productInvalidMessage}
${productInvalidMessage}

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on