How to use Language Translation?


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
    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

echo $this->Category->translation(‘es’)->name;

  1. but when executed this,following error comes.

Unknown method “translation”


->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);
//and in view:
echo $category->translation('es')->name;