Jump to:

23358 Posts in 17875 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » How to translate date?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 3171 Views
  • Digital-Punk.co.uk
    Avatar
    Community Member
    51 Posts

    How to translate date? Link to this post

    I use in template $Now.Day and got Thuesday. My problem is - the website is multilingual and I can't get this date in German - Dienstag (Thuesday). Has anybody any ideas how to solve this problem?

    Thanks for any help!

    Mindaugas

  • ChuckGyver
    Avatar
    Community Member
    5 Posts

    Re: How to translate date? Link to this post

    One Solution is to write in /sapphire/core/model/fieldtypes/Date.php, a new function like

    function German() {
    if($this->value)
    return strftime('%e. %B %G', strtotime($this->value));
    }


    (in the template use it with .German for Example $Now.German)

    and where is the translation?
    with setlocale

    for example: add in _config.php

    setlocale (LC_TIME,"de_DE");

    have a look on http://at.php.net/manual/de/function.date.php and especiallyhttp://at.php.net/manual/de/function.strftime.php for the format strings and the difference between date() and strftime().
    Anyway, the wanted locale has to be installed on the system.

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: How to translate date? Link to this post

    I use the following in the templates of a Dutch site:

    $Date.FormatI18N(%e %B %Y)

  • Digital-Punk.co.uk
    Avatar
    Community Member
    51 Posts

    Re: How to translate date? Link to this post

    Thanks you guys a lot for the help, but it doesn't worked for me. So, I did it with js and currentlang function:

    <% if CurrentLang = sv %>
       <script type="text/javascript">
          <!--
          var m_names = new Array("Söndag", "MÃ¥ndag", "Tisdag", "Onsdag", "Torsdag", "Fredag", "Lördag");
          var d = new Date();
          var curr_date = d.getDate();
          var curr_weekday = d.getDay();
          var curr_month = d.getMonth();
          var curr_year = d.getFullYear();
          document.write(m_names[curr_weekday]);            
          //-->
       </script>
    <% end_if %>

    Hope it helps for somebody also

  • m-phil
    Avatar
    Community Member
    37 Posts

    Re: How to translate date? Link to this post

    here is a better but not perfect solution. I've also expand the Date class -> /sapphire/core/model/fieldtypes/Date.php

    // Bugfix to translate english month names to current locale
       function TranslatedMonth() {
          if($this->value)
             $currentMonth = '';
             //echo Translatable::get_current_locale().' -> '.(int)strftime('%m', strtotime($this->value));
             if (Translatable::get_current_locale() == 'de_DE') { // german
                $month = array(1 => "Januar",
                 2 => "Februar",
                 3 => "M&auml;rz",
                 4 => "April",
                 5 => "Mai",
                 6 => "Juni",
                 7 => "Juli",
                 8 => "August",
                 9 => "September",
                 10 => "Oktober",
                 11 => "November",
                 12 => "Dezember");
                
                $currentMonth = $month[(int)strftime('%m', strtotime($this->value))];
             } else {
                $currentMonth = strftime('%B', strtotime($this->value));
             }
             return $currentMonth;
       }

    You can add another elseif for your language translation.
    The better way should be to include the language files with its translation for every month and day...

  • rsjq
    Avatar
    Community Member
    2 Posts

    Re: How to translate date? Link to this post

    I have created a small module for SS3 that will translate dates automatically once added to your project. You can check it out here: https://github.com/richardsjoqvist/silverstripe-localdate

    Bug reports and improvement suggestions are most welcome!

    3171 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.