4
votes

I am trying to get i18n to extract the strings from my model in Cakephp 2.0

The documentation states that "CakePHP will automatically assume that all model validation error messages in your $validate array are intended to be localized. When running the i18n shell these strings will also be extracted." http://book.cakephp.org/2.0/en/core-libraries/internationalization-and-localization.html But my messages in my model are not being extracted into my po file when I run cake i18n and extract the data.

Does anyone know how to get the message strings into the po file?

App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
 public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A Username is required',
                 'rule' => 'isUnique',
                'message' => 'This username has already been taken'
            )
);
}
3

3 Answers

10
votes

This is how you can solve the problem I came across.

App::uses('AuthComponent', 'Controller/Component');
        class User extends AppModel {
         function __construct() {
                parent::__construct();
                $this->validate = array(
                'username' => array(
                    'required' => array(
                        'rule' => array('notEmpty'))
                        'message' => __('A Username is required', true)), 
                      'unique' => array(
                        'rule' => 'isUnique',
                        'message' => _('This username has already been taken', true)
                    )
        );}
        }
4
votes

The correct way of achieve this is:

class AppModel extends Model {

    public $validationDomain  = 'validation_errors';
.
.
.
}

internally cake will call:

__d('validation_errors', 'Username should be more fun bla bla');

http://book.cakephp.org/2.0/en/console-and-shells/i18n-shell.html#model-validation-messages

http://book.cakephp.org/2.0/en/core-libraries/internationalization-and-localization.html#translating-model-validation-errors

-1
votes

Your $validate structure is a little messed up, you have two identical array keys (rule,message) under the required key. It should be:

public $validate = array(
    'username' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => __('A Username is required', true),
        ),
        'unique'=>array(
            'rule' => 'isUnique',
            'message' => __('This username has already been taken', true)
        )
    )
);