The AEM internationalization framework(i18n) uses dictionaries in the repository to store English strings and their translations in other languages. The framework uses English as the default language. Localized strings can be stored in several dictionaries in the repository, e.g., component level. The AEM internationalization framework combines the dictionaries and makes them available in Sling as a single ResourceBundle object. The i18n can be used in slightly, JavaScript, and Java to display the language-specific labels and values.
In some cases, we may have the use case to share the i18n dictionaries externally.
There are a couple of options to share the dictionaries externally.
ResourceBundleExportServlet:
By default the dictionaries are exposed using the ResourceBundleExportServlet (com.adobe.granite.i18n.impl.ResourceBundleExportServlet) registered under path /libs/cq/i18n/dict.
e.g
http://localhost:4502/libs/cq/i18n/dict.de_de.json
http://localhost:4502/libs/cq/i18n/dict.de.json
http://localhost:4502/libs/cq/i18n/dict.fr_fr.json
The ResourceBundleExportServlet collects all the dictionaries defined in the AEM system for a particular language and(or) country and exposes them as JSON.
By default, the /libs path is blocked from Dispatcher; the dictionary path should be allowed to access externally. The challenge with this approach is all the available dictionaries are exported as part of the JSON response but will not be able to export the selected dictionaries.
Client library JSON:
Another option is to share the i18n JSON as a client library resource e.g
http://localhost:4502/etc.clientlibs/project/clientlibs/clientlib-site/resources/i18n/de.json
Define the i18n dictionary as a json under the /apps folder(ui.apps) and copy the i18n from /apps(ui.apps) to corresponding clientlibs resource folder through Front End build script.
Custom servlet with sling:basename:
Another approach is defining a custom servlet combined with sling:basename for the dictionary to share the specific dictionary values externally.
As a first step, enable the sling:basename for the all language nodes( mix:language node), e.g., sling:basename=search, and the sling:basename will be used to select the specific dictionary. You can define i18n dictionary-specific sling:basename’s(e.g., search,search1, etc.), so those particular dictionaries can be selected by specifying the sling:basename. Ensure the jcr:primaryType of the language node is sling:Folder, the nt:Folder won't support adding sling:basename property.
Now enable a custom servlet(refer below), the path is defined based on the sling:basename enabled e.g./bin/fetchi18nvalues/search, you can specify multiple paths to support different sling:basename.
The language can be added as a selector to the URL; the language selector is validated against the predefined list of languages.
e.g
http://localhost:4502/bin/fetchi18nvalues/search.it.json
http://localhost:4502/bin/fetchi18nvalues/search.bg.json
http://localhost:4502/bin/fetchi18nvalues/search1.it.json
http://localhost:4502/bin/fetchi18nvalues/search1.bg.json
The servlet selects the i18n dictionary based on the sling:basename on the request path and the language selector and shares the data as JSON.
The custom servlet shares only the specific dictionary/languages tagged with the same sling:basename; the servlet can be customized to add additional functionalities.
No comments:
Post a Comment