Forum Discussion

muraleedharan's avatar
4 years ago

text key with condition

I'm trying to use this key with a custom component 

li.memberships.MembershipCount.count = {count, plural, one{1 member} other{# members}}

How I can pass the mentioned parameters in text.format("li.memberships.MembershipCount.count") to get the results with the plural condition.

answer with an example code as great help

3 Replies

  • AdamN's avatar
    AdamN
    Khoros Oracle
    4 years ago

    Hi muraleedharan , you can pass in the values as additional parameters for text.format. For example:

    <#assign membershipCount = 123 />
    ${text.format("li.memberships.MembershipCount.count", membershipCount)}

    text.format is overloaded to accept up to  6 arguments for core and custom text keys.

    I hope this helps!

  • Hi AdamN 

    Thanks for your update and providing an example, but unfortunately that is not working for me.

    These are the keys I want to looking to pass parameters, 

    li.nodes.NodeCreationDate.date = Created {date}

    li.memberships.MembershipCount.count = {count, plural, one{1 member} other{# members}}

     

  • AdamN's avatar
    AdamN
    Khoros Oracle
    4 years ago

    muraleedharan You're right, I think I see the issue. The text keys you're referencing I think are used by some of our JavaScript based components, and use a different format that isn't compatible with the Freemarker text.format context object.

    The text keys compatible with text.format have a slightly different syntax. For example:

    message.message-view.kudos-count.label             = {0} {0,choice,0#Kudos|1#Kudo|1<Kudos}

    I created a test component like this:

    ${text.format("message.message-view.kudos-count.label",123)}

    And the output was:

    123 Kudos

     

    So what you could do instead is to create a couple of custom text keys like:

    custom.li.nodes.NodeCreationDate.date = Created {0}
    custom.li.memberships.MembershipCount.count = {0} {0,choice,0#members|1#member|1<members}

    And then in your custom component, you'd have:

    <#-- Could be from API result or other source -->
    <#assign myDate = "10/22/21" /> 
    <#assign myMembershipCount = 123 />
    
    ${text.format("custom.li.nodes.NodeCreationDate.date",myDate)}<br/>
    ${text.format("custom.li.memberships.MembershipCount.count",myMembershipCount)}

    And this would render: 

    Created 10/22/21
    123 members

     

    I hope this helps!