Skip to content

Commit

Permalink
Change translate script
Browse files Browse the repository at this point in the history
  • Loading branch information
ihavecoke committed Oct 31, 2023
1 parent 711336a commit 989d37b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
5 changes: 0 additions & 5 deletions docs/index.md

This file was deleted.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{
"scripts": {
"export": "./scripts/export",
"translate": "node ./scripts/cn2hk.js",
"translate": "node ./scripts/hk2cn.js",
"build": "vitepress build",
"dev": "vitepress dev"
"dev": "vitepress dev",
"clean:vitepress": "rm -rf .vitepress/dist && rm -rf docs/*",
"clean:export": "rm -rf dist/*",
"clean": "bun run clean:export && bun run clean:vitepress",
"local:build": "bun run export && bun run translate && bun run build"
},
"dependencies": {
"autocorrect-node": "^2.8.4",
Expand Down
34 changes: 27 additions & 7 deletions scripts/cn2hk.js → scripts/hk2cn.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@ const OpenCC = require('opencc-js');

const converter = OpenCC.Converter({ from: 'hk', to: 'cn' });


function isFile(filePath) {
try {
const stats = fs.statSync(filePath);
return stats.isFile();
} catch (err) {
// An error occurred, which likely means the path doesn't exist or is not accessible.
return false;
}
}
/**
* Iterator all .md file in docs/zh-CN/ and convert to docs/zh-HK/
*/
const convert = () => {
if(!fs.existsSync(path.resolve(__dirname,'../docs/zh-HK.md'))) return
let content = fs.readFileSync(path.resolve(__dirname,'../docs/zh-HK.md'), 'utf8');
let newContent = converter(content);
fs.writeFileSync(path.resolve(__dirname,'../docs/zh-HK.md'), newContent);
fs.writeFileSync(path.resolve(__dirname,'../docs/zh-CN.md'), newContent);

// 翻译 CN to HK
let files = glob.sync(path.resolve(__dirname,'../docs/zh-HK/**/*.md'));
files.forEach((file) => {
let content = fs.readFileSync(file, 'utf8');
Expand All @@ -27,9 +36,20 @@ const convert = () => {
fs.mkdirSync(folder, { recursive: true });
fs.writeFileSync(newFile, newContent);
});

// 复制非 md 文件
let assetsFiles = glob.sync(path.join(__dirname, '..', 'docs', 'zh-HK', '**', '!(*.md)'));
assetsFiles.forEach((file) => {
if(isFile(file)){
console.log('copy file:', file)
const destinationDirectory = file.replace('zh-HK', 'zh-CN').replace(path.basename(file), '');
fs.mkdirSync(destinationDirectory, { recursive: true });
fs.copyFileSync(file, file.replace('zh-HK', 'zh-CN'));
}
})
};

const deepConvertDocIntoZhHK = (doc) => {
const deepConvertDocIntoZhCN = (doc) => {
let newDoc = Object.assign({}, doc);

newDoc.title = converter(newDoc.title);
Expand All @@ -38,15 +58,15 @@ const deepConvertDocIntoZhHK = (doc) => {

if (newDoc.children) {
newDoc.children = newDoc.children.map((child) => {
return deepConvertDocIntoZhHK(child);
return deepConvertDocIntoZhCN(child);
});
}

return newDoc;
};

/**
* Parse docs.json and duplicate zh-CN to zh-HK by use OpenCC to convert
* Parse docs.json and duplicate zh-HK to zh-CN by use OpenCC to convert
*/
const convertDocsJSON = () => {
let docs = require('../docs/docs.json');
Expand All @@ -58,9 +78,9 @@ const convertDocsJSON = () => {
newDocs.push(doc);
let newDoc = Object.assign({}, doc);

newDoc.title = '简体中文';
newDoc.title = '繁体中文';

newDoc = deepConvertDocIntoZhHK(newDoc);
newDoc = deepConvertDocIntoZhCN(newDoc);

newDocs.push(newDoc);
found = true;
Expand Down

0 comments on commit 989d37b

Please sign in to comment.