├── .c2cignore ├── .gitignore ├── README.md ├── config.js ├── index.js └── package.json /.c2cignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | yarn.lock 3 | input/* 4 | output/ 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C2C 2 | 3 | > 一键转换(简体|繁体)项目到(繁体|简体)项目 4 | 5 | ## 使用 6 | 7 | 将需要转换的项目、文件夹等资源复制到根目录下的 `/input/` 文件夹下(如果不存在需手动创建),运行 `npm run start` 即可开始转换,期间会自动跳过二进制文件的转换,以免发生错误 8 | 9 | ## 忽略文件或文件夹 10 | 11 | 在根目录下的 `.c2cignore` 中添加需要忽略的内容即可,语法同 [.gitignore](https://git-scm.com/docs/gitignore) 12 | 13 | ## 转换方法 14 | 15 | > 修改根目录下的 `config.js` 文件中的 `type` 选项,可以是以下参数 16 | 17 | | Function name | Translation | 18 | | ------------------------------- | ----------------------------------------- | 19 | | `hongKongToSimplified` | Hong Kong to Simplified Chinese | 20 | | `simplifiedToHongKong` | Simplified Chinese to Hong Kong | 21 | | `simplifiedToTraditional` | Simplified Chinese to Traditional Chinese | 22 | | `simplifiedToTaiwan` | Simplified Chinese to Taiwan | 23 | | `simplifiedToTaiwanWithPhrases` | Simplified Chinese to Taiwan with phrases | 24 | | `traditionalToHongKong` | Traditional Chinese to Hong Kong | 25 | | `traditionalToSimplified` | Traditional Chinese to Simplified Chinese | 26 | | `traditionalToTaiwan` | Traditional Chinese to Taiwan | 27 | | `taiwanToSimplified` | Taiwan to Simplified Chinese | 28 | | `taiwanToSimplifiedWithPhrases` | Taiwan to Simplified Chinese with phrases | 29 | -------------------------------------------------------------------------------- /config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | type: 'simplifiedToTraditional' 3 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const opencc = require('node-opencc'); 2 | const path = require('path'); 3 | const fs = require('fs'); 4 | const { isBinaryFileSync } = require("isbinaryfile"); 5 | const { type } = require('./config'); 6 | const ig = require('ignore').default().add(fs.readFileSync('./.c2cignore', 'utf-8').toString()); 7 | 8 | const outputFolderPath = path.join(__dirname, './output'); 9 | if (!fs.existsSync(outputFolderPath)) fs.mkdirSync(outputFolderPath); 10 | 11 | const s2t = e => opencc[type](e); 12 | const getRelativePath = path => { 13 | return path.replace(__dirname, '').replace('\\input\\', ''); 14 | } 15 | 16 | function doReadDir(dir = path.join(__dirname, './input')) { 17 | const files = fs.readdirSync(dir).reverse(); 18 | for (let index = 0; index < files.length; index++) { 19 | const file = files[index]; 20 | const modulePath = path.join(dir, file); 21 | const isDir = fs.lstatSync(modulePath).isDirectory(); 22 | if (isDir) { 23 | doReadDir(modulePath); 24 | } else { 25 | try { 26 | const outputPath = path.join(__dirname, './output', getRelativePath(modulePath)); 27 | const outputFolder = path.join(__dirname, './output', getRelativePath(dir)); 28 | if (ig.ignores(getRelativePath(modulePath))) continue; 29 | if (!fs.existsSync(outputFolder)) { 30 | fs.mkdirSync(outputFolder, { 31 | recursive: true 32 | }); 33 | } 34 | if (isBinaryFileSync(modulePath)) { 35 | fs.copyFileSync(modulePath, outputPath); 36 | } else { 37 | console.log(`正在转换:${outputPath}`); 38 | const translateData = fs.readFileSync(modulePath, 'utf8'); 39 | fs.writeFileSync(outputPath, s2t(translateData)); 40 | } 41 | } catch (error) { 42 | console.error(error); 43 | } 44 | } 45 | } 46 | } 47 | 48 | doReadDir(); 49 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "c2c", 3 | "version": "1.0.0", 4 | "author": "Ice-Hazymoon", 5 | "homepage": "https://github.com/Ice-Hazymoon/c2c", 6 | "scripts": { 7 | "start": "node index.js" 8 | }, 9 | "keywords": [ 10 | "opencc", 11 | "簡繁轉換", 12 | "chinese", 13 | "简繁转换" 14 | ], 15 | "main": "index.js", 16 | "license": "MIT", 17 | "dependencies": { 18 | "ignore": "^5.1.4", 19 | "isbinaryfile": "^4.0.2", 20 | "node-opencc": "^2.0.1" 21 | } 22 | } 23 | --------------------------------------------------------------------------------