MessageController.php 8.92 KB
Newer Older
Qiang Xue committed
1 2 3 4
<?php
/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.yiiframework.com/
Qiang Xue committed
5
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
6 7 8
 * @license http://www.yiiframework.com/license/
 */

9 10
namespace yii\console\controllers;

11
use Yii;
12
use yii\console\Controller;
13 14
use yii\console\Exception;
use yii\helpers\FileHelper;
15

Qiang Xue committed
16
/**
17
 * This command extracts messages to be translated from source files.
18 19 20
 * The extracted messages are saved either as PHP message source files
 * or ".po" files under the specified directory. Format depends on `format`
 * setting in config file.
Qiang Xue committed
21
 *
22
 * Usage:
23 24
 * 1. Create a configuration file using the 'message/config' command:
 *    yii message/config /path/to/myapp/messages/config.php
25
 * 2. Edit the created config file, adjusting it for your web application needs.
26
 * 3. Run the 'message/extract' command, using created config:
27 28
 *    yii message /path/to/myapp/messages/config.php
 *
Qiang Xue committed
29
 * @author Qiang Xue <qiang.xue@gmail.com>
30
 * @since 2.0
Qiang Xue committed
31
 */
32
class MessageController extends Controller
Qiang Xue committed
33
{
34 35 36
	/**
	 * @var string controller default action ID.
	 */
37 38 39
	public $defaultAction = 'extract';


Qiang Xue committed
40
	/**
41
	 * Creates a configuration file for the "extract" command.
42
	 *
43 44 45
	 * The generated configuration file contains detailed instructions on
	 * how to customize it to fit for your needs. After customization,
	 * you may use this configuration file with the "extract" command.
46
	 *
47
	 * @param string $filePath output file name or alias.
48 49 50 51
	 * @throws Exception on failure.
	 */
	public function actionConfig($filePath)
	{
52
		$filePath = Yii::getAlias($filePath);
53 54 55 56 57 58 59 60 61 62 63
		if (file_exists($filePath)) {
			if (!$this->confirm("File '{$filePath}' already exists. Do you wish to overwrite it?")) {
				return;
			}
		}
		copy(Yii::getAlias('@yii/views/messageConfig.php'), $filePath);
		echo "Configuration file template created at '{$filePath}'.\n\n";
	}

	/**
	 * Extracts messages to be translated from source code.
64
	 *
65 66
	 * This command will search through source code files and extract
	 * messages that need to be translated in different languages.
67
	 *
68
	 * @param string $configFile the path or alias of the configuration file.
69 70 71
	 * You may use the "yii message/config" command to generate
	 * this file and then customize it for your needs.
	 * @throws Exception on failure.
Qiang Xue committed
72
	 */
73
	public function actionExtract($configFile)
Qiang Xue committed
74
	{
75
		$configFile = Yii::getAlias($configFile);
76
		if (!is_file($configFile)) {
Qiang Xue committed
77
			throw new Exception("The configuration file does not exist: $configFile");
resurtm committed
78
		}
79

Alexander Makarov committed
80
		$config = array_merge([
81 82 83 84
			'translator' => 'Yii::t',
			'overwrite' => false,
			'removeUnused' => false,
			'sort' => false,
85
			'format' => 'php',
Alexander Makarov committed
86
		], require($configFile));
Qiang Xue committed
87

88
		if (!isset($config['sourcePath'], $config['messagePath'], $config['languages'])) {
89
			throw new Exception('The configuration file must specify "sourcePath", "messagePath" and "languages".');
resurtm committed
90
		}
91 92
		if (!is_dir($config['sourcePath'])) {
			throw new Exception("The source path {$config['sourcePath']} is not a valid directory.");
resurtm committed
93
		}
94 95
		if (!is_dir($config['messagePath'])) {
			throw new Exception("The message path {$config['messagePath']} is not a valid directory.");
resurtm committed
96
		}
97
		if (empty($config['languages'])) {
98
			throw new Exception("Languages cannot be empty.");
resurtm committed
99
		}
100 101 102
		if (empty($config['format']) || !in_array($config['format'], ['php', 'po'])) {
			throw new Exception('Format should be either "php" or "po".');
		}
Qiang Xue committed
103

104
		$files = FileHelper::findFiles(realpath($config['sourcePath']), $config);
Qiang Xue committed
105

Alexander Makarov committed
106
		$messages = [];
resurtm committed
107
		foreach ($files as $file) {
Qiang Xue committed
108
			$messages = array_merge_recursive($messages, $this->extractMessages($file, $config['translator']));
resurtm committed
109
		}
Qiang Xue committed
110

111 112
		foreach ($config['languages'] as $language) {
			$dir = $config['messagePath'] . DIRECTORY_SEPARATOR . $language;
resurtm committed
113
			if (!is_dir($dir)) {
Qiang Xue committed
114
				@mkdir($dir);
resurtm committed
115 116
			}
			foreach ($messages as $category => $msgs) {
117
				$file = str_replace("\\", '/', "$dir/$category." . $config['format']);
118 119 120 121
				$path = dirname($file);
				if (!is_dir($path)) {
					mkdir($path, 0755, true);
				}
resurtm committed
122
				$msgs = array_values(array_unique($msgs));
123
				$this->generateMessageFile($msgs, $file, $config['overwrite'], $config['removeUnused'], $config['sort'], $config['format']);
Qiang Xue committed
124 125 126 127
			}
		}
	}

Alexander Makarov committed
128 129 130 131 132 133 134
	/**
	 * Extracts messages from a file
	 *
	 * @param string $fileName name of the file to extract messages from
	 * @param string $translator name of the function used to translate messages
	 * @return array
	 */
resurtm committed
135
	protected function extractMessages($fileName, $translator)
Qiang Xue committed
136 137
	{
		echo "Extracting messages from $fileName...\n";
resurtm committed
138
		$subject = file_get_contents($fileName);
Alexander Makarov committed
139
		$messages = [];
140
		if (!is_array($translator)) {
Alexander Makarov committed
141
			$translator = [$translator];
142 143 144 145 146 147 148 149 150 151 152 153 154
		}
		foreach ($translator as $currentTranslator) {
			$n = preg_match_all(
				'/\b' . $currentTranslator . '\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*,\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s',
				$subject, $matches, PREG_SET_ORDER);
			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
resurtm committed
155
			}
Qiang Xue committed
156 157 158 159
		}
		return $messages;
	}

Alexander Makarov committed
160 161 162 163 164 165
	/**
	 * Writes messages into file
	 *
	 * @param array $messages
	 * @param string $fileName name of the file to write to
	 * @param boolean $overwrite if existing file should be overwritten without backup
166
	 * @param boolean $removeUnused if obsolete translations should be removed
Alexander Makarov committed
167
	 * @param boolean $sort if translations should be sorted
168
	 * @param string $format output format
Alexander Makarov committed
169
	 */
170
	protected function generateMessageFile($messages, $fileName, $overwrite, $removeUnused, $sort, $format)
Qiang Xue committed
171 172
	{
		echo "Saving messages to $fileName...";
resurtm committed
173
		if (is_file($fileName)) {
174 175
			if($format === 'po'){
				$translated = file_get_contents($fileName);
176
				preg_match_all('/(?<=msgid ").*(?="\n(#*)msgstr)/', $translated, $keys);
177 178 179 180 181
				preg_match_all('/(?<=msgstr ").*(?="\n\n)/', $translated, $values);
				$translated = array_combine($keys[0], $values[0]);
			} else {
				$translated = require($fileName);
			}
Qiang Xue committed
182 183
			sort($messages);
			ksort($translated);
resurtm committed
184
			if (array_keys($translated) == $messages) {
Qiang Xue committed
185 186 187
				echo "nothing new...skipped.\n";
				return;
			}
Alexander Makarov committed
188 189
			$merged = [];
			$untranslated = [];
resurtm committed
190
			foreach ($messages as $message) {
191 192 193
				if($format === 'po'){
					$message = preg_replace('/\"/', '\"', $message);
				}
194
				if (array_key_exists($message, $translated) && strlen($translated[$message]) > 0) {
resurtm committed
195 196 197 198
					$merged[$message] = $translated[$message];
				} else {
					$untranslated[] = $message;
				}
Qiang Xue committed
199 200 201
			}
			ksort($merged);
			sort($untranslated);
Alexander Makarov committed
202
			$todo = [];
resurtm committed
203 204 205
			foreach ($untranslated as $message) {
				$todo[$message] = '';
			}
Qiang Xue committed
206
			ksort($translated);
207 208 209 210 211 212
			foreach ($translated as $message => $translation) {
				if (!isset($merged[$message]) && !isset($todo[$message]) && !$removeUnused) {
					if (substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@') {
						$todo[$message] = $translation;
					} else {
						$todo[$message] = '@@' . $translation . '@@';
SergeiKutanov committed
213 214
					}
				}
215
			}
resurtm committed
216 217
			$merged = array_merge($todo, $merged);
			if ($sort) {
Qiang Xue committed
218
				ksort($merged);
resurtm committed
219 220 221 222
			}
			if (false === $overwrite) {
				$fileName .= '.merged';
			}
223
			if ($format === 'po'){
224
				$out_str = '';
225
				foreach ($merged as $k => $v){
SergeiKutanov committed
226 227
					$k = preg_replace('/(\")|(\\\")/', "\\\"", $k);
					$v = preg_replace('/(\")|(\\\")/', "\\\"", $v);
228
					if (substr($v, 0, 2) === '@@' && substr($v, -2) === '@@') {
229 230
						$out_str .= "#msgid \"$k\"\n";
						$out_str .= "#msgstr \"$v\"\n";
231
					} else {
232 233 234
						$out_str .= "msgid \"$k\"\n";
						$out_str .= "msgstr \"$v\"\n";
					}
235 236 237 238
					$out_str .= "\n";
				}
				$merged = $out_str;
			}
Qiang Xue committed
239
			echo "translation merged.\n";
resurtm committed
240
		} else {
241 242 243 244
			if ($format === 'po') {
				$merged = '';
				sort($messages);
				foreach($messages as $message) {
SergeiKutanov committed
245
					$message = preg_replace('/(\")|(\\\")/', '\\\"', $message);
246 247 248 249 250 251 252 253 254 255 256
					$merged .= "msgid \"$message\"\n";
					$merged .= "msgstr \"\"\n";
					$merged .= "\n";
				}
			} else {
				$merged = [];
				foreach ($messages as $message) {
					$merged[$message] = '';
				}
				ksort($merged);
			}
Qiang Xue committed
257 258
			echo "saved.\n";
		}
259 260 261 262 263
		if ($format === 'po') {
			$content = $merged;
		} else {
			$array = str_replace("\r", '', var_export($merged, true));
			$content = <<<EOD
Qiang Xue committed
264 265 266 267
<?php
/**
 * Message translations.
 *
268
 * This file is automatically generated by 'yii {$this->id}' command.
Qiang Xue committed
269 270 271 272 273 274 275 276 277 278 279
 * 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.
 *
280
 * NOTE: this file must be saved in UTF-8 encoding.
Qiang Xue committed
281 282 283 284
 */
return $array;

EOD;
285
		}
Qiang Xue committed
286 287 288
		file_put_contents($fileName, $content);
	}
}