Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
SOLVED

Dynamic locale in HTL date object

Avatar

Level 2

I am currently working on a list component in AEM 6.4.8 which shows the last edited date for the pages listed. For this I'm using the built in HTL properties like so:

${'dd MMMM yyyy' @ format=item.lastModified}

 

Now I am trying to update this so it can be used in various languages rather than just in English. The same property above can also have a locale, and that is working fine. However, that is fairly meaningless since that would still be hardcoded to a single locale. So I've been trying to fetch the language value of the current page from the backend. So when I hardcode this value in the HTL like this:

${'dd MMMM yyyy' @ format=item.lastModified,locale='sv'}

 

It works just fine - component is rendered properly and month names are printed in Swedish. However, when I try to get value from the backend and the backend returns the value 'sv' I get an error, and I'm not sure if this is a bug or something that I am missing. This is the HTL:

${'dd MMMM yyyy' @ format=item.lastModified,locale=item.language}

And the error I get when trying to use this is:

org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: Invalid locale format: 'sv'
	at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:510) [org.apache.sling.scripting.core:2.0.54]
	at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:552) [org.apache.sling.engine:2.7.2]
	at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:44) [org.apache.sling.engine:2.7.2]
	at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:82) [org.apache.sling.engine:2.7.2]
	at com.day.cq.wcm.core.impl.WCMDeveloperModeFilter.doFilterWithErrorHandling(WCMDeveloperModeFilter.java:164) [com.day.cq.wcm.cq-wcm-core:5.11.312]
	at com.day.cq.wcm.core.impl.WCMDeveloperModeFilter.doFilter(WCMDeveloperModeFilter.java:135) [com.day.cq.wcm.cq-wcm-core:5.11.312]

 

So it is complaining about an invalid locale format - despite the value being exactly the same in the logs as what I hardcoded on the HTL frontend. So, is this a bug? Or is it simply not possible to fetch values from the backend for the HTL date? Or am I doing something else wrong? 

1 Accepted Solution

Avatar

Correct answer by
Level 8

Hi @Richard_Bystrom 

Send language code from backend without single quote, it will resolve the issue.

 

return "sv";

View solution in original post

6 Replies

Avatar

Community Advisor

Hi @Richard_Bystrom,

Can you let know the data type of "language" (item.language used in your HTL) in backend. Is it a Locale object or String?

Hello! Currently I am returning it as a simple string from the backend, like this:

return "'sv'";

 I also tried escaping the quotes, to no success.

Avatar

Community Advisor
Try storing the locale value in a variable and return the variable
Tried storing the value as a String and return that variable to the frontend. Same result - "invalid locale format"

Avatar

Correct answer by
Level 8

Hi @Richard_Bystrom 

Send language code from backend without single quote, it will resolve the issue.

 

return "sv";

Avatar

Level 2
I was entirely sure I had already tried this - but apparently not! Works great now, thanks for the help!