2018-03-03 12:00:01 +08:00
|
|
|
import * as fs from 'fs';
|
2018-03-01 22:19:59 +08:00
|
|
|
import * as _ from 'lodash';
|
|
|
|
import { DictEntry } from './dict-entry';
|
2018-03-03 12:00:01 +08:00
|
|
|
import { dirs } from './dirs';
|
|
|
|
import { listMarkdownFiles } from './extractor';
|
2018-03-24 16:33:17 +08:00
|
|
|
import { exactlyTest, extractOriginalContent, indentOf, hasInlineText, kernelText, normalizeLines, repeat } from './utils';
|
2018-03-15 16:40:11 +08:00
|
|
|
|
|
|
|
// TODO: 改用 markdown 解析器实现
|
2018-03-01 22:19:59 +08:00
|
|
|
|
2018-03-07 15:55:33 +08:00
|
|
|
export const dict = require('./dict-latest.json') as DictEntry[];
|
2018-03-01 22:19:59 +08:00
|
|
|
|
2018-03-24 16:33:17 +08:00
|
|
|
export function lookup(english: string): DictEntry[] {
|
|
|
|
const englishKernel = kernelText(extractOriginalContent(english));
|
2018-03-03 21:05:33 +08:00
|
|
|
const entries = dict
|
2018-03-24 16:33:17 +08:00
|
|
|
.filter(entry => exactlyTest(entry.key, englishKernel));
|
2018-03-02 14:25:07 +08:00
|
|
|
return _.uniqBy(entries, 'translation');
|
|
|
|
}
|
|
|
|
|
2018-03-24 16:33:17 +08:00
|
|
|
function isPureTag(line: string): boolean {
|
|
|
|
let content = line.trim();
|
|
|
|
return /^<\/?\w+\b[^>]*>$/.test(content) ||
|
|
|
|
/^<(\w+)\b[^>]*>\s*<\/\1>$/.test(content);
|
|
|
|
}
|
|
|
|
|
2018-03-01 22:52:16 +08:00
|
|
|
export function translate(content: string): string[] {
|
2018-03-02 14:25:07 +08:00
|
|
|
const lines = normalizeLines(content)
|
2018-03-24 16:33:17 +08:00
|
|
|
.split(/\n{2,}/);
|
2018-03-01 22:52:16 +08:00
|
|
|
return lines
|
|
|
|
.map(line => {
|
2018-03-24 16:33:17 +08:00
|
|
|
if (!line.trim() || isPureTag(line)) {
|
2018-03-01 22:52:16 +08:00
|
|
|
return line;
|
|
|
|
}
|
2018-03-03 17:47:09 +08:00
|
|
|
const translations = lookup(line);
|
2018-03-24 16:33:17 +08:00
|
|
|
if (translations.length > 0 && hasInlineText(translations[0].translation)) {
|
|
|
|
return translations[0].translation;
|
2018-03-01 22:52:16 +08:00
|
|
|
} else {
|
2018-03-24 16:33:17 +08:00
|
|
|
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' + padding + translations[translations.length - 1].translation;
|
|
|
|
}
|
2018-03-01 22:52:16 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-03-03 12:00:01 +08:00
|
|
|
|
|
|
|
export function translateFile(sourceFile: string, targetFile: string): void {
|
|
|
|
const content = fs.readFileSync(sourceFile, 'utf-8');
|
|
|
|
const result = translate(content);
|
2018-03-24 16:33:17 +08:00
|
|
|
fs.writeFileSync(targetFile, result.join('\n\n') + '\n', 'utf-8');
|
2018-03-03 12:00:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function translateDirectory(sourceDir: string, targetDir: string): void {
|
|
|
|
const files = listMarkdownFiles(sourceDir);
|
|
|
|
files.forEach(fileName => {
|
2018-03-03 17:47:09 +08:00
|
|
|
console.log('translating ...', fileName);
|
2018-03-03 12:00:01 +08:00
|
|
|
translateFile(fileName, fileName.replace(/^.*content-en\//, dirs.content));
|
2018-03-03 17:47:09 +08:00
|
|
|
console.log('translated ', fileName);
|
2018-03-03 12:00:01 +08:00
|
|
|
});
|
|
|
|
}
|