3

我需要翻译后备。

如果我有 2 个翻译,例如:'eng' 和 'es' 有些记录有两种翻译,有些只有一种或没有。我希望如果我这样做:

$this->Post->locale = ['es', 'eng'];
$results = $this->Post->find('first', array(
    'conditions' => array('Post.id' => $id)
));

我会得到“es”翻译,如果“es”不可用,我会得到“eng”。但这似乎不起作用。如果我将 'locale' 设置为单个值 'eng' 或 'es' 它工作正常,但是当我设置 $this->locale = ['es', 'eng']; 它似乎被忽略了,结果没有翻译,只是模型表中的数据。

4

1 回答 1

0

我遇到了同样的问题,没有找到足够的解决方案,所以我使用了以下解决方法:

        if (!isset($data['Page']['content'])) {
            // TRANSLATION NOT AVAILABLE or empty content -> take default lang
            $this->Page->locale = Configure::read('Page.default_language');
            $data = $this->Page->find('first', $options);
            if (!isset($data['Page'])) {
            // Not even the default translation found
                throw new NotFoundException();
            } 
        }

备注:“内容”字段是使用翻译行为进行翻译的字段。

于 2016-01-04T14:16:18.903 回答