我试图让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'
)
);
}
答案 0 :(得分: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)
)
);}
}
答案 1 :(得分:4)
实现这一目标的正确方法是:
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
答案 2 :(得分:-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)
)
)
);