November 24, 2014

CakePHP is a wonderful MVC-based rapid development web framework heavily inspired by Ruby on Rails. I recently struggled with creating localized date time strings that originated from a database and depending on some database flags should produce different datetime strings.

I tried using the time helper CakeTime and its method format, but this does not support for localization, and just calls PHP's strftime method.

After a while, it was clear that CakePHP doesn't localize datetime strings, because PHP already does when the correct locale is set. Thus the solutation was rather simple. I just put the following in the bootstrap.php file:

// Setting the locale to de_DE for strftime.
if (!setlocale(LC_ALL, 'de_DE.UTF-8')) {
    if (!setlocale(LC_ALL, 'de_DE')) {
        CakeLog::error('Could not set locale!');
    }
}

And everything worked out of the box, thanks to POSIX.

comments powered by Disqus

Topics: