Message Boards

Get localized expando value in freemarker template

Elena Stefanova, modified 6 Years ago.

Get localized expando value in freemarker template

Junior Member Posts: 42 Join Date: 8/24/15 Recent Posts
Hi, there!

I have the following problem. I've added a new custom attribute to my site pages. The attribute is of type "Localized text". Now I need to get and show this attribute value in one of my freemarker theme templates. However, I'm not able to do it by some reason. The site is multi-lingual and I want to view the correct value of this attribute according to the locale that is set currently. Code of type:
<#assign expandoAttribute = layout.getExpandoBridge().getAttribute("ATTRIBUTE_NAME") />

returns a result of type extended_hash, but I cannot access its elements the way I need.
I tried this way:
${expandoAttribute.get( locale )}

as well as:
 <#assign value = expandoAttribute[locale] />

and also:

<#list expandoAttribute?keys as key>
<#if key == locale>
<#assign value = expandoAttribute[key] />
<!--#if-->
<!--#list-->

but I got only debug errors saying that the value is evaluated to null or missing. emoticon

Any help or advice will be appreciated!

Thanks in advance!

Eli
thumbnail
Massimo Bevilacqua, modified 6 Years ago.

RE: Get localized expando value in freemarker template

Regular Member Posts: 210 Join Date: 12/27/16 Recent Posts
HI,

Try to print the key value to see if your the key locale is present.
&lt;#list expandoAttribute?keys as key&gt;
${key}
<!--#list-->


Otherwise try to use values instead of key(I had problem iterating map with keys)
&lt;#list expandoAttribute?values as field&gt;
${field}
<!--#list-->
Elena Stefanova, modified 6 Years ago.

RE: Get localized expando value in freemarker template

Junior Member Posts: 42 Join Date: 8/24/15 Recent Posts
Yes, this prints the correct keys and values but if I have 2 values for this attribute( for example en_GB and es_ES translations ), then I got all of them and I need to print only the current one - the one that is relevant to the current site locale.
thumbnail
Massimo Bevilacqua, modified 6 Years ago.

RE: Get localized expando value in freemarker template

Regular Member Posts: 210 Join Date: 12/27/16 Recent Posts
Which version of liferay are you using?
Elena Stefanova, modified 6 Years ago.

RE: Get localized expando value in freemarker template

Junior Member Posts: 42 Join Date: 8/24/15 Recent Posts
Liferay 7, ga4
thumbnail
Massimo Bevilacqua, modified 6 Years ago.

RE: Get localized expando value in freemarker template

Regular Member Posts: 210 Join Date: 12/27/16 Recent Posts
Could you please paste your template code so I can test it?
Elena Stefanova, modified 6 Years ago.

RE: Get localized expando value in freemarker template

Junior Member Posts: 42 Join Date: 8/24/15 Recent Posts
Ok, so here are the steps to reproduce the scenario:
1. Go to Control Panel -> Configuration -> Custom Fields -> Page -> Add Custom Field
2. Add a new field with key: Page Title, type: Localized Text.
3. Go to theme portal_normal.flt template and add the following code:

&lt;#assign expandoAttribute = layout.getExpandoBridge().getAttribute("Page Title") /&gt;
<!-- Print the attribute value with the current locale -->
<p>${expandoAttribute[locale]}</p>
or 
<p>${expandoAttribute.get( locale )}</p>

4. Go and test this on some page on frontend.
thumbnail
Massimo Bevilacqua, modified 6 Years ago.

RE: Get localized expando value in freemarker template

Regular Member Posts: 210 Join Date: 12/27/16 Recent Posts
Now I understand the problem,
if you have custom attribute and for example you add as value "Hello" in english and "Hallo" in german when you retrive the values liferay print
"HelloHallo"
This is how data is stored in my case:
{valueId=49020, companyId=20116, tableId=48997, columnId=49000, rowId=49019, classNameId=29201, classPK=49018, data=HalloHello}


I made some test but I was not able to solve the problem, I am sorry.
Elena Stefanova, modified 6 Years ago.

RE: Get localized expando value in freemarker template

Junior Member Posts: 42 Join Date: 8/24/15 Recent Posts
Ok, Massimo!

Thank you very much anyway!
Elena Stefanova, modified 6 Years ago.

RE: Get localized expando value in freemarker template

Junior Member Posts: 42 Join Date: 8/24/15 Recent Posts
I finally managed to find a solution that works for me and it looks like this:

&lt;#list expandoAttribute?keys as key&gt;
&lt;#if key == locale&gt;
&lt;#assign value = expandoAttribute?values[key_index] /&gt;
<!--#if-->
<!--#list-->

${value}


Apparently, it was a matter of choosing the correct freemarker method to access the extended_hash value. Might not be the best solution, but at least it helped me get what I need emoticon
Mladen B., modified 5 Years ago.

RE: Get localized expando value in freemarker template

New Member Posts: 2 Join Date: 5/17/18 Recent Posts

I also had this issue and it took me several hours to finally resolve it (Liferay Community Edition Portal 7.0.6 GA7)... It's really unfortunate that in this version of LR we still have this issue, preventing people from doing basic actions, like properly iterate a collection...

 

Anyway, your example helped a lot, with a remark that key_index should actually be key?index

thumbnail
Aravinth Kumar, modified 4 Years ago.

RE: Get localized expando value in freemarker template

Regular Member Posts: 152 Join Date: 6/26/13 Recent Posts
Hi All,
Is there any better solution provided in the recent versions?
Thanks in Advance.

Regards,
Aravinth