Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Blog Module

Discuss the Blog Module.

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

Date Field in Blog Module


Reply

13 Posts   6500 Views

Avatar
spierala

9 March 2010 at 10:45pm Community Member, 80 Posts

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

Avatar
klikhier

29 October 2010 at 6:41am (Last edited: 30 October 2010 3:14am), Community Member, 150 Posts

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));
}

Avatar
micschk

25 March 2011 at 9:56pm (Last edited: 25 March 2011 9:58pm), Community Member, 19 Posts

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...

Avatar
klikhier

26 March 2011 at 1:17am Community Member, 150 Posts

Many thanks, I'll try this myself!

Avatar
rsjq

20 April 2013 at 4:19am (Last edited: 20 April 2013 4:19am), Community Member, 2 Posts

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

Bug reports and improvement suggestions are most welcome!