CakePh 2.0 Localization for Model Messages

I am trying to get i18n to retrieve 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 for localization. When you run the i18n shell, these lines 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 extracted to my po file when I run cake i18n and retrieve the data.

Does anyone know how to get message strings in 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
source share
3 answers

, .

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)
                    )
        );}
        }
+9

:

class AppModel extends Model {

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

:

__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

+4

The $ validate structure is a bit confused, you have two identical array keys (usually a 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)
        )
    )
);
-1
source

All Articles