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

AEM i18n from Javascript (try 2)

Avatar

Avatar
Validate 1
Level 2
daitienshi
Level 2

Likes

2 likes

Total Posts

27 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
daitienshi
Level 2

Likes

2 likes

Total Posts

27 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 1
Affirm 1
View profile
daitienshi
Level 2

01-11-2016

Apologies for the duplicate post. Seems like something in the original is breaking the page, so I'll try this again:

I'm trying to dynamically retrieve a translated message using Javascript from my created dictionary in AEM's translator (/libs/cq/i18n/translator.html).

We've got dictionary values set up like:

String               EN                  FR
========================================================
TEST-001        This is a Test1     FR:This is a Test1
TEST-002        This is a Test2     FR:This is a Test2
TEST-003        This is a Test3     FR:This is a Test3

I've looked through the Internationalizing UI Strings article (https://docs.adobe.com/docs/en/aem/6-2/develop/components/i18n/i18n-dev.html) but when I try to the following:

Granite.I18n.setLocale("en");
Granite.I18n.get("TEST-001");
or

Granite.I18n.setLocale("fr");
Granite.I18n.get("TEST-001");


I only get returned the string I've passed in (i.e. "TEST-001").

Could someone help me understand how to retrieve the translated value back?

Thanks!

Replies

Avatar

Avatar
Boost 1
Employee
leandror2195450
Employee

Like

1 like

Total Posts

1 post

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Employee
leandror2195450
Employee

Like

1 like

Total Posts

1 post

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile
leandror2195450
Employee

01-11-2016

I'm not sure about the relationship between "String" and "EN" here, as the English string is typically the key. What do you get if you do:

Granite.I18n.setLocale("fr");
Granite.I18n.get("This is a Test1");

Avatar

Avatar
Validate 1
Level 2
daitienshi
Level 2

Likes

2 likes

Total Posts

27 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
daitienshi
Level 2

Likes

2 likes

Total Posts

27 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 1
Affirm 1
View profile
daitienshi
Level 2

01-11-2016

Hi Leandro,

The relationship between String and EN is based on the translator library found in /libs/cq/i18n/translator.html.

The String value is normally used as the lookup key in JSP or Sling and the EN is the actual translated value being returned if the locale is EN.

Regardless, if I pass the "This is a Test1" string into the Granite.I18n.get() function, with the locale set to FR, I still get back "This is a Test1"

Avatar

Avatar
Validate 1
Level 2
Anton_Smulskiy
Level 2

Likes

3 likes

Total Posts

35 posts

Correct Reply

4 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
Applaud 5
Affirm 3
View profile

Avatar
Validate 1
Level 2
Anton_Smulskiy
Level 2

Likes

3 likes

Total Posts

35 posts

Correct Reply

4 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
Applaud 5
Affirm 3
View profile
Anton_Smulskiy
Level 2

01-11-2016

Have you checked if your i18n works on JSP/Java? If it works there - I have no answer.

If not - make sure use set your dictionary correctly under /apps/your-app/i18n. 

Also, your page should be under /content/your-app/...

Avatar

Avatar
Validate 1
Level 2
daitienshi
Level 2

Likes

2 likes

Total Posts

27 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
daitienshi
Level 2

Likes

2 likes

Total Posts

27 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 1
Affirm 1
View profile
daitienshi
Level 2

01-11-2016

Hi Anton, Yes, I'm able to successfully use i18n using JSP, but we're looking to dynamically retrieve the string via javascript rather than preloading the string on page render.

Avatar

Avatar
Validate 1
Level 2
Anton_Smulskiy
Level 2

Likes

3 likes

Total Posts

35 posts

Correct Reply

4 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
Applaud 5
Affirm 3
View profile

Avatar
Validate 1
Level 2
Anton_Smulskiy
Level 2

Likes

3 likes

Total Posts

35 posts

Correct Reply

4 solutions
Top badges earned
Validate 1
Boost 3
Boost 1
Applaud 5
Affirm 3
View profile
Anton_Smulskiy
Level 2

01-11-2016

  • Try to remove "-" from i18n key name.
  • Try to use CQ.I18n
  • Debug

Avatar

Avatar
Boost 5
Employee
Joseph_Rignanes
Employee

Likes

14 likes

Total Posts

19 posts

Correct Reply

5 solutions
Top badges earned
Boost 5
Boost 3
Boost 10
Boost 1
Affirm 5
View profile

Avatar
Boost 5
Employee
Joseph_Rignanes
Employee

Likes

14 likes

Total Posts

19 posts

Correct Reply

5 solutions
Top badges earned
Boost 5
Boost 3
Boost 10
Boost 1
Affirm 5
View profile
Joseph_Rignanes
Employee

01-11-2016

Hi,

Do you see any errors from your browser?

The Javascript implementation will need to retrieve the dictionary as JSON via an Ajax request. You should see a request to something like /libs/cq/i18n/dict.fr.json.

Can you check this url returns the expected information?

Avatar

Avatar
Validate 1
Level 2
daitienshi
Level 2

Likes

2 likes

Total Posts

27 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
daitienshi
Level 2

Likes

2 likes

Total Posts

27 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 1
Affirm 1
View profile
daitienshi
Level 2

01-11-2016

Hi Joseph, How would I request the dictionary via AJAX? I can call Granite.I18n.getDictionary() and get an object back but I have no clue what this object is (have tried a bunch of the functions it has defined) but still can't get anything out of it.

Avatar

Avatar
Boost 1
Level 2
Ankur_Sial
Level 2

Like

1 like

Total Posts

18 posts

Correct Reply

5 solutions
Top badges earned
Boost 1
Affirm 5
Affirm 3
Affirm 1
View profile

Avatar
Boost 1
Level 2
Ankur_Sial
Level 2

Like

1 like

Total Posts

18 posts

Correct Reply

5 solutions
Top badges earned
Boost 1
Affirm 5
Affirm 3
Affirm 1
View profile
Ankur_Sial
Level 2

01-11-2016

Hi Daitienshi,

Consolidated i18n dictionaries are cached in the system. Therefore, addition of an entry may not reflect immediately. Does the dictionaries loaded at runtime have the keys TEST-001, TEST-002 and TEST-003 ? 

Also, in case the keys are missing, one could update the cache by restarting the bundle - org.apache.sling.i18n.

Avatar

Avatar
Validate 1
Level 2
daitienshi
Level 2

Likes

2 likes

Total Posts

27 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
daitienshi
Level 2

Likes

2 likes

Total Posts

27 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 1
Affirm 1
View profile
daitienshi
Level 2

02-11-2016

Hi Ankur,

The dictionaries do have the keys TEST-001, TEST-002, TEST-003 during runtime, as I can retrieve those values via JSP.  I can even modify the values and see those changes come in via JSP after a page refresh