Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Internationalization in Cakephp 3.x

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3
    • 0
    • 2.12k
    Comment on it

    Internationalization in Cakephp 3.x

    Hello friends, welcome to findnerd. Today I am going to tell you how to implement Internationalization in Cakephp 3. It is similar to multilingual. In my previous blog I have mentioned how to develop multilingual website in Cakephp 3 using database. You can follow the blog by clicking on this link . Here we don't need to create any tables for storing translations. Just follow few steps below to go internationalization.


    Step 1: Create new folder Locale in src i.e. src\Locale.

    Step 2: Now under directory src\Locale create a sub-directory for each languages. Example: fr_Fr, en_US, de_DE.

    Step 3: Create default.po under each language directory i.e. fr_Fr, en_US, de_DE

    Have a look on the directory structure as shown below:



    Step 4: Write the following lines in fr_Fr/default.po.


    msgid "A ball"
    msgstr "Un ballon"

    Here msgid is the original message which you want to translate. Whereas msgstr is the translated text in French. This message will be used in the view template as shown below:


    <?php echo __('A ball'); ?>


    In order to internationalize the code, you will need to wrap strings in:

    __() ==> __('A ball');

    If the translation is available for the given text 'A ball' , then this function ' __() ' will translate the string.


    Step 5: Set the default locale from config/app.php . You can change it by setting this variable 'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_US')


    'App' => [
            'namespace' => 'App',
            'encoding' => env('APP_ENCODING', 'UTF-8'),
            'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_US'), // default locale is en_US
            'base' => false,
            'dir' => 'src',
            'webroot' => 'webroot',
            'wwwRoot' => WWW_ROOT,
            // 'baseUrl' => env('SCRIPT_NAME'),
            'fullBaseUrl' => false,
            'imageBaseUrl' => 'img/',
            'cssBaseUrl' => 'css/',
            'jsBaseUrl' => 'js/',
            'paths' => [
                'plugins' => [ROOT . DS . 'plugins' . DS],
                'templates' => [APP . 'Template' . DS],
                'locales' => [APP . 'Locale' . DS],



    Also you can change the default locale at runtime . Write the following code to implement it:


    use Cake\I18n\I18n;


    Step 6: Create InternationalizationsController.php at src/Controller/InternationalizationsController.php . Write the following code:


       namespace App\Controller;
       use App\Controller\AppController;
       use Cake\I18n\I18n;
       class InternationalizationsController extends AppController{
          public function index(){
                $locale = $this->request->data('international');


    Step 7: Copy following lines and paste them in the default.po under following directory:




    msgid "A ball"
    msgstr "Ein Ball"




    msgid "A ball"
    msgstr "A ball"


    src/Locale/fr_FR (Note: in Step 4 we have made this file) 


    msgid "A ball"
    msgstr "Un ballon"


    Step 8: Create new folder with name Internationalizations at src/Template/

    Step 9: Create index.ctp file at src/Template/Internationalizations/

    Step 10: Copy and paste the following lines of code at src/Template/Internationalizations/index.ctp


       echo $this->Form->create("Internationalization",array('url'=>'/international'));
       echo $this->Form->radio("international",[
       echo $this->Form->button('Change Language');
       echo $this->Form->end();
    <?php echo __('A ball'); ?>



    Step 11: Change the routing at config/routes.php 




    Step 12: Access the following url on browser  http://localhost/cakephp3/international . You will see the following output:



    Select German option and click on "Change Language" button and you will see that text 'A ball' has been translated to 'Ein Ball' as shown below:



    All done!


    Thanks for reading the blog.

    Internationalization in Cakephp 3.x

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: