From c2f0045a5efb63c6738d4309957171abb325da45 Mon Sep 17 00:00:00 2001 From: Zhicheng Wang Date: Fri, 2 Mar 2018 14:25:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8F=AA=E6=A0=B9=E6=8D=AE=E6=A0=B8?= =?UTF-8?q?=E5=BF=83=E5=AD=97=E7=AC=A6=E6=AF=94=E8=BE=83=20feat:=20?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=88=97=E8=A1=A8=E5=92=8C=E6=A0=87=E9=A2=98?= =?UTF-8?q?=E7=AD=89=E4=B8=8D=E5=BF=85=E7=B4=A7=E8=B7=9F=E6=8D=A2=E8=A1=8C?= =?UTF-8?q?=E7=AC=A6=E7=9A=84=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aio/tools/translator/translate.spec.ts | 15 ++++++++++--- aio/tools/translator/translate.ts | 29 +++++++++++++------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/aio/tools/translator/translate.spec.ts b/aio/tools/translator/translate.spec.ts index c6fcb0d5de..261eaf934e 100644 --- a/aio/tools/translator/translate.spec.ts +++ b/aio/tools/translator/translate.spec.ts @@ -1,21 +1,30 @@ import { expect } from 'chai'; import { dirs } from './dirs'; -import { dict, lookup, translate } from './translate'; +import { dict, kernelText, lookup, normalizeLines, translate } from './translate'; describe('根据字典进行翻译', () => { it('忽略明显错误的条目', function () { expect(dict.filter(entry => /^
{ expect(lookup('# Forms')[0].translation).eql('# 表单'); }); + it('把“- * 1. #”等处理成空行分隔的格式,以便处理', function () { + const lines = normalizeLines('1. abc\n11. def\n'); + expect(lines).eql('1. abc\n\n11. def\n'); + }); + it('自动根据字典翻译单个文件', function () { const fs = require('fs'); - const content = fs.readFileSync(dirs.content + 'guide/forms.md', 'utf-8'); + 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'); }); - }); diff --git a/aio/tools/translator/translate.ts b/aio/tools/translator/translate.ts index b3056a4763..c2fc3df6e0 100644 --- a/aio/tools/translator/translate.ts +++ b/aio/tools/translator/translate.ts @@ -4,22 +4,29 @@ import { DictEntry } from './dict-entry'; const dict1 = require('./dict-1.json') as DictEntry[]; const dict2 = require('./dict-2.json') as DictEntry[]; const dict3 = require('./dict-3.json') as DictEntry[]; -export const dict = dict1.concat(dict2).concat(dict3).filter(entry => !/^\s*
!/^\s*
filename.test(entry.sourceFile)).filter(entry => entry.original === english), 'translation'); + let entries = dict3 + .filter(entry => filename.test(entry.sourceFile)) + .filter(entry => kernelText(entry.original) === kernelText(english)); + return _.uniqBy(entries, 'translation'); +} + +export function kernelText(text: string): string { + return text.replace(/[\s\n]+/g, ' ').trim(); } export function translate(content: string): string[] { - const lines = content.split(/\n+\s*\n+/); + const lines = normalizeLines(content) + .split(/\n+\s*\n+/); return lines - .map(splitList) - .map(flatten) .map(line => { if (!line.trim()) { return line; } - const translations = lookup(line.trim(), /forms.md$/); + const translations = lookup(line, /forms.md$/); const indent = indentOf(line); const padding = repeat(indent); if (translations.length === 0) { @@ -53,12 +60,6 @@ function repeat(indent: number): string { return result; } -function splitList(line: string): string[] { - const subLines = line.split(/\n/); - // 把 markdown 里面不必空行的元素都拆开 - return [line]; -} - -export function flatten(arr: T[]): T { - return Array.prototype.concat.apply([], arr); +export function normalizeLines(text: string): string { + return text.replace(/(?=\n *(\d+\.|-|\*|#|<) )\n/g, '\n\n'); }