CakePHP3 Tutorial

Next: Building for Mobile and Desktop Clients UP: TOC

What need to be done in order to make a CakePHP 3 App multilingual?

  1. Put all String in echo __('Hello');
  2. Adjust the routes configuration
  3. Extract all Strings to a .po file

code.....

Imbed all Strings in a Transations Function

Welcome

Adjust the routes configuartion

Router::scope('/', function ($routes) {
    $routes->connect('/', ['controller' => 'Objects', 'action' => 'index', 'lang' => 'eng']);
    $routes->connect('/:lang/:controller/:action/:id',[], ['id' => '[0-9]+','lang' => '[a-z]{3}','routeClass' => 'InflectedRoute'] );
    $routes->connect('/:lang/:controller/:action', [], ['lang' => '[a-z]{3}', 'routeClass' => 'InflectedRoute'] );
    $routes->connect('/:lang/:controller',[], ['lang' => '[a-z]{3}','routeClass' => 'InflectedRoute'] );

Extract all Strings

make sure that the directory .../src/Locale exists.

Export Strings to POT File

/usr/local/bin/php -d memory_limit=48M -f /usr/local/www/cakephp/bin/cake.php i18n extract \
  --app /usr/local/www/cakephp/src --output /usr/local/www/cakephp/src/Locale/ \
  --paths /usr/local/www/cakephp --merge yes --overwrite --extract-core no \
  --exclude test,vendor,debugKit > /usr/local/cakephp/logs/lang_pot.log

Load extrated Strings to a database table for translation

create sequence so_i18n_id_seq; CREATE TABLE i18n ( id integer primary key default nextval('so_i18n_id_seq'::regclass), locale varchar(6) NOT NULL, model varchar(255) NOT NULL, foreign_key integer NOT NULL, field varchar(255) NOT NULL, content text ); create unique index so_i18n_locale_field on i18n (locale, model, foreign_key, field); create index so_i18n_field on i18n (model, foreign_key, field); Next: Building for Mobile and Desktop Clients UP: TOC