Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

Accessing i18n strings through javascript

rubenf42159101
Level 3
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
smacdonald2008
Level 10
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. 

rubenf42159101
Level 3
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);

antoniom5495929
Level 7
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')

rubenf42159101
Level 3
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

Bharath_valse
Level 4
Level 4

You could use the CQ object like below:

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