When you develop an international site using CPhpMessageSource, it’s offen to copy and paste messages from view into message files.
To cut off this step, do following:
- Create file protected/components/MessageBehavior.php:
<?php
class MessageBehavior extends CBehavior {
public function events() {
//we don't want to decrease the performance of server
if (YII_DEBUG) {
return array(
'onMissingTranslation'=>'missingTranslation',
);
}
return array();
}
public function missingTranslation($event) {
$file = $event->sender->basePath.DIRECTORY_SEPARATOR.$event->language.DIRECTORY_SEPARATOR.$event->category.'.php';
$message = str_replace("'", "\'",$event->message);
if (is_file($file)) {
$contents = file_get_contents($file);
$contents = preg_replace("/\)\s*;/", "\t'{$message}'=>'{$message}',\n);", $contents);
}
else {
$contents = "<?php\nreturn array(\n\t'{$message}'=>'{$message}',\n);";
}
$fp = @fopen($file, 'w');
if ($fp) {
fwrite($fp, $contents);
fclose($fp);
}
}
}
- In the config file main.php, define message component (using behavior) as follows:
return array(
......
'language'=>'en',
'components'=>array(
......
'messages'=>array(
'class'=>'CPhpMessageSource',
'behaviors'=>array(
'message'=>array(
'class'=>'application.components.MessageBehavior',
),
),
),
......
),
...
);
- After all messages are saved into message files, translate the files.
OK, that’s all.
Sorry for my bad English.