3
votes

I need to have fallback in translation.

If I have 2 translations ex.: 'eng' and 'es' Some records have both translations and some only one or none. I hoped that if I do it this way:

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

I'll get 'es' translation and if 'es' is not available I'll get 'eng'. but this does not seem to work. If I set 'locale' to a single value 'eng' or 'es' it works fine, but when I set $this->locale = ['es', 'eng']; It seems to be ignored and no translation is taken into the result, just data from model table.

1

1 Answers

0
votes

I had the same problem and did not find a sufficient solution, so I used the following workaround:

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

Remarks: The field "content" is the one being translated with the Translate Behaviour.