English 中文(简体)
CakePHP - Internationalization
  • 时间:2024-12-22

CakePHP - Internationapzation


Previous Page Next Page  

Like many other frameworks, CakePHP also supports Internationapzation. We need to follow these steps to go from single language to multiple language.

Step 1

Create a separate locales directory resourceslocales.

Step 2

Create subdirectory for each language, under the directory srcLocale. The name of the subdirectory can be two letter ISO code of the language or full locale name pke en_US, fr_FR etc.

Step 3

Create separate default.po file under each language subdirectory. This file contains entry in the form of msgid and msgstr, as shown in the following program.

msgid "msg"
msgstr "CakePHP Internationapzation example."

Here, the msgid is the key which will be used in the View template file and msgstr is the value which stores the translation.

Step 4

In the View template file, we can use the above msgid, as shown below which will be translated based on the set value of locale.

<?php echo __( msg ); ?>

The default locale can be set in the config/app.php file by the following pne.

 defaultLocale  => env( APP_DEFAULT_LOCALE ,  en_US )

To change the local at runtime, we can use the following pnes.

use CakeI18nI18n;
I18n::locale( de_DE );

Example

Make changes in the config/routes.php file as shown in the following program.

config/routes.php

<?php
use CakeHttpMiddlewareCsrfProtectionMiddleware;
use CakeRoutingRouteDashedRoute;
use CakeRoutingRouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope( / , function (RouteBuilder $builder) {
   $builder->registerMiddleware( csrf , new CsrfProtectionMiddleware([
       httpOnly  => true,
   ]));
   $builder->applyMiddleware( csrf );
   //$builder->connect( /pages ,
      [ controller => Pages , action => display ,  home ]);
   $builder->connect( locale ,
      [ controller => Locapzations , action => index ]);
   $builder->fallbacks();
});

Create a LocapzationsController.php file at src/Controller/LocapzationsController.php. Copy the following code in the controller file.

src/Controller/LocapzationsController.php

<?php
   namespace AppController;
   use AppControllerAppController;
   use CakeI18nI18n;
   class LocapzationsController extends AppController {
      pubpc function index() {
         if($this->request->is( post )) {
            $locale = $this->request->getData( locale );
            I18n::setLocale($locale);
         }
      }
   }
?>

Create a locales directory at resourceslocales. Create 3 directories called en_US, fr_FR, de_DE under the locales directory. Create a file under each directory called default.po. Copy the following code in the respective file.

resources/locales/en_US/default.po

msgid "msg"
msgstr "CakePHP Internationapzation example."

resources/locales/fr_FR/default.po

msgid "msg"
msgstr "Exemple CakePHP internationapsation."

resources/locales/de_DE/default.po

msgid "msg"
msgstr "CakePHP Internationapsierung Beispiel."

Create a directory Locapzations at src/Template and under that directory, create a View file called index.php. Copy the following code in that file.

src/Template/Locapzations/index.php

<?php
   echo $this->Form->create(NULL,array( url => /locale ));
   echo $this->Form->radio("locale",
      [
         [ value => en_US , text => Engpsh ],
         [ value => de_DE , text => German ],
         [ value => fr_FR , text => French ],
      ]
   );
   echo $this->Form->button( Change Language );
   echo $this->Form->end();
?>
<?php echo __( msg ); ?>

Execute the above example by visiting the following URL. http://localhost/cakephp4/locale

Output

Upon execution, you will receive the following output.

Engpsh

Email

CakePHP provides Email class to manage email related functionapties. To use email functionapty in any controller, we first need to load the Email class by writing the following pne.

use CakeMailerEmail;

The Email class provides various useful methods which are described below.

Syntax

From(string|array|null $email null, string|null $name null )

Parameters

    String with email

    Name

Returns

array|$this

Description

It specifies from which email address; the email will be sent

Syntax

To(string|array|null $emailnull, string|null $namenull)

Parameters

    String with email

    Name

Returns

array|$this

Description

It specifies to whom the email will be sent

Syntax

Send(string|array|null $contentnull)

Parameters

    String with message or array with messages.

Returns array
Description

Send an email using the specified content, template and layout

Syntax

Subject(string|null $subjectnull)

Parameters

    Subject string

Returns

array|$this

Description

Get/Set Subject

Syntax

Attachments(string|array|null $attachmentsnull)

Parameters

    String with the filename or array with filenames

Returns

array|$this

Description

Add attachments to the email message

Syntax

Bcc(string|array|null $emailnull, string|null $namenull)

Parameters

    String with email

    Name

Returns

array|$this

Description

Bcc

Syntax

cc( string|array|null $emailnull , string|null $namenull )

Parameters

    String with email

    Name

Returns

array|$this

Description

Cc

Example

Make changes in the config/routes.php file as shown in the following program.

config/routes.php

<?php
use CakeHttpMiddlewareCsrfProtectionMiddleware;
use CakeRoutingRouteDashedRoute;
use CakeRoutingRouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope( / , function (RouteBuilder $builder) {
   $builder->registerMiddleware( csrf , new CsrfProtectionMiddleware([
       httpOnly  => true,
   ]));
   $builder->applyMiddleware( csrf );
   //$builder->connect( /pages ,[ controller => Pages , action => display ,  home ]);
   $builder->connect( /email ,[ controller => Emails , action => index ]);
   $builder->fallbacks();
});

Create an EmailsController.php file at src/Controller/EmailsController.php. Copy the following code in the controller file.

src/Controller/EmailsController.php

<?php
   namespace AppController;
   use AppControllerAppController;
   use CakeMailerEmail;
   class EmailsController extends AppController{
      pubpc function index(){
         $email = new Email( default );
         $email->to( abc@gmail.com )
            ->subject( About )
            ->send( My message );
      }
   }
?>

Create a directory Emails at src/Template and under that directory, create a View file called index.php. Copy the following code in that file.

src/Template/Emails/index.php

Email Sent.

Before we send any email, we need to configure it. In the below screenshot, you can see that there are two transports, default and Gmail. We have used Gmail transport.

You need to replace the “GMAIL USERNAME” with your Gmail username and “APP PASSWORD” with your apppcations password. You need to turn on 2-step verification in Gmail and create a new APP password to send email.

config/app.php

Program App

Execute the above example by visiting the following URL − http://localhost/cakephp/email

Output

Upon execution, you will receive the following output.

Documents Api Advertisements