模型消息的 Cakephp 2.0 本地化



我正在尝试让 i18n 从 Cakephp 2.0 中的模型中提取字符串

文件指出"CakePHP 会自动假设您的$validate数组中的所有模型验证错误消息都是要本地化的。当运行 i18n shell 时,这些字符串也会被提取出来。http://book.cakephp.org/2.0/en/core-libraries/internationalization-and-localization.html但是当我运行 cake i18n 并提取数据时,我在模型中的消息没有被提取到我的 po 文件中。

有谁知道如何将消息字符串放入 po 文件中?

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'
            )
);
}

这就是解决我遇到的问题的方法。

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

实现此目的的正确方法是:

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

你的$validate结构有点混乱,你在必需的键下有两个相同的数组键(规则,消息(。它应该是:

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)
        )
    )
);

最新更新