Localization in Hybris
- 24th Jun 2018
- 0
- 37510
- how to achieve Localization in Hybris how to do localization in hybris How to load locale specific messages from properties file ? How to load locale specific properties ? How to read locale properties file in hybris how to read localization message in hybris using message source and i18n service 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
- address.country.invalid = Please select a country
- address.phone.invalid = Incorrect phone number
- 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
- <spring:theme code="address.country.invalid "/>
- <spring:theme code="address.phone.invalid "/>
- <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
- 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
- }
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
- String productInvalidMessage = getMessageSource().getMessage("product.code.invalid", null, getI18nService().getCurrentLocale());
- 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
- ${productInvalidMessage}
${productInvalidMessage}