hyperf/bin/doc-translate

55 lines
1.9 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env php
<?php
/**
* @notice Please DO NOT run this script manually, when you trying to submit a Pull Request of Documentation.
* @notice Install https://github.com/nauxliu/opencc4php extension before use this script.
* Run this PHP script to generate the zh-tw and zh-hk doucmentations via zh-cn.
*/
! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1));
require BASE_PATH . '/vendor/autoload.php';
use Symfony\Component\Finder\Finder;
$config = [
'zh-tw' => [
2020-06-10 00:20:28 +08:00
'targetDir' => BASE_PATH . '/docs/zh-tw/',
'rule' => 's2twp.json',
],
'zh-hk' => [
2020-06-10 00:20:28 +08:00
'targetDir' => BASE_PATH . '/docs/zh-hk/',
'rule' => 's2hk.json',
],
];
2020-06-10 00:20:28 +08:00
$finder = new Finder();
$finder->files()->in(BASE_PATH . '/docs/zh-cn');
foreach ($config as $key => $item) {
$od = opencc_open($item['rule']);
foreach ($finder as $fileInfo) {
$targetDir = $item['targetDir'];
$targetPath = $targetDir . $fileInfo->getRelativePath();
$isCreateDir = false;
if (! is_dir($targetPath)) {
mkdir($targetPath, 0777, true);
chmod($targetPath, 0777);
$isCreateDir = true;
}
if (! is_writable($targetPath)) {
echo sprintf('Target path %s is not writable.' . PHP_EOL, $targetPath);
}
if ($fileInfo->getExtension() === 'md') {
$translated = opencc_convert($fileInfo->getContents(), $od);
$translated = str_replace('](zh-cn/', '](' . $key . '/', $translated);
2020-06-22 11:20:16 +08:00
$translated = str_replace('](./zh-cn/', '](./' . $key . '/', $translated);
2020-06-10 00:20:28 +08:00
$targetTranslatedPath = $targetDir . $fileInfo->getRelativePathname();
@file_put_contents($targetTranslatedPath, $translated);
} else {
$targetTranslatedPath = $targetDir . $fileInfo->getRelativePathname();
@copy($fileInfo->getRealPath(), $targetTranslatedPath);
}
}
2020-06-10 00:20:28 +08:00
opencc_close($od);
}