Question

I can translate an individual month or day just fine using my .po files:

echo __('December'); //becomes diciembre
echo __('Thursday'); //becomes jueves
//...etc

But, when I use a date formate like this:

 echo __(date("j F, Y"));  //becomes 20 December 2012

It doesn't translate - I assume because I have translations for each month and day in individual lines.

Normally I would just do something like this:

__(date('j')) . ' ' . __(date('F')) . ' ' . __(date('Y'));

But, in the CMS, the admin is allowed to change the date to any format they want. So, it could be "j F, Y", or "Y-m-d", or... anything else.

I thought maybe I could make a helper or something, that broke apart a date into pieces, and returns each part in a __(), but - this seems overkill. Is there an easy way to do this?

I am setting my locale in the AppController:

setlocale(LC_ALL, $currentLanguage['locale']);
Configure::write('Config.language', $currentLanguage['code2']);
Was it helpful?

Solution

Turns out CakePHP has a TimeHelper i18nFormat function:

$time = time();
$timestring = $this->Time->format('Y-m-d H:i:s', $time);
$this->Time->i18nFormat($timestring, "%A %e %B %Y");

Create a file "LC_TIME" (no extension) and put it in your /Locale/ara/ folder (or replace 'ara' with whatever 3-char language code you want)

Copy the contents of CakePHP's time_test LC_TIME file and put it into yours (then save of course).

Then change it's contents to whatever language you want (I believe that example is in Spanish).

That's it!

Notes:

More details about the LC_TIME file here: http://pic.dhe.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.files%2Fdoc%2Faixfiles%2FLC_TIME.htm

OTHER TIPS

The CakeTime class (and thus the TimeHelper) uses the 'cake' domain for day and month names translation. So put those translations in cake.po file instead of default.po

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top