May 9, 2020
Estimated Post Reading Time ~

How to use i18N in AEM

These are the steps to implement Internationalization with i18n in Adobe CQ5.

Step-1: Need to create the base folder(sling:Folder) called "i18n" in your project("/apps/<project>").
(You can create in global level called "/apps")
jcr:primaryType = sling:Folder

Step-2: Need to create the language folders(sling:Folder) called "en" in "/apps/<project>/i18n/" .
"fr" in "/apps/<project>/i18n/" .
"de" in "/apps/<project>/i18n/" .
jcr:primaryType = sling:Folder


Assign mixin to the language folders created in the previous step (e.g. en, ar etc) from crx (http://localhost:4502/crx/explorer)console mix:language

For the language nodes (e.g. en, ar etc) add String property jcr: language, value = ISO language code (en,fr, de etc)


Step-3: Create nodes of type sling:MessageEntry for each field label, and add below properties.
sling:key = <keyname> and sling:message = <message>


Setting properties for English(en) node


Setting properties for France(fr) node

Now we can use this "key" to get the "value" of the appropriate locale string .

Use this below code in your jsp file.

<%@page session="false"%>
<%@page import="java.util.Locale,java.util.ResourceBundle,com.day.cq.i18n.I18n"%>
<%@include file="/libs/foundation/global.jsp"%>
<cq:setContentBundle/>
<%
      final Locale pageLocale = currentPage.getLanguage(false);
      final ResourceBundle resourceBundle = slingRequest.getResourceBundle(pageLocale);
      I18n i18n = new I18n(resourceBundle);
%>
<div class="item_label">
      <%= i18n.get("itemname") %>
</div>

Now you can manage this through i18n console as well.

Go to "http://localhost:4502/libs/cq/i18n/translator.html"
In filters choose your projects i18n directory path("/apps/<project>/i18n").
You can get the appropriate strings for different locales which we mentioned above.


Accessing properties in translator console ( http://localhost:4502/libs/cq/i18n/translator.html )

After setting up of required langauge nodes in i18n folder, you can modify these strings from here directly from now as shown above.

Now we can start work on i18N.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.