CakePHP 国际化

cakephp 国际化

 

像许多其他框架一样,cakephp 也支持国际化。我们需要按照这些步骤从单一语言到多语言。

 

步骤 1

创建一个单独的语言环境目录资源\ 语言环境

 

步骤 2

在目录 src\locale 下为每种语言创建子目录。子目录的名称可以是语言的两个字母 iso 代码或完整的语言环境名称,如 en_us、fr_fr 等。

 

步骤 3

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

msgid "msg"
msgstr "cakephp internationalization example."

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

 

步骤 4

在查看模板文件,我们可以使用上面的 msgid,如下图,会根据locale的设置值进行翻译。

 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

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

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

在资源\ 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.php 的 view 文件。 在该文件中复制以下代码。

src/template/localizations/index.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();
?>
 echo __('msg'); 

通过访问以下 url 执行上述示例。 http://localhost/cakephp4/locale

 

输出

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

 

电子邮件

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

use cake\mailer\email;

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

语法

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

参数
  • 带电子邮件的字符串
  • 姓名
  • 返回

    数组|$this

    描述

    它指定来自哪个电子邮件地址;电子邮件将被发送

    语法

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

    参数
  • 带电子邮件的字符串
  • 姓名
  • 返回

    数组|$this

    描述

    它指定电子邮件将发送给谁

    语法

    发送(string|array|null $contentnull)

    参数
  • 带有消息的字符串或带有消息的数组。
  • 退货 数组
    描述

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

    语法

    subject(string|null $subjectnull)

    参数
  • 主题字符串
  • 返回

    数组|$this

    说明

    获取/设置主题

    语法

    attachments(string|array|null $attachmentsnull)

    参数
  • 带有文件名的字符串或带有文件名的数组
  • 退货

    数组|$this

    说明

    在电子邮件中添加附件

    语法

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

    参数
  • 带电子邮件的字符串
  • 姓名
  • 退货

    数组|$this

    说明

    密件抄送

    语法

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

    参数
  • 带电子邮件的字符串
  • 姓名
  • 退货

    数组|$this

    说明

    抄送

     

    示例

    在 config/routes.php 文件中进行更改,如以下程序所示。

    config/routes.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

       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 的视图文件。 复制以下代码在那个文件中。

    src/template/emails/index.php

    email sent.

    在发送任何电子邮件之前,我们需要对其进行配置。在下面的屏幕截图中,您可以看到有两种传输方式,default 和 gmail。我们使用了 gmail 传输。

    您需要将"gmail username"替换为您的 gmail 用户名,将"app password"替换为您的应用程序密码。您需要在 gmail 中开启两步验证并创建新的 app 密码才能发送电子邮件。

    config/app.php

    通过访问以下 url 执行上述示例-http://localhost/cakephp/email

     

    输出

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

    下一节:cakephp 会话管理

    cakephp 教程

    相关文章