user3661042
user3661042

Reputation: 167

Cakephp 3 does not work for the default language

Sorry for my english, but I hope you will understand me. Simplified code looks like this:

//in bootstrap.php

ini_set('intl.default_locale', 'deu');

// MainMenusTable.php

public function initialize(array $config) { parent::initialize($config); ...

    $this->addBehavior('Translate', ['fields' => ['title']]);  

... }

//in controller - THIS WORKS!

public function add()
{

    I18n::locale('eng');
    $mainMenu = $this->MainMenus->newEntity();
    if ($this->request->is('post')) {     
        $mainMenu = $this->MainMenus->patchEntity($mainMenu, $this->request->data);
        $this->MainMenus->save($mainMenu)
    }
    $this->set(compact('mainMenu'));
}

// in controller BUT THIS DOES'T WORK:

public function add()
{

    I18n::locale('deu');
    $mainMenu = $this->MainMenus->newEntity();
    if ($this->request->is('post')) {     
        $mainMenu = $this->MainMenus->patchEntity($mainMenu, $this->request->data);
        $this->MainMenus->save($mainMenu)
    }
    $this->set(compact('mainMenu'));
}

I have the same problem when I read the record

//in controller - THIS WORKS!

    I18n::locale('eng');      
    $query = $this->MainMenus->find('all')->order(['MainMenus.id' => 'ASC'])->all();

// in controller BUT THIS DOES'T WORK:

    I18n::locale('deu');      
    $query = $this->MainMenus->find('all')->order(['MainMenus.id' => 'ASC'])->all();

For 'deu' I manually entered records. Do you know what the problem is? Thanks!

Upvotes: 2

Views: 924

Answers (1)

user3661042
user3661042

Reputation: 167

This is a solution to the problem from https://github.com/cakephp/cakephp/issues/8416:

The behavior assumes that you store the records in the default language. If the current locale is the same as the default language, then it will just return the records in the database instead of fetching from the translations table.

The title will not be saved in the i18n table for the default language, that is only done for other languages.

Upvotes: 3

Related Questions