feat: 提供根据字典翻译功能
This commit is contained in:
parent
40bd48bb2a
commit
0dbc49c7cc
@ -31,6 +31,11 @@ describe('auto check translations', function () {
|
|||||||
|
|
||||||
expect(lines).eql([]);
|
expect(lines).eql([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('english should not be <div class', function () {
|
||||||
|
const lines = entries.filter(entry => /^ *<div.*/.test(entry.original));
|
||||||
|
expect(lines).eql([]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function isNotImg(entry: DictEntry): boolean {
|
function isNotImg(entry: DictEntry): boolean {
|
||||||
|
@ -1,9 +1,21 @@
|
|||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import { lookup } from './translate';
|
import { dirs } from './dirs';
|
||||||
|
import { dict, lookup, translate } from './translate';
|
||||||
|
|
||||||
|
|
||||||
describe('translate with dict', () => {
|
describe('translate with dict', () => {
|
||||||
|
it('should ignore wrong entry', function () {
|
||||||
|
expect(dict.filter(entry => /^<div/.test(entry.original))).eql([]);
|
||||||
|
});
|
||||||
it('translate original english', () => {
|
it('translate original english', () => {
|
||||||
expect(lookup('# Forms')[0].translation).eql('# 表单');
|
expect(lookup('# Forms')[0].translation).eql('# 表单');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should auto translate based on dict', function () {
|
||||||
|
const fs = require('fs');
|
||||||
|
const content = fs.readFileSync(dirs.content + 'guide/forms.md', 'utf-8');
|
||||||
|
const result = translate(content);
|
||||||
|
fs.writeFileSync(dirs.content + 'guide/forms.md', result.join('\n\n'), 'utf-8');
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -4,8 +4,61 @@ import { DictEntry } from './dict-entry';
|
|||||||
const dict1 = require('./dict-1.json') as DictEntry[];
|
const dict1 = require('./dict-1.json') as DictEntry[];
|
||||||
const dict2 = require('./dict-2.json') as DictEntry[];
|
const dict2 = require('./dict-2.json') as DictEntry[];
|
||||||
const dict3 = require('./dict-3.json') as DictEntry[];
|
const dict3 = require('./dict-3.json') as DictEntry[];
|
||||||
const dict = dict1.concat(dict2).concat(dict3);
|
export const dict = dict1.concat(dict2).concat(dict3).filter(entry => !/^\s*<div/.test(entry.original));
|
||||||
|
|
||||||
export function lookup(english: string, filename: RegExp = /.*/): DictEntry[] {
|
export function lookup(english: string, filename: RegExp = /.*/): DictEntry[] {
|
||||||
return _.uniqBy(dict.filter(entry => filename.test(entry.sourceFile)).filter(entry => entry.original === english), 'translation');
|
return _.uniqBy(dict.filter(entry => filename.test(entry.sourceFile)).filter(entry => entry.original === english), 'translation');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function translate(content: string): string[] {
|
||||||
|
const lines = 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 indent = indentOf(line);
|
||||||
|
const padding = repeat(indent);
|
||||||
|
if (translations.length === 0) {
|
||||||
|
return line;
|
||||||
|
} else if (translations.length === 1) {
|
||||||
|
return line + '\n\n' + padding + translations[0].translation;
|
||||||
|
} else {
|
||||||
|
return line + '\n\n' + translations.map(t => '???\n' + padding + t.translation).join('\n\n');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function indentOf(line): number {
|
||||||
|
let pattern = /^( *)[\s\S]*/;
|
||||||
|
if (!pattern.test(line)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
const leadSpaces = line.replace(pattern, '$1').length;
|
||||||
|
if (/^ *(\d+\.|-|\*) /.test(line)) {
|
||||||
|
return leadSpaces + 3;
|
||||||
|
} else {
|
||||||
|
return leadSpaces;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function repeat(indent: number): string {
|
||||||
|
let result = '';
|
||||||
|
for (let i = 0; i < indent; ++i) {
|
||||||
|
result = result + ' ';
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function splitList(line: string): string[] {
|
||||||
|
const subLines = line.split(/\n/);
|
||||||
|
// 把 markdown 里面不必空行的元素都拆开
|
||||||
|
return [line];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function flatten<T>(arr: T[]): T {
|
||||||
|
return Array.prototype.concat.apply([], arr);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user