I am facing an issue with i18n labels.
My application reads few i18n labels at js frontend using Granite.I18n.get(\'\') function. The entire dictionary gets downloa
We faced the similar requirement to fetch I18n values at client side using Granite.i18n Library This is what I did.
4.In clientlibs javascript file setting Granite.I18n.setUrlPrefix("/bin/custom/i18n/dict."); to fetch from
custom servlet URL. This doesn't requires modification of OOTB I18n.js
The English dictionary is small because English entries are the keys and not the translation. French (and other languages) are big because they contain the key in English and further translation. Also, a lot of keys are only available in translated languages only because the key is used as default translation.
So for French, if you use Granite.I18n.get('Hello world!')
, it will return the French translation if it finds it otherwise it will simply return 'Hello World', which doesn't require a translation if the language context is English.
Due to the nature of JS being evaluated on client side, the product is designed to download the full dictionary including the OOTB translation of the product itself as the i18n implementation is not context aware and cannot filter out unwanted translation.
While convenient, this is a limitation and side effect of using Granite.I18n.get('')
unfortunately.
Possible Workarounds
In general, the pages must only render what is required. Using JS to do late translation will force a dictionary download and Granite.i18n does not cater for optimised downloading experience.
I ended up writing a custom implementation as I didnt get much help from Adobe tickets as well on this issue.
This solved my problem. Though am not convinced as proper solution. There needs to an otb way of rendering this clean.
here is an answer that could help you when you are trying to get a specific dictionary: https://stackoverflow.com/a/55656153/14465431
look at the part where is mention the sling:basename
this also works for one site
example:
[sling:Language] > mix:language
mixin
- sling:basename (string)
- sling:basename (string) multiple
http://localhost:4502/libs/cq/i18n/dict.es.[sling:basename-value].json
OTB solution :)