ホームページ  >  記事  >  バックエンド開発  >  CakePHP の国際化

CakePHP の国際化

PHPz
PHPzオリジナル
2024-09-10 17:26:30272ブラウズ

他の多くのフレームワークと同様、CakePHP も国際化をサポートしています。単一言語から複数言語に移行するには、次の手順に従う必要があります。

ステップ 1

別のロケール ディレクトリ リソースを作成しますlocales.

ステップ 2

ディレクトリ srcLocale の下に、言語ごとにサブディレクトリを作成します。サブディレクトリの名前は、言語の 2 文字の ISO コード、または en_US、fr_FR などの完全なロケール名にすることができます。

ステップ 3

各言語サブディレクトリの下に個別の default.po ファイルを作成します。このファイルには、次のプログラムに示すように、msgid および msgstr の形式のエントリが含まれています。

msgid "msg"
msgstr "CakePHP Internationalization example."

ここで、msgid はビュー テンプレート ファイルで使用されるキーであり、msgstr は翻訳を保存する値です。

ステップ 4

View テンプレート ファイルでは、以下に示すように、上記の msgid を使用できます。これはロケールの設定値に基づいて翻訳されます。

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

デフォルトのロケールは、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);
         }
      }
   }
?>

resourceslocaleslocales ディレクトリを作成します。 locales ディレクトリの下に、en_US、fr_FR、de_DE という 3 つのディレクトリを作成します。各ディレクトリの下に 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.php. という名前の View ファイルを作成します。そのファイル内の次のコード。

src/Template/Localizations/index.php

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

次の URL にアクセスして、上記の例を実行します。 http://localhost/cakephp4/locale

出力

実行すると、次の出力が表示されます。

CakePHP の国際化

メール

CakePHP は、電子メール関連の機能を管理するための Email クラスを提供します。コントローラーで電子メール機能を使用するには、まず次の行を記述して電子メール クラスをロードする必要があります。

use Cake\Mailer\Email;

Email クラスは、以下で説明するさまざまな便利なメソッドを提供します。

構文
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

From(string|array|null $email null, string|null $name null ) パラメータ
  • 電子メールを含む文字列
    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

配列|$this 説明
送信元の電子メール アドレスを指定します。メールが送信されます
Syntax

Subject(string|null $subjectnull)

Parameters
  • Subject string

Returns

array|$this

Description

Get/Set Subject

構文 To(string|array|null $emailnull, string|null $namenull) パラメータ
  • 電子メールを含む文字列
  • 名前
返品 配列|$this 説明 メールの送信先を指定します 構文 Send(string|array|null $contentnull) パラメータ
  • メッセージを含む文字列、またはメッセージを含む配列。
返品 配列 説明 指定されたコンテンツ、テンプレート、レイアウトを使用してメールを送信します 構文 件名(文字列|null $subjectnull) パラメータ
  • 件名の文字列
返品 配列|$this 説明 件名の取得/設定 テーブル>
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 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();
});

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

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');
      }
   }
?>

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 applications 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

以上がCakePHP の国際化の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:CakePHP フォーム処理次の記事:CakePHP フォーム処理

関連記事

続きを見る