Using LC_TIME with CakePHP

CakePHP supports LC_TIME translations since version 1.3 and since it took me some time to completely figure out the logic behind it I am storing my notes here as a mental reminder to self (again).

First things first… make sure you read  the i18n paragraph on LC_TIME in the Book to get some basic understanding of what we are trying to do here.

Preparing for LC_TIME

For __c(), $this->Time->format() and $this->Time->i18nFormat() to work:

  1. create a file called /app/locale/dut/LC_TIME
  2. on your local Linux workstation open /usr/share/i18n/locales/nl_NL
  3. copy everything between LC_TIME and END LC_TIME to the file created in step 1 and save that file

Note: make sure to add the escape_char and comment_char definitions to your LC_TIME file as well or your setup will not be fully functional (see this page for more info):


comment_char %
escape_char  /

strftime()

There is no need to use php’s strftime() if you consistently stick to using the CakePHP functions mentioned above. However, if you do need to get strftime() up and running you should add the following line to one of your controllers.


setlocale(LC_TIME, 'nl_NL.UTF8');

Please note (and understand) that this will use your server’s locales and NOT your manually created Cake LC_TIME file.

Testing your LC_TIME setup

Add the following lines to one of your views (and make sure the TimeHelper is available):


$timestamp = time();
 $timestring = $this->Time->format('Y-m-d H:i:s', $timestamp);
 $months = __c('mon', 5 ,true);

 pr("Timestamp = $timestamp");
 pr("Timestring = $timestring");
 pr("strftime() translated = " . strftime("%A %e %B %Y", strtotime($timestring)));
 pr("i18nFormat  translated = " . $this->Time->i18nFormat($timestring, "%A %e %B %Y"));
 pr("Time::format translated = " . $this->Time->format($timestring, '%A %e %B %Y'));
 pr($months);

If your setup is fully operational it should display the following LC_TIME translations:

 Timestamp = 1292417839
Timestring = 2010-12-15 13:57:19
strftime() translated = Wednesday 15 December 2010
i18nFormat  translated = woensdag 15 december 2010
Time::format translated = woensdag 15 december 2010
Array
(
    [0] => januari
    [1] => februari
    [2] => maart
    [3] => april
    [4] => mei
    [5] => juni
    [6] => juli
    [7] => augustus
    [8] => september
    [9] => oktober
    [10] => november
    [11] => december
)

Note: you might have spotted that the strftime() translation is not translated. This is intentional since I always stick to Cake methods. See the paragraph on strftime() if you do need to use that function.

Enjoy your time translations ;)

Posted in: CakePHP, Coding

One comment on “Using LC_TIME with CakePHP
  1. Bravo_Kernel says:

    I just realized that I missed pointing out that you will need to use the PHP strftime() format parameters to format your translated time-strings. An overview of all supported parameters can be found at http://www.php.net/manual/en/function.strftime.php

1 Pings/Trackbacks for "Using LC_TIME with CakePHP"
  1. [...] or the PHP strftime() functions . I found a good tutorial on getting it set up in your environment here. Luckily I develop on Linux so i was able to find the LC_TIME files and import them in my [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>