他の多くのフレームワークと同様、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); } } } ?>
resourceslocales に locales ディレクトリを作成します。 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 は、電子メール関連の機能を管理するための Email クラスを提供します。コントローラーで電子メール機能を使用するには、まず次の行を記述して電子メール クラスをロードする必要があります。
use Cake\Mailer\Email;
Email クラスは、以下で説明するさまざまな便利なメソッドを提供します。
Syntax | From(string|array|null $email null, string|null $name null ) |
---|---|
Parameters |
|
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 |
|
Returns | array |
Description | Send an email using the specified content, template and layout |
Syntax | Subject(string|null $subjectnull) |
---|---|
Parameters |
|
Returns | array|$this |
Description | Get/Set Subject |
- 電子メールを含む文字列
- 名前
- メッセージを含む文字列、またはメッセージを含む配列。
- 件名の文字列
Syntax | Attachments(string|array|null $attachmentsnull) |
---|---|
Parameters |
|
Returns | array|$this |
Description | Add attachments to the email message |
Syntax | Bcc(string|array|null $emailnull, string|null $namenull) |
---|---|
Parameters |
|
Returns | array|$this |
Description | Bcc |
Syntax | cc( string|array|null $emailnull , string|null $namenull ) |
---|---|
Parameters |
|
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

Execute the above example by visiting the following URL − http://localhost/cakephp/email
Output
Upon execution, you will receive the following output.

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

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

SublimeText3 中国語版
中国語版、とても使いやすい

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)
