Jump to:

1835 Posts in 1139 Topics by 554 members

Blog Module

SilverStripe Forums » Blog Module » Date Field in Blog Module

Discuss the Blog Module.

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

Page: 1 2
Go to End
Author Topic: 6210 Views
  • spierala
    Avatar
    Community Member
    80 Posts

    Re: Date Field in Blog Module Link to this post

    thanks,

    adding

    setlocale(LC_TIME, i18n::get_locale() . ".utf8");

    to the function FormatI18N also helped me to solve an "umlaut" - bug with "märz".
    thank you,
    florian

  • klikhier
    Avatar
    Community Member
    150 Posts

    Re: Date Field in Blog Module Link to this post

    I've tried all of the above on an SS2.4.2 install:

    Added this in _config.php:

    Translatable::set_default_locale('nl_NL');
    Object::add_extension('SiteTree', 'Translatable');
    Object::add_extension('SiteConfig', 'Translatable');
    i18n::set_locale('nl_NL');
    setlocale(LC_TIME, 'nl_NL');

    Altered function FormatI18N() in sapphire/core/model/fieldtypes/Date.php:

    function FormatI18N($formattingString) {
    setlocale(LC_TIME, i18n::get_locale() . ".utf8");
    if($this->value) return strftime($formattingString, strtotime($this->value));
    }

    Have this in my BlogEntry.ss:

    $Date.FormatI18N(%e %B)

    And still I get 'October' instead of the Dutch 'oktober'. What am I doing wrong?

    PS. $Locale in my Page.ss does provide me with nl_NL

    PPS. It does work on my local MAMP environment. On shared hosting it doesnot work, even with this code (server issue?!):

    function FormatI18N($formattingString) {
    setlocale(LC_TIME, i18n::get_locale() . ".utf8");
    setlocale(LC_TIME, 'nl_NL.UTF8');
    if($this->value) return strftime($formattingString, strtotime($this->value));
    }

  • micschk
    Avatar
    Community Member
    18 Posts

    Re: Date Field in Blog Module Link to this post

    As it also took me quite some time to get datefields printed in the correct locale (of the visitor), I'll outline how I achieved this (SS2.4.5);

    To translate $Date fields for the current locale, first set the correct locale. This code works for me if I put it in the Page_Controller (_config.php seems to be too early):

    // mysite/code/Page.php
    class Page_Controller extends ContentController {

       public function init() {
       
          ...

          // Set locale for PHP, dates etc;
          setlocale(LC_TIME, Translatable::get_current_locale() . ".UTF8");

          // using i18n::get_locale() didn't seem to work for me;
          // setlocale(LC_TIME, i18n::get_locale() . ".utf8");

       }
    }

    Then cast the date to the current locale in your template;

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

    If this is not working, make sure you add a ?flush=1 to the URL.

    Still not working, it could be that the locale is not installed on the server. If you have a shell account, do a

    locale -a

    This shows you all installed locales. If the needed locale is indeed not installed, you can install it yourself if you have root (Debian/Ubuntu);

    dpkg-reconfigure locales

    And add the neede locale. It should also be possible to install locales in your home directory (as non-root), Google probably knows best...

  • klikhier
    Avatar
    Community Member
    150 Posts

    Re: Date Field in Blog Module Link to this post

    Many thanks, I'll try this myself!

  • rsjq
    Avatar
    Community Member
    2 Posts

    Re: Date Field in Blog Module 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!

    6210 Views
Page: 1 2
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.