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.