You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
217 lines
6.8 KiB
217 lines
6.8 KiB
13 years ago
|
<?php
|
||
|
/**
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
13 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\console\controllers;
|
||
|
|
||
|
use yii\console\Controller;
|
||
|
|
||
13 years ago
|
/**
|
||
12 years ago
|
* This command extracts messages to be translated from source files.
|
||
13 years ago
|
* The extracted messages are saved as PHP message source files
|
||
|
* under the specified directory.
|
||
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
12 years ago
|
* @since 2.0
|
||
13 years ago
|
*/
|
||
12 years ago
|
class MessageController extends Controller
|
||
13 years ago
|
{
|
||
|
/**
|
||
12 years ago
|
* Searches for messages to be translated in the specified
|
||
|
* source files and compiles them into PHP arrays as message source.
|
||
|
*
|
||
|
* @param string $config the path of the configuration file. You can find
|
||
|
* an example in framework/messages/config.php.
|
||
|
*
|
||
|
* The file can be placed anywhere and must be a valid PHP script which
|
||
|
* returns an array of name-value pairs. Each name-value pair represents
|
||
|
* a configuration option.
|
||
|
*
|
||
|
* The following options are available:
|
||
|
*
|
||
|
* - sourcePath: string, root directory of all source files.
|
||
|
* - messagePath: string, root directory containing message translations.
|
||
|
* - languages: array, list of language codes that the extracted messages
|
||
12 years ago
|
* should be translated to. For example, array('zh_cn', 'en_au').
|
||
12 years ago
|
* - fileTypes: array, a list of file extensions (e.g. 'php', 'xml').
|
||
|
* Only the files whose extension name can be found in this list
|
||
|
* will be processed. If empty, all files will be processed.
|
||
|
* - exclude: array, a list of directory and file exclusions. Each
|
||
|
* exclusion can be either a name or a path. If a file or directory name
|
||
|
* or path matches the exclusion, it will not be copied. For example,
|
||
|
* an exclusion of '.svn' will exclude all files and directories whose
|
||
|
* name is '.svn'. And an exclusion of '/a/b' will exclude file or
|
||
|
* directory 'sourcePath/a/b'.
|
||
|
* - translator: the name of the function for translating messages.
|
||
|
* Defaults to 'Yii::t'. This is used as a mark to find messages to be
|
||
|
* translated.
|
||
|
* - overwrite: if message file must be overwritten with the merged messages.
|
||
|
* - removeOld: if message no longer needs translation it will be removed,
|
||
|
* instead of being enclosed between a pair of '@@' marks.
|
||
|
* - sort: sort messages by key when merging, regardless of their translation
|
||
|
* state (new, obsolete, translated.)
|
||
13 years ago
|
*/
|
||
12 years ago
|
public function actionIndex($config)
|
||
13 years ago
|
{
|
||
12 years ago
|
if (!is_file($config)) {
|
||
12 years ago
|
$this->usageError("the configuration file {$config} does not exist.");
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
$config = require_once($config);
|
||
13 years ago
|
|
||
|
$translator='Yii::t';
|
||
|
extract($config);
|
||
|
|
||
12 years ago
|
if (!isset($sourcePath, $messagePath, $languages)) {
|
||
13 years ago
|
$this->usageError('The configuration file must specify "sourcePath", "messagePath" and "languages".');
|
||
12 years ago
|
}
|
||
|
if (!is_dir($sourcePath)) {
|
||
13 years ago
|
$this->usageError("The source path $sourcePath is not a valid directory.");
|
||
12 years ago
|
}
|
||
|
if (!is_dir($messagePath)) {
|
||
13 years ago
|
$this->usageError("The message path $messagePath is not a valid directory.");
|
||
12 years ago
|
}
|
||
|
if (empty($languages)) {
|
||
13 years ago
|
$this->usageError("Languages cannot be empty.");
|
||
12 years ago
|
}
|
||
13 years ago
|
|
||
12 years ago
|
if (!isset($overwrite)) {
|
||
13 years ago
|
$overwrite = false;
|
||
12 years ago
|
}
|
||
|
if (!isset($removeOld)) {
|
||
13 years ago
|
$removeOld = false;
|
||
12 years ago
|
}
|
||
|
if (!isset($sort)) {
|
||
13 years ago
|
$sort = false;
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
$options = array();
|
||
|
if (isset($fileTypes)) {
|
||
|
$options['fileTypes'] = $fileTypes;
|
||
|
}
|
||
|
if (isset($exclude)) {
|
||
|
$options['exclude'] = $exclude;
|
||
|
}
|
||
|
$files = CFileHelper::findFiles(realpath($sourcePath), $options);
|
||
13 years ago
|
|
||
12 years ago
|
$messages = array();
|
||
|
foreach ($files as $file) {
|
||
|
$messages = array_merge_recursive($messages, $this->extractMessages($file, $translator));
|
||
|
}
|
||
13 years ago
|
|
||
12 years ago
|
foreach ($languages as $language) {
|
||
|
$dir = $messagePath . DIRECTORY_SEPARATOR . $language;
|
||
|
if (!is_dir($dir)) {
|
||
13 years ago
|
@mkdir($dir);
|
||
12 years ago
|
}
|
||
|
foreach ($messages as $category => $msgs) {
|
||
|
$msgs = array_values(array_unique($msgs));
|
||
|
$this->generateMessageFile($msgs, $dir . DIRECTORY_SEPARATOR . $category . '.php', $overwrite, $removeOld, $sort);
|
||
13 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
protected function extractMessages($fileName, $translator)
|
||
13 years ago
|
{
|
||
|
echo "Extracting messages from $fileName...\n";
|
||
12 years ago
|
$subject = file_get_contents($fileName);
|
||
|
$n = preg_match_all(
|
||
|
'/\b' . $translator . '\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*,\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s',
|
||
|
$subject, $matches, PREG_SET_ORDER);
|
||
|
$messages = array();
|
||
|
for ($i = 0; $i < $n; ++$i) {
|
||
|
if (($pos = strpos($matches[$i][1], '.')) !== false) {
|
||
|
$category=substr($matches[$i][1], $pos + 1, -1);
|
||
|
} else {
|
||
|
$category=substr($matches[$i][1], 1, -1);
|
||
|
}
|
||
|
$message = $matches[$i][2];
|
||
|
$messages[$category][] = eval("return $message;"); // use eval to eliminate quote escape
|
||
13 years ago
|
}
|
||
|
return $messages;
|
||
|
}
|
||
|
|
||
12 years ago
|
protected function generateMessageFile($messages, $fileName, $overwrite, $removeOld, $sort)
|
||
13 years ago
|
{
|
||
|
echo "Saving messages to $fileName...";
|
||
12 years ago
|
if (is_file($fileName)) {
|
||
|
$translated = require($fileName);
|
||
13 years ago
|
sort($messages);
|
||
|
ksort($translated);
|
||
12 years ago
|
if (array_keys($translated) == $messages) {
|
||
13 years ago
|
echo "nothing new...skipped.\n";
|
||
|
return;
|
||
|
}
|
||
12 years ago
|
$merged = array();
|
||
|
$untranslated = array();
|
||
|
foreach ($messages as $message) {
|
||
|
if (!empty($translated[$message])) {
|
||
|
$merged[$message] = $translated[$message];
|
||
|
} else {
|
||
|
$untranslated[] = $message;
|
||
|
}
|
||
13 years ago
|
}
|
||
|
ksort($merged);
|
||
|
sort($untranslated);
|
||
12 years ago
|
$todo = array();
|
||
|
foreach ($untranslated as $message) {
|
||
|
$todo[$message] = '';
|
||
|
}
|
||
13 years ago
|
ksort($translated);
|
||
12 years ago
|
foreach ($translated as $message => $translation) {
|
||
|
if (!isset($merged[$message]) && !isset($todo[$message]) && !$removeOld)
|
||
13 years ago
|
{
|
||
12 years ago
|
if (substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@') {
|
||
13 years ago
|
$todo[$message]=$translation;
|
||
12 years ago
|
} else {
|
||
|
$todo[$message] = '@@' . $translation . '@@';
|
||
|
}
|
||
13 years ago
|
}
|
||
|
}
|
||
12 years ago
|
$merged = array_merge($todo, $merged);
|
||
|
if ($sort) {
|
||
13 years ago
|
ksort($merged);
|
||
12 years ago
|
}
|
||
|
if (false === $overwrite) {
|
||
|
$fileName .= '.merged';
|
||
|
}
|
||
13 years ago
|
echo "translation merged.\n";
|
||
12 years ago
|
} else {
|
||
|
$merged = array();
|
||
|
foreach ($messages as $message) {
|
||
|
$merged[$message] = '';
|
||
|
}
|
||
13 years ago
|
ksort($merged);
|
||
|
echo "saved.\n";
|
||
|
}
|
||
12 years ago
|
$array = str_replace("\r", '', var_export($merged, true));
|
||
|
$content = <<<EOD
|
||
13 years ago
|
<?php
|
||
|
/**
|
||
|
* Message translations.
|
||
|
*
|
||
12 years ago
|
* This file is automatically generated by 'yii message' command.
|
||
13 years ago
|
* It contains the localizable messages extracted from source code.
|
||
|
* You may modify this file by translating the extracted messages.
|
||
|
*
|
||
|
* Each array element represents the translation (value) of a message (key).
|
||
|
* If the value is empty, the message is considered as not translated.
|
||
|
* Messages that no longer need translation will have their translations
|
||
|
* enclosed between a pair of '@@' marks.
|
||
|
*
|
||
|
* Message string can be used with plural forms format. Check i18n section
|
||
|
* of the guide for details.
|
||
|
*
|
||
|
* NOTE, this file must be saved in UTF-8 encoding.
|
||
|
*/
|
||
|
return $array;
|
||
|
|
||
|
EOD;
|
||
|
file_put_contents($fileName, $content);
|
||
|
}
|
||
|
}
|