ローカライズ前のコード
コントローラ
1 2 3 4 5 6 7 8 |
class BoardsController extends AppController { .... public function index() { $boards = $this->Board->find('all'); $this->set('boards', $boards); } .... } |
ビュー
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<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 __(‘テキスト’); ?> に書き直す。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<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を使って翻訳データを記述したリソースファイルを作成する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
> 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 が作成される。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# 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に翻訳したテキストを記述する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# 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を日本語に設定するとビュー内の静的テキストが日本語で表示されるようになる。
1 2 3 4 5 6 7 8 9 |
class BoardsController extends AppController { .... public function index() { Configure::write('Config.language', 'ja'); $boards = $this->Board->find('all'); $this->set('boards', $boards); } .... } |
表示結果
英語表示 (オリジナル)
日本語表示 (ローカライズ)