CakePHP - 国际化



像许多其他框架一样,CakePHP 也支持国际化。我们需要遵循以下步骤才能从单一语言切换到多种语言。

步骤 1

创建一个单独的 locales 目录 resources\locales

步骤 2

在 src\Locale 目录下为每种语言创建子目录。子目录的名称可以是语言的两位字母 ISO 代码或完整区域设置名称,例如 en_US、fr_FR 等。

步骤 3

在每个语言子目录下创建单独的 default.po 文件。此文件包含以 msgidmsgstr 形式的条目,如下面的程序所示。

msgid "msg"
msgstr "CakePHP Internationalization example."

这里,msgid 是将在视图模板文件中使用的键,msgstr 是存储翻译的值。

步骤 4

在视图模板文件中,我们可以使用上面的 msgid,如下所示,它将根据 locale 的设置值进行翻译。

<?php echo __('msg'); ?>

默认的 locale 可以通过以下行在 config/app.php 文件中设置。

'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_US')

要更改运行时的本地化,我们可以使用以下行。

use Cake\I18n\I18n;
I18n::locale('de_DE');

示例

按照以下程序所示对 config/routes.php 文件进行更改。

config/routes.php

<?php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$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'=>'Localizations','action'=>'index']);
   $builder->fallbacks();
});

src/Controller/LocalizationsController.php 创建一个 LocalizationsController.php 文件。将以下代码复制到控制器文件中。

src/Controller/LocalizationsController.php

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

在 resources\locales 创建一个 locales 目录。在 locales 目录下创建 3 个名为 en_US、fr_FR、de_DE 的目录。在每个目录下创建一个名为 default.po 的文件。将以下代码复制到相应的文件中。

resources/locales/en_US/default.po

msgid "msg"
msgstr "CakePHP Internationalization example."

resources/locales/fr_FR/default.po

msgid "msg"
msgstr "Exemple CakePHP internationalisation."

resources/locales/de_DE/default.po

msgid "msg"
msgstr "CakePHP Internationalisierung Beispiel."

src/Template 创建一个 Localizations 目录,并在该目录下创建一个名为 index.phpView 文件。将以下代码复制到该文件中。

src/Template/Localizations/index.php

<?php
   echo $this->Form->create(NULL,array('url'=>'/locale'));
   echo $this->Form->radio("locale",
      [
         ['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 __('msg'); ?>

通过访问以下 URL 执行以上示例。https:///cakephp4/locale

输出

执行后,您将收到以下输出。

English

电子邮件

CakePHP 提供 Email 类来管理与电子邮件相关的功能。要在任何控制器中使用电子邮件功能,我们首先需要通过编写以下行加载 Email 类。

use Cake\Mailer\Email;

Email 类提供了各种有用的方法,如下所述。

语法

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

参数
  • 包含电子邮件的字符串

  • 姓名

返回值

array|$this

描述

它指定了将从哪个电子邮件地址发送电子邮件

语法

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

参数
  • 包含电子邮件的字符串

  • 姓名

返回值

array|$this

描述

它指定了将向谁发送电子邮件

语法

Send(string|array|null $contentnull)

参数
  • 包含消息的字符串或包含消息的数组。

返回值 array
描述

使用指定的内容、模板和布局发送电子邮件

语法

Subject(string|null $subjectnull)

参数
  • 主题字符串

返回值

array|$this

描述

获取/设置主题

语法

Attachments(string|array|null $attachmentsnull)

参数
  • 包含文件名或包含文件名的数组的字符串

返回值

array|$this

描述

将附件添加到电子邮件消息

语法

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

参数
  • 包含电子邮件的字符串

  • 姓名

返回值

array|$this

描述

密件抄送

语法

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

参数
  • 包含电子邮件的字符串

  • 姓名

返回值

array|$this

描述

抄送

示例

按照以下程序所示对 config/routes.php 文件进行更改。

config/routes.php

<?php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$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();
});

src/Controller/EmailsController.php 创建一个 EmailsController.php 文件。将以下代码复制到控制器文件中。

src/Controller/EmailsController.php

<?php
   namespace App\Controller;
   use App\Controller\AppController;
   use Cake\Mailer\Email;
   class EmailsController extends AppController{
      public function index(){
         $email = new Email('default');
         $email->to('abc@gmail.com')
            ->subject('About')
            ->send('My message');
      }
   }
?>

src/Template 创建一个 Emails 目录,并在该目录下创建一个名为 index.php 的 View 文件。将以下代码复制到该文件中。

src/Template/Emails/index.php

Email Sent.

在发送任何电子邮件之前,我们需要对其进行配置。在下图中,您可以看到有两个传输,默认和 Gmail。我们使用了 Gmail 传输。

您需要将“GMAIL 用户名”替换为您的 Gmail 用户名,并将“应用密码”替换为您的应用程序密码。您需要在 Gmail 中打开 2 步验证并创建一个新的应用密码才能发送电子邮件。

config/app.php

Program App

通过访问以下 URL 执行以上示例 - https:///cakephp/email

输出

执行后,您将收到以下输出。

Documents Api
广告

© . All rights reserved.