簡體文章批量轉換爲繁體

簡體繁體相互轉化是個常見需求,比如網站的多語言支持、多語言的操作手冊、輸入法等。最近,我發現本站有部分繁體用戶訪問,因此有必要提供繁體語言支持。

Linux 操作系統下的簡繁轉換操作如下:

1.安裝 opencc

1sudo apt-get install opencc

opencc是個開源項目,感謝相關人員的無私奉獻。

2.單文件轉換示例:

1➜  ~ cat simplified.txt
2静夜思
3床前明月光,疑是地上霜。
4举头望明月,低头思故乡。
5➜  ~ opencc -i simplified.txt -o traditional.txt -c s2t.json
6➜  ~ cat traditional.txt
7靜夜思
8牀前明月光,疑是地上霜。
9舉頭望明月,低頭思故鄉。

就這麼簡單,一首《靜夜思》簡繁轉換就完成了。

參數說明

-i 輸入文件

-o 輸出文件

-c 配置文件

  • s2t.json Simplified Chinese to Traditional Chinese 簡體到繁體
  • t2s.json Traditional Chinese to Simplified Chinese 繁體到簡體
  • s2tw.json Simplified Chinese to Traditional Chinese (Taiwan Standard) 簡體到臺灣正體
  • tw2s.json Traditional Chinese (Taiwan Standard) to Simplified Chinese 臺灣正體到簡體
  • s2hk.json Simplified Chinese to Traditional Chinese (Hong Kong variant) 簡體到香港繁體
  • hk2s.json Traditional Chinese (Hong Kong variant) to Simplified Chinese 香港繁體到簡體
  • s2twp.json Simplified Chinese to Traditional Chinese (Taiwan Standard) with Taiwanese idiom 簡體到繁體(臺灣正體標準)並轉換爲臺灣常用詞彙
  • tw2sp.json Traditional Chinese (Taiwan Standard) to Simplified Chinese with Mainland Chinese idiom 繁體(臺灣正體標準)到簡體並轉換爲中國大陸常用詞彙
  • t2tw.json Traditional Chinese (OpenCC Standard) to Taiwan Standard 繁體(OpenCC 標準)到臺灣正體
  • hk2t.json Traditional Chinese (Hong Kong variant) to Traditional Chinese 香港繁體到繁體(OpenCC 標準)
  • t2hk.json Traditional Chinese (OpenCC Standard) to Hong Kong variant 繁體(OpenCC 標準)到香港繁體
  • t2jp.json Traditional Chinese Characters (Kyūjitai) to New Japanese Kanji (Shinjitai) 繁體(OpenCC 標準,舊字體)到日文新字體
  • jp2t.json New Japanese Kanji (Shinjitai) to Traditional Chinese Characters (Kyūjitai) 日文新字體到繁體(OpenCC 標準,舊字體)
  • tw2t.json Traditional Chinese (Taiwan standard) to Traditional Chinese 臺灣正體到繁體(OpenCC 標準)
➜  ~ opencc --help

Open Chinese Convert (OpenCC) Command Line Tool
Author: Carbo Kuo <byvoid@byvoid.com>
Bug Report: http://github.com/BYVoid/OpenCC/issues

Usage:

opencc  [--noflush <bool>] [-i <file>] [-o <file>] [-c <file>] [--]
        [--version] [-h]

Options:

--noflush <bool>
    Disable flush for every line

-i <file>,  --input <file>
    Read original text from <file>.

-o <file>,  --output <file>
    Write converted text to <file>.

-c <file>,  --config <file>
    Configuration file

--,  --ignore_rest
    Ignores the rest of the labeled arguments following this flag.

--version
    Displays version information and exits.

-h,  --help
    Displays usage information and exits.


Open Chinese Convert (OpenCC) Command Line Tool

3.多文件批量轉換

使用fd 命令, 以批量轉換 markdown 文件爲例:

1fd -e .md -x opencc -i {} -o {/.}.zh-tw.md -c s2t.json
  • fd -e .md 意爲查找以.md結尾的文件

  • -x/--exec 對上一步搜索的每個結果執行額外操作

  • opencc -i {} -o {/.}.zh-tw.md -c s2t.json 對每個文件文件執行簡繁轉換,{}{./}爲 fd 命令的語法(示例意思爲,假設輸入爲 aaa.md 輸出就爲 aaa.zh-tw.md,之所以這麼寫是博客系統的多語言規則要求,讀者可根據下面說明適當更改):

    • {}: A placeholder token that will be replaced with the path of the search result (documents/images/party.jpg).
    • {.}: Like {}, but without the file extension (documents/images/party).
    • {/}: A placeholder that will be replaced by the basename of the search result (party.jpg).
    • {//}: The parent of the discovered path (documents/images).
    • {/.}: The basename, with the extension removed (party).

想要轉換文件,只要寫對搜索匹配規則,輸出爲自己想要的文件名即可,筆者用openccfd命令,十多分鐘就把幾千篇文章從簡體轉換爲繁體了,漢字的各種體間隨意轉換,方便好用。

最後修改於: Monday, August 28, 2023

相關文章:

翻譯: