CakePHP 1.2 doesn't use 'lazy loading', the Model may therefore be loaded/constructed before you set the language/locale (for example, inside your AppController::beforeFilter()
).
Things to consider
It may be worth considering;
- If these strings should already be translated inside the constructor (you won't be using them all the time)
- If these strings should be translated inside the model at all
Especially this last point; IMO translating strings is part of the 'presentation layer'; i.e. belongs inside a View (or a Helper).
Translate 'on demand'; at the last possible moment
This also allows you to switch the language, without the Model having already translated the strings;
In your model:
class Contact extends AppModel
{
protected $_sexOptions = array(
'U' => 'Choose Sex',
'M' => 'Male',
'F' => 'Female',
);
public function getSexOptions()
{
$out = array();
foreach ($this->_sexOptions as $k => $v) {
$out[$k] = __($v, true);
}
return $out;
}
}
Inside your controller;
$this->set('sex', $this->Contact->getSexOptions());
Translate inside the 'presentation' layer; i.e. your view:
Create a simple helper to make this re-usable;
app/views/helpers/options.php
class OptionsHelper extends AppHelper
{
/**
* translates options
*/
public function translate($options)
{
$out = array();
foreach (options as $k => $v) {
$out[$k] = __($v, true);
}
return $out;
}
}
Add this helper to your controller;
public $helpers = array(
// ... other helpers
'options',
);
Inside your view;
echo $this->Form->input('sex', array(
'type' => 'radio',
'options' => $this->Options->translate($sex)
));