Accessing i18n strings through javascript | Community
Skip to main content
rubenf42159101
Level 2
August 26, 2016

Accessing i18n strings through javascript

  • August 26, 2016
  • 3 replies
  • 7662 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

smacdonald2008
Level 10
August 26, 2016

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 2
August 26, 2016

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
August 27, 2016

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 2
August 27, 2016

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
January 12, 2018

You could use the CQ object like below:

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