検索

CakePHP の国際化

Sep 10, 2024 pm 05:26 PM
phpcakephpPHP framework

他の多くのフレームワークと同様、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 までご連絡ください。
PHPメール:ステップバイステップ送信ガイドPHPメール:ステップバイステップ送信ガイドMay 09, 2025 am 12:14 AM

PhpisusedForsedingEmailsDueToitsIttegration withServerMailServicesAndExternalSmtpproviders、自動化とMarketingCampaign.1)SetupYourphpenvironment withebeBironment witheBiserverandphp、保証

PHP経由で電子メールを送信する方法:例とコードPHP経由で電子メールを送信する方法:例とコードMay 09, 2025 am 12:13 AM

メールを送信する最良の方法は、PHPMailerライブラリを使用することです。 1)Mail()関数を使用することはシンプルですが信頼できないため、電子メールがスパムを入力するか、配信できない場合があります。 2)PHPMailerは、より良い制御と信頼性を提供し、HTMLメール、添付ファイル、SMTP認証をサポートします。 3)SMTP設定が正しく構成されていることを確認し、暗号化(StartTLSやSSL/TLSなど)を使用してセキュリティを強化します。 4)大量の電子メールについては、メールキューシステムを使用してパフォーマンスを最適化することを検討してください。

高度なPHPメール:カスタムヘッダーと機能高度なPHPメール:カスタムヘッダーと機能May 09, 2025 am 12:13 AM

customedersandaddadvancedfeaturesinphpemailentalitylivainability.1)customederadddetadata fortrackingandcategorization.2)htmLemailsallowStingtintintintintintinteractivity.3)添付物質の添付物質の添付

php&smtpでメールを送信するためのガイドphp&smtpでメールを送信するためのガイドMay 09, 2025 am 12:06 AM

PHPとSMTPを使用してメールを送信することは、PHPMailerライブラリを介して実現できます。 1)PHPMailerをインストールして構成する、2)SMTPサーバーの詳細を設定する、3)電子メールコンテンツを定義し、4)メールを送信してエラーを処理します。この方法を使用して、電子メールの信頼性とセキュリティを確保します。

PHPを使用して電子メールを送信する最良の方法は何ですか?PHPを使用して電子メールを送信する最良の方法は何ですか?May 08, 2025 am 12:21 AM

BestappRoachforseminginphpisusingthephpmailerlibrarydueToitsReliability、featurrichness、andeaseofuse.phpmailerSupportssmtpは、detairederorhandlingを提供します

PHPでの依存関係注射のベストプラクティスPHPでの依存関係注射のベストプラクティスMay 08, 2025 am 12:21 AM

依存関係注射(DI)を使用する理由は、コードのゆるい結合、テスト可能性、および保守性を促進するためです。 1)コンストラクターを使用して依存関係を注入します。2)サービスロケーターの使用を避け、3)依存関係噴射コンテナを使用して依存関係を管理する、4)依存関係を注入することでテスト可能性を向上させる、5)注入依存性を回避、6)パフォーマンスに対するDIの影響を考慮します。

PHPパフォーマンスのチューニングのヒントとコツPHPパフォーマンスのチューニングのヒントとコツMay 08, 2025 am 12:20 AM

phpperformancetuningisucial cuseenhancess andandandadsand。

PHP電子メールセキュリティ:電子メールを送信するためのベストプラクティスPHP電子メールセキュリティ:電子メールを送信するためのベストプラクティスMay 08, 2025 am 12:16 AM

bestpracticesforsendingemails securlyinphpinclude:1)sutureconsmttarttlsencryptionとの使用の使用、2)検証およびサンシジン化のinputStopReventinjectuctacks、3)adinitivedinitivedInemailsopenslsl、4)adlinglinglingemailoaに

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

mPDF

mPDF

mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境