端くれプログラマの備忘録 CakePHP [CakePHP] 静的テキストをローカライズする手順メモ

[CakePHP] 静的テキストをローカライズする手順メモ

ローカライズ前のコード

コントローラ

class BoardsController extends AppController {
    ....
    public function index() {
        $boards = $this->Board->find('all');
        $this->set('boards', $boards);
    }
    ....
}

ビュー

<h1>Boards</h1>
<table>
    <tr>
        <th>Name</th>
        <th>Title</th>
        <th>Content</th>
    </tr>
    <?php foreach ($boards as $board) { ?>
    <tr>
        <td><?php echo h($board['Board']['name']); ?></td>
        <td><?php echo h($board['Board']['title']); ?></td>
        <td><?php echo h($board['Board']['content']); ?></td>
    </tr>
    <?php } ?>
</table>

手順

ビュー中の翻訳対象テキストを <?php echo __(‘テキスト’); ?> に書き直す。

<h1><?php echo __('Boards'); ?></h1>
<table>
    <tr>
        <th><?php echo __('Name'); ?></th>
        <th><?php echo __('Title'); ?></th>
        <th><?php echo __('Content'); ?></th>
    </tr>
    <?php foreach ($boards as $board) { ?>
    <tr>
        <td><?php echo h($board['Board']['name']); ?></td>
        <td><?php echo h($board['Board']['title']); ?></td>
        <td><?php echo h($board['Board']['content']); ?></td>
    </tr>
    <?php } ?>
</table>

I18n Shellを使って翻訳データを記述したリソースファイルを作成する。

> cd c:\xampp\htdocs\cakephp\app
> Console\cake.bat i18n

Welcome to CakePHP v2.7.3 Console
---------------------------------------------------------------
App : app
Path: C:\xampp\htdocs\cakephp\app\
---------------------------------------------------------------
I18n Shell
---------------------------------------------------------------
[E]xtract POT file from sources
[I]nitialize i18n database table
[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
[C:\xampp\htdocs\cakephp\app\] >

Current paths: C:\xampp\htdocs\cakephp\app\
What is the path you would like to extract?
[Q]uit [D]one
[D] >

Would you like to extract the messages from the CakePHP core? (y/n)
[n] >
What is the path you would like to output?
[Q]uit
[C:\xampp\htdocs\cakephp\app\Locale] >

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


Extracting...
---------------------------------------------------------------
Paths:
   C:\xampp\htdocs\cakephp\app\
Output Directory: C:\xampp\htdocs\cakephp\app\Locale\
---------------------------------------------------------------

Done.
---------------------------------------------------------------
I18n Shell
---------------------------------------------------------------
[E]xtract POT file from sources
[I]nitialize i18n database table
[H]elp
[Q]uit
What would you like to do? (E/I/H/Q)
> q

すると、app/Locate/default.pot が作成される。

# LANGUAGE translation of CakePHP Application
# Copyright YEAR NAME <EMAIL@ADDRESS>
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\n"
"Last-Translator: NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <EMAIL@ADDRESS>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"

#: View/Boards/index.ctp:1
msgid "Boards"
msgstr ""

#: View/Boards/index.ctp:11
msgid "Name"
msgstr ""

#: View/Boards/index.ctp:12
msgid "Title"
msgstr ""

#: View/Boards/index.ctp:13
msgid "Content"
msgstr ""

app/Locate/jpn/LC_MESSAGESフォルダを作成してapp/Locate/default.potをdefault.poとしてコピーする。

そして、ファイルを編集してmsgstrに翻訳したテキストを記述する。

# LANGUAGE translation of CakePHP Application
# Copyright YEAR NAME <EMAIL@ADDRESS>
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\n"
"Last-Translator: NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <EMAIL@ADDRESS>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"

#: View/Boards/index.ctp:1
msgid "Boards"
msgstr "掲示板"

#: View/Boards/index.ctp:11
msgid "Name"
msgstr "名前"

#: View/Boards/index.ctp:12
msgid "Title"
msgstr "タイトル"

#: View/Boards/index.ctp:13
msgid "Content"
msgstr "内容"

コントローラでConfig.languageを日本語に設定するとビュー内の静的テキストが日本語で表示されるようになる。

class BoardsController extends AppController {
    ....
    public function index() {
        Configure::write('Config.language', 'ja');
        $boards = $this->Board->find('all');
        $this->set('boards', $boards);
    }
    ....
}

表示結果

英語表示 (オリジナル)

boards-eng

日本語表示 (ローカライズ)

boards-jpn