- 多言語対応したいメッセージをYii:t()で囲む
public static function t($category,$message,$params=array(),$source=null)
$category毎にファイルで分けられる。実際の文字は$message。
- protected/messages/config.phpを作成する
<?php /** * This is the configuration for generating message translations * for the Yii framework. It is used by the 'yiic message' command. */ return array( 'sourcePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', 'messagePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'messages', 'languages'=>array('ja_jp'), 'fileTypes'=>array('php'), 'exclude'=>array( '.svn', 'yiilite.php', '/i18n/data', '/messages', '/vendors', '/web/js', ), );
(元ファイルは本体に入っています)
- 以下のコマンドを実行
protected/yiic message protected/messages/config.php
- protected/config/main.phpにlanguageを指定
return array( ... 'language'=>'ja', ...
- あとは手動で修正してもいいし再生成してもいいしみたいな
ちなみに
protected/config/main.phpで設定している配列はCApplicationのメンバ変数と一致しているようです。
あとCGettextMessageSourceとかCDbMessageSourceとかもあるようなので、そっちから取ることもできそうです。
validationのエラーメッセージとかの変更方法
ちなみにこのファイルは読み込まれているわけではないので、このファイルを編集しても反映はされない。
- protected/config/main.phpに以下の設定をする
return array( ... 'components'=>array( ... 'coreMessages'=>array( 'basePath'=>null, ),
ちなみにこれを書かないとフレームワークフォルダ側のmessagesフォルダを読みに行く。それでもいいならこの設定は不要。
手っ取り早いのは、フレームワーク側のzh/yii.phpをen_us/yii.phpにコピーして対訳を書く。
途中で言語切り替え
Yii::app()->setLanguage('en_us');