CakeFp 3.x - Full Page Internationalization

I work with cakephp 3.x and I would like to translate my site into several languages. I read the internationalization documentation ( http://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html ) for cakephp 3.x, and only explains how to translate words and sentences, but not full page

What is the best solution to translate a full page of text? I think that you need to have a page for each language and in the beforeFilter function, select the correct page. This is a good decision?

I also ask how to have a translation of a paragraph that is stored in the database, for example, my news page is stored in the database, and I would like to have a version of my news in a different language. Should I add a row for each language in my database or is there a better solution?

thank you for your time

Dani

+4
source share
2 answers

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

+3
source

Internationalization and localization has two main tasks: in both cases, it is assumed that the only thing you need to do at run time is to change the language of the I18n :: locale call , for example:

 use Cake\I18n\I18n; ... public function beforeFilter() { if (should show german) { I18n::locale('de_DE'); } } 

After setup, there are no additional steps to create a multilingual site. It is imperative to set the default locale in the bootstrap file as this is what is used if the locale is not overridden at run time.

Static content

think that you need to make a page for each language and in the beforeFilter function, select the correct page. This is a good decision?

Not.

Static files, if you do not intend to have fundamentally different page layouts in one language or the like, usually represent a single template using translate functions and po files.

From the docs:

The following is sample code for a monolingual application:

 <h2>Popular Articles</h2> 

To internationalize your code, all you have to do is wrap the lines in __ () like this:

 <h2><?= __('Popular Articles') ?></h2> 

There are several steps (see the docs), but using the double underline method with the default text for your text prepares your application for use with multiple languages.

Dynamic content

Database content is usually translated using translation behavior .

In this mode, all translated content for all tables using behavior is saved in a separate table. At run time, translations are automatically retrieved. To illustrate:

 $articles = TableRegistry::get('Articles'); I18n::locale('eng'); $article = $articles->get(12); echo $article->title; // Echoes 'A title' I18n::locale('spa'); $article = $articles->get(12); echo $article->title; // Echoes 'Un titulo' 

Putting it all together

Using both static translations and translation behavior, your template files will look something like this:

 <h1><?= h($article->title) ?></h1> <?= $article->body ?> <p><?= __('Did you like that article? Let us know') ?></p> 

Please note that the appart from the double underscore method is exactly the same as for a single language application.

+3
source

All Articles