4
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CakePHP3:メッセージを日本語化する

Last updated at Posted at 2017-01-16

分かってしまえば至って簡単なのですが、ちょっとハマったので以下に記載します。

辞書ファイルの場所について

辞書ファイルの場所は自分で作ります。場所はapp/src/Locale/ja_JPです。パーミッションが正しいことを確認ください。apacheを使っているならば、apacheユーザがフォルダに入れること。

cd app/src/Locale
mkdir ja_JP
chmod (apacheユーザがアクセスできる権限)default.po

日本語を有効化する

デフォルトを変更する場合

日本語対応する場合はapp.phpをいじります:

app/config/app.php
 'App' => [
         'namespace' => 'App',
         'encoding' => env('APP_ENCODING', 'UTF-8'),
-        'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_US'),
+        'defaultLocale' => env('APP_DEFAULT_LOCALE', 'ja_JP'),

デフォルトを維持し、コントローラ側で指定する場合

app/src/Controller/AppController.php
+ use Cake\I18n\I18n

 public function initialize()
 {
+   I18n::locale('ja_JP');
    parent::initialize();

とします

辞書ファイルの生成

cd app/bin
./cake i18n

[E]xtract POT file from sources
[I]nitialize a language from POT file
[H]elp
[Q]uit
What would you like to do? (E/I/H/Q)
> E

Current paths: None
What is the path you would like to extract?
[Q]uit [D]one
[/var/www/html/app/src/] > (改行)

Current paths: /var/www/html/app/src/
What is the path you would like to extract?
[Q]uit [D]one
[D] > D

Would you like to extract the messages from the CakePHP core? (y/n)
[n] > y

What is the path you would like to output?
[Q]uit
[/var/www/html/app/src/Locale] >(改行)

Would you like to merge all domain strings into the default.pot file? (y/n)
[n] > y

Extracting...
---------------------------------------------------------------
Paths:
   /var/www/html/app/src/
   /var/www/html/app/vendor/cakephp/cakephp/src/
Output Directory: /var/www/html/app/src/Locale/
---------------------------------------------------------------
==========================================================================> 100%
Done.
---------------------------------------------------------------
I18n Shell
---------------------------------------------------------------
[E]xtract POT file from sources
[I]nitialize a language from POT file
[H]elp
[Q]uit
What would you like to do? (E/I/H/Q)
> Q

これで、default.potファイルがLocale配下に作成されるので、ja_JPディレクトリに移動&default.poにリネームしてください。そして、default.poのパーミッションを必要に応じて修正します:

cd ja_JP
chmod (apacheユーザがアクセスできる権限)default.po

poファイルを更新しても反映されない場合

app/tmp/cache/persistentディレクトリの中に_translations_といった名前のファイルがあるので、削除してみてください

4
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?