Expand my Community achievements bar.

Accessing i18n strings through javascript

Avatar

Level 3

Hi,

As per below link, I'm trying to access i18n string using javascript. I have included the clientlibs granite.util in my page.

https://docs.adobe.com/docs/en/aem/6-1/develop/components/i18n/i18n-dev.html

I have key/string as imported by {0} on {1}. I'm trying to access the i18n strings using below line of code

Granite.I18n.setLocale("de");
Granite.I18n.get("imported by {0} on {1}", [userName, date], "user name, date");

 

All I get was username and date substituted in the key as imported by Peter on 26th Aug. It doesn't give localized string. Whereas if I use below code

 

Granite.I18n.get("Warning"); It gives localized string "Warnung:.

 

Kindly let me know if I miss something.

 

Thanks,

Ruben

5 Replies

Avatar

Level 10

I assume you mean you are using JSP code right - not JavaScript. Can you post all of your code that you are using. 

Avatar

Level 3

No, I'm using Javascript not JSP. Please find the below sample code

Granite.I18n.setLocale("de");

var userName = "Peter";

var date = "26th Aug";

var text = Granite.I18n.get("imported by {0} on {1}", [userName, date], "user name, date");

console.log(text);

Avatar

Level 7

Have you put the translation of your keywords in dictionary? 

If yes, are you able to see the dictionary request in your browser? (Something like 'dict.en.json')

Avatar

Level 3

Yes I have translation in the dictionary and I can see request in the browser 'dict.de.json'.

And response contains "imported by {0} on {1} ":" importiert von {0} am {1}"

But in javascript, it doesn't give translated value. Am I missing something. Above sample code is what I use

Avatar

Level 4

You could use the CQ object like below:

CQ.I18n.getMessage('Key-name');