Hello for me the best way:
initialization of the i18n database table
CREATE TABLE i18n ( id int NOT NULL auto_increment, locale varchar(6) NOT NULL, model varchar(255) NOT NULL, foreign_key int(10) NOT NULL, field varchar(255) NOT NULL, content text, PRIMARY KEY (id), UNIQUE INDEX I18N_LOCALE_FIELD(locale, model, foreign_key, field), INDEX I18N_FIELD(model, foreign_key, field) );
Attaching translation behavior to your tables
class ArticlesTable extends Table { public function initialize(array $config) { $this->addBehavior('Translate', ['fields' => ['title', 'body']]); } }
The first thing to note is that you need to pass the field key in the configuration array. This list of fields is required to indicate which columns will be able to store translations.
use Cake\ORM\Behavior\Translate\TranslateTrait; use Cake\ORM\Entity; class Article extends Entity { use TranslateTrait; }
Saving multiple translations
$translations = [ 'en_US' => ['title' => "An article"], 'fr_FR' => ['title' => "Un article"] ]; foreach ($translations as $lang => $data) { $article->translation($lang)->set($data, ['guard' => false]); } $articles->save($article);
Reading translated content
I18n::locale('en_US'); $articles = TableRegistry::get('Articles'); // All entities in results will contain english translation $results = $articles->find()->all();
CakePHP: Translate
CakePHP: Internationalization and Localization