Forum Discussion

domladden's avatar
9 years ago

Translating dates

We have the publication date of posts written like this in English:

 

08 January, 2015

 

Which is configured like this:

 

<#assign posttime = rest("messages/id/${msg_id}/post_time").value/>
<#assign postTimeObject = posttime?datetime("yyyy-MM-dd'T'hh:mm:ss") />

<p>${postTimeObject?string("dd MMMM")}, ${postTimeObject?string("yyyy")}</p>

This works fine but is the same across all languages. We have publications in 5 languages all in all so we'd obviously prefer it if we could add in a way of rendering all dates in the relevant languages.

 

I can't see anyway of doing it other than manually translating all the months, anyone had any similar experience with this?

 

Thanks,

 

Dom

 

  • DougS's avatar
    DougS
    Khoros Oracle

    The REST response provides you with dates that are formatted the same as they are in Lithium "core" components -- here is some logic that you can add to display the formatted date:

     

    <#-- Display the friendly date of the post if Use Relative Dates is configured in Community Admin -->
    <span class=“post-time”>
    <#if t.post_time.@view_friendly_date[0]??>
      ${t.post_time.@view_friendly_date}
    <#-- Otherwise default to the date format configured -->
    <#else>
      ${t.post_time.@view_date}
    </#if>
    </span>

    If that doesn't give you what you want, take a look at the datesupport freemarker object, which gives you some useful utility methods to modify and format dates (go to freemarker documentation, select the "Refer" drop-down, select "context objects", then click on the link for "datesupport" to view that section).

     

    If you want to use one of the formatting methods in the datesupport object, it should give you a date that is localized differently based on language (using the same formatting dates that core components are localized using in the community). If you use that, you probably want to check for a "friendly date" first, and then fall back to getting a formatted date -- something like this:

     

    <#assign ds = datesupport />
    <#assign thisDate = ds.parseAsIso8601(message.post_time) />
    <#if coreNode.settings.name.get("layout.friendly_dates_enabled") == "true">
    	<#assign post_date = ds.setDate(thisDate).friendlyDateAsString!"" />
    </#if>
    <#if !post_date?? || post_date == "">
    	<#assign post_date = text.format("custom.dateformat.datetime", ds.setDate(thisDate).dateAsString, ds.setDate(thisDate).timeAsString)  />
    </#if>
    <span class=“post-time”>${post_date}</span>

    (In this example, you'd also need to add  a custom text key named custom.dateformat.datetime with the value {0} {1})

     

    If you need to completely roll your own (maybe the datesupport object doesn't provide you what you are looking for), you could store the date format as a custom text key variable (using the "text editor" tab in studio), then reference that text key in your component -- something like this maybe:

     

     

    <#assign posttime = rest("messages/id/${msg_id}/post_time").value/>
    <#assign postTimeObject = posttime?datetime("yyyy-MM-dd'T'hh:mm:ss") />
    <#assign dateFormat = text.format("custom.dateformat.date") />
    
    <p>${postTimeObject?string(dateFormat)}</p>

     

     

    Then you'll be able to change the format on a per-language basis. The value of the custom.dateformat.date text key in the above example would probably be dd MMMM, yyyy (based on the example you gave).

     

    I hope that helps!

     

    -Doug