mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +08:00 
			
		
		
		
	Fixes #1973: Re-implemented solution for yii message/extract is now able to generate .po files
				
					
				
			This commit is contained in:
		@ -64,6 +64,7 @@ Yii Framework 2 Change Log
 | 
				
			|||||||
- Enh #1852: ActiveRecord::tableName() now returns table name using DbConnection::tablePrefix (creocoder)
 | 
					- Enh #1852: ActiveRecord::tableName() now returns table name using DbConnection::tablePrefix (creocoder)
 | 
				
			||||||
- Enh #1894: The path aliases `@webroot` and `@web` are now available right after the application is initialized (qiangxue)
 | 
					- Enh #1894: The path aliases `@webroot` and `@web` are now available right after the application is initialized (qiangxue)
 | 
				
			||||||
- Enh #1921: Grid view ActionColumn now allow to name buttons like `{controller/action}` (creocoder)
 | 
					- Enh #1921: Grid view ActionColumn now allow to name buttons like `{controller/action}` (creocoder)
 | 
				
			||||||
 | 
					- Enh #1973: `yii message/extract` is now able to generate `.po` files (SergeiKutanov, samdark)
 | 
				
			||||||
- Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark)
 | 
					- Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark)
 | 
				
			||||||
- Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue)
 | 
					- Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue)
 | 
				
			||||||
- Enh: Support for file aliases in console command 'message' (omnilight)
 | 
					- Enh: Support for file aliases in console command 'message' (omnilight)
 | 
				
			||||||
 | 
				
			|||||||
@ -15,8 +15,9 @@ use yii\helpers\FileHelper;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * This command extracts messages to be translated from source files.
 | 
					 * This command extracts messages to be translated from source files.
 | 
				
			||||||
 * The extracted messages are saved as PHP message source files
 | 
					 * The extracted messages are saved either as PHP message source files
 | 
				
			||||||
 * under the specified directory.
 | 
					 * or ".po" files under the specified directory. Format depends on `format`
 | 
				
			||||||
 | 
					 * setting in config file.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * Usage:
 | 
					 * Usage:
 | 
				
			||||||
 * 1. Create a configuration file using the 'message/config' command:
 | 
					 * 1. Create a configuration file using the 'message/config' command:
 | 
				
			||||||
@ -69,7 +70,7 @@ class MessageController extends Controller
 | 
				
			|||||||
	 * this file and then customize it for your needs.
 | 
						 * this file and then customize it for your needs.
 | 
				
			||||||
	 * @throws Exception on failure.
 | 
						 * @throws Exception on failure.
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public function actionExtract($configFile, $out=null)
 | 
						public function actionExtract($configFile)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		$configFile = Yii::getAlias($configFile);
 | 
							$configFile = Yii::getAlias($configFile);
 | 
				
			||||||
		if (!is_file($configFile)) {
 | 
							if (!is_file($configFile)) {
 | 
				
			||||||
@ -81,6 +82,7 @@ class MessageController extends Controller
 | 
				
			|||||||
			'overwrite' => false,
 | 
								'overwrite' => false,
 | 
				
			||||||
			'removeUnused' => false,
 | 
								'removeUnused' => false,
 | 
				
			||||||
			'sort' => false,
 | 
								'sort' => false,
 | 
				
			||||||
 | 
								'format' => 'php',
 | 
				
			||||||
		], require($configFile));
 | 
							], require($configFile));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (!isset($config['sourcePath'], $config['messagePath'], $config['languages'])) {
 | 
							if (!isset($config['sourcePath'], $config['messagePath'], $config['languages'])) {
 | 
				
			||||||
@ -95,6 +97,9 @@ class MessageController extends Controller
 | 
				
			|||||||
		if (empty($config['languages'])) {
 | 
							if (empty($config['languages'])) {
 | 
				
			||||||
			throw new Exception("Languages cannot be empty.");
 | 
								throw new Exception("Languages cannot be empty.");
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							if (empty($config['format']) || !in_array($config['format'], ['php', 'po'])) {
 | 
				
			||||||
 | 
								throw new Exception('Format should be either "php" or "po".');
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		$files = FileHelper::findFiles(realpath($config['sourcePath']), $config);
 | 
							$files = FileHelper::findFiles(realpath($config['sourcePath']), $config);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -109,21 +114,13 @@ class MessageController extends Controller
 | 
				
			|||||||
				@mkdir($dir);
 | 
									@mkdir($dir);
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			foreach ($messages as $category => $msgs) {
 | 
								foreach ($messages as $category => $msgs) {
 | 
				
			||||||
                if($out == 'po'){
 | 
									$file = str_replace("\\", '/', "$dir/$category." . $config['format']);
 | 
				
			||||||
                    $file = str_replace("\\", '/', "$dir/$category.po");
 | 
					 | 
				
			||||||
                }else if($out == null){
 | 
					 | 
				
			||||||
                    $file = str_replace("\\", '/', "$dir/$category.php");
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
				$path = dirname($file);
 | 
									$path = dirname($file);
 | 
				
			||||||
				if (!is_dir($path)) {
 | 
									if (!is_dir($path)) {
 | 
				
			||||||
					mkdir($path, 0755, true);
 | 
										mkdir($path, 0755, true);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				$msgs = array_values(array_unique($msgs));
 | 
									$msgs = array_values(array_unique($msgs));
 | 
				
			||||||
                if($out == 'po'){
 | 
									$this->generateMessageFile($msgs, $file, $config['overwrite'], $config['removeUnused'], $config['sort'], $config['format']);
 | 
				
			||||||
                    $this->generateMessageFile($msgs, $file, $config['overwrite'], $config['removeUnused'], $config['sort'], $out);
 | 
					 | 
				
			||||||
                }else if($out == null){
 | 
					 | 
				
			||||||
                    $this->generateMessageFile($msgs, $file, $config['overwrite'], $config['removeUnused'], $config['sort']);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -168,20 +165,21 @@ class MessageController extends Controller
 | 
				
			|||||||
	 * @param boolean $overwrite if existing file should be overwritten without backup
 | 
						 * @param boolean $overwrite if existing file should be overwritten without backup
 | 
				
			||||||
	 * @param boolean $removeUnused if obsolete translations should be removed
 | 
						 * @param boolean $removeUnused if obsolete translations should be removed
 | 
				
			||||||
	 * @param boolean $sort if translations should be sorted
 | 
						 * @param boolean $sort if translations should be sorted
 | 
				
			||||||
 | 
						 * @param string $format output format
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	protected function generateMessageFile($messages, $fileName, $overwrite, $removeUnused, $sort, $type=null)
 | 
						protected function generateMessageFile($messages, $fileName, $overwrite, $removeUnused, $sort, $format)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		echo "Saving messages to $fileName...";
 | 
							echo "Saving messages to $fileName...";
 | 
				
			||||||
		if (is_file($fileName)) {
 | 
							if (is_file($fileName)) {
 | 
				
			||||||
            if($type == 'po'){
 | 
								if($format === 'po'){
 | 
				
			||||||
                $translated = file_get_contents($fileName);
 | 
									$translated = file_get_contents($fileName);
 | 
				
			||||||
                preg_match_all('/(?<=msgid ").*(?="\nmsgstr)/', $translated, $keys);
 | 
									preg_match_all('/(?<=msgid ").*(?="\nmsgstr)/', $translated, $keys);
 | 
				
			||||||
                preg_match_all('/(?<=msgstr ").*(?="\n\n)/', $translated, $values);
 | 
									preg_match_all('/(?<=msgstr ").*(?="\n\n)/', $translated, $values);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                $translated = array_combine($keys[0], $values[0]);
 | 
									$translated = array_combine($keys[0], $values[0]);
 | 
				
			||||||
            }else if($type == null){
 | 
								} else {
 | 
				
			||||||
                $translated = require($fileName);
 | 
									$translated = require($fileName);
 | 
				
			||||||
            }
 | 
								}
 | 
				
			||||||
			sort($messages);
 | 
								sort($messages);
 | 
				
			||||||
			ksort($translated);
 | 
								ksort($translated);
 | 
				
			||||||
			if (array_keys($translated) == $messages) {
 | 
								if (array_keys($translated) == $messages) {
 | 
				
			||||||
@ -220,40 +218,39 @@ class MessageController extends Controller
 | 
				
			|||||||
			if (false === $overwrite) {
 | 
								if (false === $overwrite) {
 | 
				
			||||||
				$fileName .= '.merged';
 | 
									$fileName .= '.merged';
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
            if($type == 'po'){
 | 
								if($format === 'po'){
 | 
				
			||||||
                $out_str = '';
 | 
									$out_str = '';
 | 
				
			||||||
                ksort($merged);
 | 
									foreach($merged as $k=>$v){
 | 
				
			||||||
                foreach($merged as $k=>$v){
 | 
										$out_str .= "msgid \"$k\"\n";
 | 
				
			||||||
                    $out_str .= "msgid \"$k\"\n";
 | 
										$out_str .= "msgstr \"$v\"\n";
 | 
				
			||||||
                    $out_str .= "msgstr \"$v\"\n";
 | 
										$out_str .= "\n";
 | 
				
			||||||
                    $out_str .= "\n";
 | 
									}
 | 
				
			||||||
                }
 | 
									$merged = $out_str;
 | 
				
			||||||
                $merged = $out_str;
 | 
								}
 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
			echo "translation merged.\n";
 | 
								echo "translation merged.\n";
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
            if($type == 'po'){
 | 
								if ($format === 'po') {
 | 
				
			||||||
                $merged = '';
 | 
									$merged = '';
 | 
				
			||||||
                sort($messages);
 | 
									sort($messages);
 | 
				
			||||||
                foreach($messages as $message){
 | 
									foreach($messages as $message) {
 | 
				
			||||||
                    $merged .= "msgid \"$message\"\n";
 | 
										$merged .= "msgid \"$message\"\n";
 | 
				
			||||||
                    $merged .= "msgstr \"\"\n";
 | 
										$merged .= "msgstr \"\"\n";
 | 
				
			||||||
                    $merged .= "\n";
 | 
										$merged .= "\n";
 | 
				
			||||||
                }
 | 
									}
 | 
				
			||||||
            }else if($type == null){
 | 
								} else {
 | 
				
			||||||
                $merged = [];
 | 
									$merged = [];
 | 
				
			||||||
                foreach ($messages as $message) {
 | 
									foreach ($messages as $message) {
 | 
				
			||||||
                    $merged[$message] = '';
 | 
										$merged[$message] = '';
 | 
				
			||||||
                }
 | 
									}
 | 
				
			||||||
                ksort($merged);
 | 
									ksort($merged);
 | 
				
			||||||
            }
 | 
								}
 | 
				
			||||||
			echo "saved.\n";
 | 
								echo "saved.\n";
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
        if($type == 'po'){
 | 
							if ($format === 'po') {
 | 
				
			||||||
            $content = $merged;
 | 
								$content = $merged;
 | 
				
			||||||
        }else if($type == null){
 | 
							} else {
 | 
				
			||||||
            $array = str_replace("\r", '', var_export($merged, true));
 | 
								$array = str_replace("\r", '', var_export($merged, true));
 | 
				
			||||||
            $content = <<<EOD
 | 
								$content = <<<EOD
 | 
				
			||||||
<?php
 | 
					<?php
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Message translations.
 | 
					 * Message translations.
 | 
				
			||||||
@ -275,7 +272,7 @@ class MessageController extends Controller
 | 
				
			|||||||
return $array;
 | 
					return $array;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
EOD;
 | 
					EOD;
 | 
				
			||||||
        }
 | 
							}
 | 
				
			||||||
		file_put_contents($fileName, $content);
 | 
							file_put_contents($fileName, $content);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -42,4 +42,6 @@ return [
 | 
				
			|||||||
		'.hgkeep',
 | 
							'.hgkeep',
 | 
				
			||||||
		'/messages',
 | 
							'/messages',
 | 
				
			||||||
	],
 | 
						],
 | 
				
			||||||
 | 
						// Generated file format. Can be either "php" or "po".
 | 
				
			||||||
 | 
						'format' => 'php',
 | 
				
			||||||
];
 | 
					];
 | 
				
			||||||
 | 
				
			|||||||
@ -6,7 +6,7 @@ return [
 | 
				
			|||||||
	// string, required, root directory containing message translations.
 | 
						// string, required, root directory containing message translations.
 | 
				
			||||||
	'messagePath' => __DIR__ . DIRECTORY_SEPARATOR . 'messages',
 | 
						'messagePath' => __DIR__ . DIRECTORY_SEPARATOR . 'messages',
 | 
				
			||||||
	// array, required, list of language codes that the extracted messages
 | 
						// array, required, list of language codes that the extracted messages
 | 
				
			||||||
	// should be translated to. For example, ['zh_cn', 'de'].
 | 
						// should be translated to. For example, ['zh-CN', 'de'].
 | 
				
			||||||
	'languages' => ['de'],
 | 
						'languages' => ['de'],
 | 
				
			||||||
	// string, the name of the function for translating messages.
 | 
						// string, the name of the function for translating messages.
 | 
				
			||||||
	// Defaults to 'Yii::t'. This is used as a mark to find the messages to be
 | 
						// Defaults to 'Yii::t'. This is used as a mark to find the messages to be
 | 
				
			||||||
@ -42,4 +42,6 @@ return [
 | 
				
			|||||||
		'.hgkeep',
 | 
							'.hgkeep',
 | 
				
			||||||
		'/messages',
 | 
							'/messages',
 | 
				
			||||||
	],
 | 
						],
 | 
				
			||||||
 | 
						// Generated file format. Can be either "php" or "po".
 | 
				
			||||||
 | 
						'format' => 'php',
 | 
				
			||||||
];
 | 
					];
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user