Cakephp翻译行为
下面是我们如何使用转换行为与cakephp模型关系。我正在努力提高这段代码的质量Cakephp翻译行为,php,refactoring,cakephp-2.0,Php,Refactoring,Cakephp 2.0,下面是我们如何使用转换行为与cakephp模型关系。我正在努力提高这段代码的质量 public function edit($id = null) { if (!$this->Faq->exists($id)) { throw new NotFoundException(__('Invalid faq')); } if ($this->request->is('post') || $this->request->is('
public function edit($id = null) {
if (!$this->Faq->exists($id)) {
throw new NotFoundException(__('Invalid faq'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Faq->saveMany($this->request->data)) {
$this->Session->setFlash('The faq has been saved', 'default', array('class' => 'success'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The faq could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Faq.' . $this->Faq->primaryKey => $id));
$this->request->data = $this->Faq->find('first', $options);
}
$languages = $this->Language->getlangs();
if(is_array($this->{$this->modelClass}->belongsTo)) {
foreach($this->{$this->modelClass}->belongsTo as $relation => $model) {
foreach($languages as $lang){
$this->{$this->modelClass}->$model['className']->locale = $lang['Language']['language_locale'];
$faqCategories[$lang['Language']['language_locale']] = $this->Faq->FaqCategory->find('list', array('conditions' => array('FaqCategory.is_active' => 1), 'recursive' => 1));
}
}
}
$this->set(compact('faqCategories'));
}
它工作得很好,但我希望有一个具有同等功能的高质量代码。
提前感谢。TranslateBehavior实际上非常容易设置,只需很少的配置就可以开箱即用。在本节中,您将学习如何添加和设置要在任何模型中使用的行为 如果在可包含问题旁边使用TranslateBehavior,请确保为查询设置“字段”键。否则,最终可能会生成无效的SQL ****初始化i18n数据库表****
./cake i18n
正确的布局
class Post extends AppModel {
public $actsAs = array(
'Translate'
);
}
阅读翻译内容
默认情况下,TranslateBehavior将根据当前区域设置自动获取和添加数据。当前区域设置是从L10n类指定的Configure::read('Config.language')读取的。您可以使用
$Model->locale