Wednesday, May 15, 2013

Translating month names in CakePHP (i18n)

I found this, and I think it is worth saving.

Taken from: http://www.bravo-kernel.com/2010/12/using-lc_time-with-cakephp/


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):
1
2
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.
1
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):
1
2
3
4
5
6
7
8
9
10
$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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 ;)

No comments:

Post a Comment

cancel script completely on ctrl-c

I found this question interesting: basically how to cancel completely a script and all child processes : You do this by creating a subro...