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.14k
    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  http://findnerd.com/account/#url=/list/view/How-to-use-multilingual-in-cakephp-3-x-/27525/ . 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;
    I18n::locale('de_DE');

     

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

     

    <?php
       namespace App\Controller;
       use App\Controller\AppController;
       use Cake\I18n\I18n;
    
       class InternationalizationsController extends AppController{
          public function index(){
             if($this->request->is('post')){
                $locale = $this->request->data('international');
                I18n::locale($locale);
             }
          }
       }
    ?>

     

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

     

    src/Locale/de_DE

     

    msgid "A ball"
    msgstr "Ein Ball"

     

    src/Locale/en_US

     

    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

     

    <?php
       echo $this->Form->create("Internationalization",array('url'=>'/international'));
       echo $this->Form->radio("international",[
          ['value'=>'en_US','text'=>'English'],
          ['value'=>'de_DE','text'=>'German'],
          ['value'=>'fr_FR','text'=>'French'],
       ]);
       echo $this->Form->button('Change Language');
       echo $this->Form->end();
    ?>
    <?php echo __('A ball'); ?>

     

     

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

     

    $routes->connect('international',['controller'=>'Internationalizations','action'=>'index']);

     

    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
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: