Dynamic locale in HTL date object | Community
Skip to main content
Level 2
January 4, 2021
Solved

Dynamic locale in HTL date object

  • January 4, 2021
  • 2 replies
  • 2244 views

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? 

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

Hi @richard_bystrom 

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

 

return "sv";

2 replies

Vijayalakshmi_S
Level 10
January 4, 2021

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?

Level 2
January 4, 2021

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.

Manjunath_K
Manjunath_KAccepted solution
Level 7
January 4, 2021

Hi @richard_bystrom 

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

 

return "sv";

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