feat: 批量翻译目录下的所有 markdown 文件
This commit is contained in:
parent
7f04568b62
commit
80e318584a
@ -1,6 +1,6 @@
|
||||
import { expect } from 'chai';
|
||||
import { dirs } from './dirs';
|
||||
import { kernelText, lookup, translate } from './translate';
|
||||
import { kernelText, lookup, translateDirectory, translateFile } from './translate';
|
||||
|
||||
|
||||
describe('根据字典进行翻译', () => {
|
||||
@ -12,10 +12,11 @@ describe('根据字典进行翻译', () => {
|
||||
expect(lookup('# Forms')[0].translation).eql('# 表单');
|
||||
});
|
||||
|
||||
it('自动根据字典翻译单个文件', function () {
|
||||
const fs = require('fs');
|
||||
const content = fs.readFileSync(__dirname + '/../../../../content-en/' + 'guide/forms.md', 'utf-8');
|
||||
const result = translate(content);
|
||||
fs.writeFileSync(dirs.content + 'guide/forms.md', result.join('\n\n'), 'utf-8');
|
||||
it('自动根据字典翻译单个文件(测试)', function () {
|
||||
translateFile(__dirname + '/../../../../content-en/' + 'guide/forms.md', dirs.content + 'guide/forms.md');
|
||||
});
|
||||
|
||||
it('自动根据字典翻译所有文件(非测试)', function () {
|
||||
translateDirectory(__dirname + '/../../../../content-en/', dirs.content);
|
||||
});
|
||||
});
|
||||
|
@ -1,5 +1,8 @@
|
||||
import * as fs from 'fs';
|
||||
import * as _ from 'lodash';
|
||||
import { DictEntry } from './dict-entry';
|
||||
import { dirs } from './dirs';
|
||||
import { listMarkdownFiles } from './extractor';
|
||||
import { indentOf, normalizeLines, repeat } from './utils';
|
||||
|
||||
export const dict = require('./dict-3.json') as DictEntry[];
|
||||
@ -35,3 +38,16 @@ export function translate(content: string): string[] {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function translateFile(sourceFile: string, targetFile: string): void {
|
||||
const content = fs.readFileSync(sourceFile, 'utf-8');
|
||||
const result = translate(content);
|
||||
fs.writeFileSync(targetFile, result.join('\n\n'), 'utf-8');
|
||||
}
|
||||
|
||||
export function translateDirectory(sourceDir: string, targetDir: string): void {
|
||||
const files = listMarkdownFiles(sourceDir);
|
||||
files.forEach(fileName => {
|
||||
translateFile(fileName, fileName.replace(/^.*content-en\//, dirs.content));
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user