4

我正在尝试让 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'
            )
);
}
4

3 回答 3

10

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

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)
                    )
        );}
        }
于 2012-05-21T08:12:39.887 回答
4

实现这一目标的正确方法是:

class AppModel extends Model {

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

内部 cake 将调用:

__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

于 2013-08-28T04:30:20.303 回答
-1

您的 $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)
        )
    )
);
于 2012-05-21T08:33:48.827 回答