CakePHP | How to implement Internationalization in CakePHP 3.X

To implement Internationalization, CakePHP will pick all your strings which start with “__(“ double underscore ) and generate a default file for you.

For Example: one of the strings defined in our project is as below:

echo __ ( ' Gift Cards Not available' );

Replace all your normal string with (__), to automatically pick them using extract plugin.

Steps to implement Internationalization in CakePHP 3.0 or above versions:

Go to root directory of your project.
Run command: bin/cake i18n extract

You will see following output:

Welcome to CakePHP v3.2.7 Console
—————————————————————
App: src
Path: /var/www/html/myfolder/src/
PHP: 5.5.9-1ubuntu4.14
—————————————————————
Current paths: None
What is the path you would like to extract?
[Q]uit [D]one
[/var/www/html/myfolder/src/] >

If you want to generate language files for all strings inside src folder of your project, press enter. It will accept the path.Then it will show:

Current paths: /var/www/html/myfolder/src/

What is the path you would like to extract?

[Q]uit [D]one
[D] > D

If there are any more path, type your path and press enter.

For Example:

Current paths: /var/www/html/myfolder/src/

What is the path you would like to extract?

[Q]uit [D]one
[D] > /var/www/html/myfolder/src/Controller/Component

Current paths : /var/www/html/myfolder/src/, /var/www/html/myfolder/src/Controller/Component

As you can see above, two paths are there.
You can specify as many as you want. It will pick all strings inside specified folders.

When done, Press D.

Would you like to extract the messages from the CakePHP core? (y/n)
If you want to include all the strings defined in cake php core press yes otherwise no.

What is the path you would like to output?

[Q]uit[/var/www/html/myfolder/src/Locale] >

You can specify output directory for you language files. By default it will take /var/www/html/myfolder/src/Locale (Locale ) directory as default.
Press enter.

Would you like to merge all domain strings into the default.pot file?
Press y.

Extracting…
—————————————————————
Paths:
/var/www/html/myfolder/src/
/var/www/html/myfolder/src/Controller/Component
Output Directory: /var/www/html/myfolder/src/Locale/
—————————————————————
==========================================================================> 100%

Error: default.pot already exists in this location. Overwrite? [Y]es, [N]o, [A]ll (y/n/a)

[y] >

It will generate default.pot file. If it is already there, it will ask for overwrite.
You can choose (a) option if you want to append the new contents to the existing file. Choose accordingly.

You will get Done message if everything runs fine.

Now refresh the project and you will see a new Locale directory inside your src folder.

Fig1: default.pot file (Eclipse)

default.pot is just a reference file containing string in below format:

#: Controller/TestController.php:27;28
msgid "Gift Cards Not available"
msgstr ""

To generate language specific file: Create a folder inside Locale directory, for english language folder name will be en_US. Create default.po file and replace each msgstr with the string you want to replace.

Is msgstr is “” as in default file it will show the msgid.
If msgstr is available, it will replace the string inside msgid with msgstr string value.

Leave a Reply

Your email address will not be published. Required fields are marked *