From 80e318584af9ec2793ae8670095793dc49e3b527 Mon Sep 17 00:00:00 2001 From: Zhicheng Wang Date: Sat, 3 Mar 2018 12:00:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=89=B9=E9=87=8F=E7=BF=BB=E8=AF=91?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E4=B8=8B=E7=9A=84=E6=89=80=E6=9C=89=20markdo?= =?UTF-8?q?wn=20=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aio/tools/translator/translate.spec.ts | 13 +++++++------ aio/tools/translator/translate.ts | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/aio/tools/translator/translate.spec.ts b/aio/tools/translator/translate.spec.ts index 6d6ee29d60..14913e464c 100644 --- a/aio/tools/translator/translate.spec.ts +++ b/aio/tools/translator/translate.spec.ts @@ -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); }); }); diff --git a/aio/tools/translator/translate.ts b/aio/tools/translator/translate.ts index ad04d1c891..bdeda57b56 100644 --- a/aio/tools/translator/translate.ts +++ b/aio/tools/translator/translate.ts @@ -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)); + }); +}