扫码阅读
手机扫码阅读

GitHub Actions 真香系列之文档自动简繁体转换

135 2024-01-25

GitHub Actions 是 GitHub 推出的 CI\CD 服务,正式版推出后也没有尝试过,最近搞了几个确实真香。

在 GitHub Actions 中有一些自己的术语:

  1. workflow (工作流程):持续集成一次运行的过程,就是一个 workflow;

  2. job(任务):一个 workflow 由一个或多个 jobs 构成,含义是一次持续集成的运行,可以完成多个任务;

  3. step(步骤):每个 job 由多个 step 构成,一步步完成;

  4. action(动作):每个 step 可以依次执行一个或多个命令(action);

感兴趣的可以看看 官方文档 或者 应用市场 ,入手还很快的

这篇文章说一下自动翻译简体中文文档到繁体中文问题的 action,基于 opencc 实现的简繁体转换。

首先需要安装 opencc ,我们使用的是 ubuntu-latest 的环境,所以直接使用 apt-get 安装

apt-get install libopencc-dev -y 

使用 PHP 的 opencc4php 扩展来调用,需要安装一下扩展

git clone git@github.com:NauxLiu/opencc4php.git --depth 1
cd opencc4php
phpize
./configure
make && sudo make install 

使用 symfony/finder 组件来遍历目录和文件

{ "require-dev": { "symfony/finder": "^5.1" }
} 

之后在 PHP 代码中来调用 opencc4php 的 API 进行简繁体转换

define('ROOT_DIR', dirname(__DIR__)); require ROOT_DIR . '/tools/vendor/autoload.php'; use Symfony\Component\Finder\Finder;
$config = [ 'zh-tw' => [ 'targetDir' => ROOT_DIR . '/docs/zh-tw/', 'rule' => 's2twp.json',
    ], 'zh-hk' => [ 'targetDir' => ROOT_DIR . '/docs/zh-hk/', 'rule' => 's2hk.json',
    ],
];
$finder = new Finder();
$finder->files()->in(ROOT_DIR . '/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);
            $translated = str_replace('](./zh-cn/', '](./' . $key . '/', $translated);
            $targetTranslatedPath = $targetDir . $fileInfo->getRelativePathname();
            @file_put_contents($targetTranslatedPath, $translated);
        } else {
            $targetTranslatedPath = $targetDir . $fileInfo->getRelativePathname();
            @copy($fileInfo->getRealPath(), $targetTranslatedPath);
        }
    }
    opencc_close($od);
} 

tools目录中创建为对应的composer.jsontranslate.php文件

最后,整合到一块就是一个 action 了,创建.github/workflows目录,新建一个translate.yml文件

name: Translate docs on: [push, pull_request] jobs: translate: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 with: ref: ${{ github.head_ref }} - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: 7.4 tools: phpize ini-values: extension=opencc - name: Install OpenCC run: |
          sudo apt-get install libopencc-dev -y  - name: Build opencc4php run: |
          git clone https://github.com/nauxliu/opencc4php.git --depth 1
          cd opencc4php
          phpize
          ./configure
          make
          sudo make install
          php --ri opencc
          cd ..
          rm -rf opencc4php  - name: Start Translate run: |
          cd tools
          composer install
          php translate.php  - name: Commit Updated uses: stefanzweifel/git-auto-commit-action@v4 with: commit_message: Update docs and translate 

提交到 GitHub 之后,修改文档直接 push 或者 contributor 提交 PR 的时候,都会生成一个 commit message 为Update docs and translate的提交

至此就实现了自动简繁体转换的功能,示例可以看swow/wiki

原文链接: https://mp.weixin.qq.com/s?__biz=MzAwOTgzNjY4MA==&mid=2247483815&idx=1&sn=26fb8e78c91f5ea45b9d465930acbf9d