Loop over the keys of a resourceBoundle

264 views
Skip to first unread message

Gangel Dávid

unread,
Nov 19, 2015, 6:29:11 AM11/19/15
to Hippo Community
Hi,

I would like to know are there any way to loop over the keys of a resourceBoundle in freemarker template?

Thank you.

Woonsan Ko

unread,
Nov 19, 2015, 8:08:10 AM11/19/15
to hippo-c...@googlegroups.com
I haven't tried it yet, but maybe:

<#list bundle.keys as key>
  <p>${key} = ${bundle("key", ...)}
</#list>

The javadoc [1] says you can pass message args in bundle("key", ...").



--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-c...@googlegroups.com
RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-communi...@googlegroups.com.
Visit this group at http://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.



--
w....@onehippo.com     www.onehippo.com
Boston - 745 Atlantic Ave, 8th Floor, Boston MA 02111
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466

Gangel Dávid

unread,
Nov 19, 2015, 11:33:45 AM11/19/15
to Hippo Community
Hi,

Sorry but I don't really get how to use this.
Can you please add more details?

Thank you.

Woonsan Ko

unread,
Nov 19, 2015, 8:52:27 PM11/19/15
to hippo-c...@googlegroups.com
The following works for me:

  <@hst.setBundle var="localizationContext" basename="org.onehippo.hst.demo.resources.GeneralTranslationsBundle" />
  <#assign bundle = localizationContext.resourceBundle>
  <ul>
    <#list bundle.keys as key>
      <li>${key} = ${bundle(key)}</li>
    </#list>
  </ul>

<@hst.setBundle/> tag library sets javax.servlet.jsp.jstl.fmt.LocalizationContext. The basename can be used to retrieve either repository stored resource bundle or java standard resource bundle.

HTH,

Woonsan

Gangel Dávid

unread,
Nov 20, 2015, 6:55:41 AM11/20/15
to Hippo Community
Hi,

Now works perfectly!
Thank you!

My last question would be how can I order the bundles in alphabetical order?
Maybe this is the Index column in the CMS?

Thank you.

Woonsan Ko

unread,
Nov 20, 2015, 11:26:33 AM11/20/15
to hippo-c...@googlegroups.com
I think you can simply store alphabetically ordered keys from the ResourceBundle and pass it to the ftl template, right?

Regarding the 'index', the default ordering of the keys in Java ResourceBundle is by the presence order in the properties file or repository resource document.
Repository resource bundle document doesn't have index field actually, but the index is just the presence order index of the property value.
Anyway, in properties file (for resource bundle resource or documents), the presence order has an important role like variable interpolations [1].
We support variable interpolation in repository resource bundle documents, which is obviously unrelated to the alphabetical order, but presence order.
Of course, I have to admit that the resource bundle document editor seems a bit confusing if end users need to use variable interpolation based on the index. I believe we need to improve the user experience there (e.g, at least change ordering by clicking on column).

HTH,

Woonsan


Gangel Dávid

unread,
Nov 21, 2015, 9:04:09 AM11/21/15
to Hippo Community
Hi,

Let me attach a screenshot from the CMS where I have the resource boundle.
Now the order of the languages on the site is the following:
English
Magyar
Deutsch

The template code that generates:
<#list bundle.keys as key>
  <li><a>${bundle(key)}</a></li>
</#list>  

I think there is nothing special, however I don't see any logic behind this generated order. 
1. So maybe it's a bug?
2. I agree with this and will create a ticket if we close the conversation: "I have to admit that the resource bundle document editor seems a bit confusing if end users need to use variable interpolation based on the index. I believe we need to improve the user experience there (e.g, at least change ordering by clicking on column)."
3. Maybe I should create a list inside the freemarker template, collect the data inside that and use the built in sorting for freemarker to get this work?(or that was you meant?) I don't have any custom component that handles the resourceBoundles.

Thank you.
Screenshot from 2015-11-21 14:55:05.png

Woonsan Ko

unread,
Nov 21, 2015, 1:58:16 PM11/21/15
to hippo-c...@googlegroups.com
On Sat, Nov 21, 2015 at 9:04 AM, Gangel Dávid <gan...@gmail.com> wrote:
Hi,

Let me attach a screenshot from the CMS where I have the resource boundle.
Now the order of the languages on the site is the following:
English
Magyar
Deutsch

The template code that generates:
<#list bundle.keys as key>
  <li><a>${bundle(key)}</a></li>
</#list>  

I think there is nothing special, however I don't see any logic behind this generated order. 
1. So maybe it's a bug?

If so, it seems like a bug. The index value might not be reflecting the order of the property values (which you can confirm through CMS Console), or the ResourceBundle object (HST creates internally) might be returning a wrong order in #getKeys().
If you can export the resource bundle document content through CMS Console and attach the screenshot with the description you mentioned here in a JIRA ticket, it would be greatly appreciated! Thanks in advance!
 
2. I agree with this and will create a ticket if we close the conversation: "I have to admit that the resource bundle document editor seems a bit confusing if end users need to use variable interpolation based on the index. I believe we need to improve the user experience there (e.g, at least change ordering by clicking on column)."

There seems no existing ticket for this. If you can file an improvement ticket in https://issues.onehippo.com/projects/HIPPLUG, it would be also appreciated.
 
3. Maybe I should create a list inside the freemarker template, collect the data inside that and use the built in sorting for freemarker to get this work?(or that was you meant?) I don't have any custom component that handles the resourceBoundles.

I meant sorting in a component code in prior, but your idea also could work.
Maybe you can try this (got hint from [1]):

<#list bundle.keys?sort as key>
  <li><a>${bundle(key)}</a></li>
</#list>

Gangel Dávid

unread,
Dec 13, 2015, 8:49:58 AM12/13/15
to Hippo Community

Woonsan Ko

unread,
Dec 13, 2015, 9:55:01 PM12/13/15
to hippo-c...@googlegroups.com
Thanks!


For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages