How to use TranslateBehavior in CakePHP?

There is no documentation on cakephp.org, and I cannot find it on google. Please write me some documentation or put it!

+4
source share
2 answers

Translation behavior is another very useful but poorly documented feature of CakePHP. I have implemented it several times with reasonable success on multilingual websites in the following lines.

First, the translation behavior will internationalize the content database of your site. If you have more static content, you need to look at the Cake __('string') cover function and gettext (there is useful information about it here )

Assuming the content we want to translate is with the following db table:

 CREATE TABLE `contents` ( `id` int(11) unsigned NOT NULL auto_increment, `title` varchar(255) default NULL, `body` text, PRIMARY KEY (`id`), ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

Then the content.php model has:

 var $actsAs = array('Translate' => array('title' => 'titleTranslation', 'body' => 'bodyTranslation' )); 

in its definition. Then you need to add the i18n table to the database:

 CREATE TABLE `i18n` ( `id` int(10) 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` mediumtext, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

Then, when you save the data in the database of your controller, set the language standard to the desired language (this example will be for Polish):

 $this->Content->locale = 'pol'; $result = $this->Content->save($this->data); 

This will create entries in table i18n for the header and body fields for the pol locale. Finds will be found based on the current locale set in the user browser, returning an array, for example:

 [Content] [id] [titleTranslation] [bodyTranslation] 

We use the excellent p28n component to implement a language switching solution that works fine with gettext and translates behavior.

This is not an ideal system - since it creates HABTM relationships on the fly, it can cause some problems with other relationships that you could create manually, but if you are careful, this may work well.

+13
source

For those looking for the same, cakephp has updated its documentation. For Translate Behavior go here ..

0
source

Source: https://habr.com/ru/post/1276855/


All Articles