David
David

Reputation: 1

Get single value of extended hash in Freemarker

I have an extended hash (that I didn't write by myself) of which I need to extract a single value (i.e. I don't need to list all out). Since this code get me the pairs of key/value

<#list tranlink as key,val>
   ${key} - ${val}
</#list>

That is

en_US - link_to_us
it_IT - link_to_it

How can I get the single value by key? I've tried

${tranlink.en_US}
${tranlink[en_US]}
${tranlink["en_US"]}

But I always get an error. Can someone help me please?

Upvotes: 0

Views: 342

Answers (1)

ddekany
ddekany

Reputation: 31152

I suspect the problem here is that the keys in the backing Java Map aren't String-s, but Locale objects. So #list will work, but tranlink.en_US and tranlink["en_US"] will do the lookup with a String key, and so they find nothing. In FreeMarker 2.x, when using . or [] operators, the key is always a strings. If FreeMarker configuration allows it, you can call Java API like tranlink?api.get(...), and that supports any type of key. But, you still will need the key Locale object from somewhere, as FreeMarker doesn't support creating Locale objects out-of-the-box.

Upvotes: 1

Related Questions