How to use Language Translation?


#1

currently I am studying cake php .
i am doing language translation tutorial.
so I want to load translation from database.
I am doing following steps

  1. created table
    i18n
    2)created entity
<?php use Cake\ORM\Behavior\Translate\TranslateTrait; use Cake\ORM\Entity; class Category extends Entity { use TranslateTrait; } ?>

3)created table

<?php namespace App\Model\Table; use Cake\ORM\Table; use Cake\ORM\Behavior\Translate\TranslateTrait; class Category extends Table { public $useTable = 'Category'; public function initialize(array $config) { $this->addBehavior('Translate', ['fields' => ['name']]); } } ?>
  1. printed translation value in controller

I18n::locale(‘es’);
$this->loadModel(‘Category’);
echo $this->Category->translation(‘es’)->name;

  1. but when executed this,following error comes.

Unknown method “translation”


#2

->translations is method in entity, and you are trying to use is on Table object, you need to get any record before you can translate it ie

//in controller:
$category = $this->Category->get($id);
$this->set(compact($category));
//and in view:
echo $category->translation('es')->name;