April 1, 2020
Estimated Post Reading Time ~

How to use multi language translation in JSP in CQ / AEM

Use Case: You want to display certain text in a page based on the page language

Prerequisite: http://sling.apache.org/site/internationalization-support-i18n.html

Solution: You can use the i18n bundle approach for this
Suppose you want to display certain text for a component in different languages based on-page language or URL. Take an example of /apps/geometrixx/components/homepage component.

1) Create a i18n node under /apps/geometrixx/components/homepage of type sling:Folder
2) create a node of type sling:Folder under i18n of mixin type mix:language
3) Add a property called jcr:language and assign the value of lang code or lang_COUNTRY

The structure would be like

/apps/myApp
+-- English (nt:folder, mix:language)
| +-- jcr:language = en
| +-- mx (sling:messageEntry)
| +-- sling:key = "msgXXX"
| +-- slign:message = "An Application Text"
+-- Deutsch (nt:folder, mix:language)
+-- jcr:language = de
+-- mx (sling:messageEntry)
+-- sling:key = "msgXXX"
+-- slign:message = "Ein Anwendungstext"



4) Then on jsp page use following code

<cq:setContentBundle/>
<%

Locale pageLocale = currentPage.getLanguage(false);
//If above bool is set to true. CQ looks in to page path rather than jcr:language property.
ResourceBundle resourceBundle = slingRequest.getResourceBundle(pageLocale);
I18n i18n = new I18n(resourceBundle);
%>
I am printing using I18n <%=i18n.get("updateLocations") %>
I am printing using fmt:message now <fmt:message key="updateLocations"/>
I found page locale is <%=pageLocale.toString() %>

You can also test language using grid under http://localhost:4502/libs/cq/i18n/translator.html
More Info: http://labs.sixdimensions.com/blog/dklco/2012-05-25/adobe-cq5-translator

To change what languages are shown in the translator's grid, create a multi-value string property /etc/languages/languages (node structure must be created), containing the iso language codes (e.g. ["de", "fr", …]). The default is hardcoded in the translator and maps the OOTB CQ languages: ['de', 'fr', 'it', 'es', 'ja', 'zh-cn']

Some More Good blog Post on this:
http://blogs.adobe.com/dekesmith/2012/10/21/internationalization-within-sling-and-cq


By aem4beginner

No comments:

Post a Comment

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