Forum Discussion

nicgutierrez's avatar
11 years ago

How to use text.format

Hello.

I'm working on a custom component and I'm trying to use text.format to localize the copy.


Here's the sentence that I need to localise:

 

<h4>${notification.target.actor.login} @mentioned you in his post "${subject}"</h4>

and here's the output:

 

Nicolas @mentioned you in his post "name of the post" 

So, now that I need to localise the copy I was thinking of using text.format with some "placeholders".
I was searching and I found on the documentation something about the "text.format" parameters, so I changed my freemarker code into this:

 

<h4>${text.format( 'this_is_the_string_key_in_admin', notification.target.actor.login, subject )}</h4>

But I'm not really sure of how should the key in the Admin should looks like, so I tried:

 

this_is_the_string_key_in_admin = $1 @mentioned you in his post  "$2"

 

but the output is not what I was thinking of:

 

[login: null] @mentioned you in his post "[subject: null]" 


Does anybody know what I'm doing wrong or how the localization key should look like?

Thanks.
Nic

  • PaoloT's avatar
    PaoloT
    11 years ago

    Hi nicgutierrez 

     

    you can definitely use placeholders in your text keys and pass appropriate variables when you use them

     

    For example , if you defined custom-text-key = "My name is {0}"

     

    Then what you can do is:

     

    <#assign myName = "Slim Shady">
    ${text.format("custom-text-key",myName)}

    Or even having multiple placeholders and so on ...

     

    Hope it helps!

     

4 Replies

  • nicgutierrez  - You're using the text keys alright, just modify the text key and code as 

     

    this_is_the_string_key_in_admin = @mentioned you in his post
    <h4>${notification.target.actor.login} ${text.format( 'this_is_the_string_key_in_admin')} "${subject}"</h4>

     I hope this helps.

     

  • nicgutierrez's avatar
    nicgutierrez
    Ace
    11 years ago

    Hey GrazittiI .

    Thanks again for your help.


    I was thinking actually at a solution like this one the problem is that this is an easy example but for other string and other languages maybe the keys order could be different (like date formats) and I want to keep that open and easy to change.

    There is maybe another easy fix and should be create custom placeholders like %%user%% and %%subject%% and then user ?replace( '%%user%%', 'Nicolas' ) before echoing the string but I was looking for a native implementation.

    Thanks.

    Nic

  • PaoloT's avatar
    PaoloT
    Lithium Alumni (Retired)
    11 years ago

    Hi nicgutierrez 

     

    you can definitely use placeholders in your text keys and pass appropriate variables when you use them

     

    For example , if you defined custom-text-key = "My name is {0}"

     

    Then what you can do is:

     

    <#assign myName = "Slim Shady">
    ${text.format("custom-text-key",myName)}

    Or even having multiple placeholders and so on ...

     

    Hope it helps!

     

  • Ciao PaoloT .

    Works like a charm, thank you.
    I was trying to pass as argument some values from the REST call and I noticed that works only if you cast the values as strings, i.e.:

    ${text.format( 'pdc6.notifications.liked_string', notification.target.actor.login?string, subject?string )}

    Other than that: it's perfect.

    Thanks.
    Nic