@awesome-fe/translate
Version:
Translation utils
82 lines • 3.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TranslationEngine = void 0;
const lodash_1 = require("lodash");
const promise_maker_1 = require("../dom/promise-maker");
const sentence_formatter_1 = require("./sentence-formatter");
const delay_1 = require("../dom/delay");
function isPlainUrl(url) {
return /^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$/.test(url);
}
function inBlackList(text) {
return ['angular', 'number', 'string', 'object'].includes(text.toLowerCase());
}
function isCode(html) {
return html.startsWith('<code') && html.endsWith('</code>') && html.indexOf('</code>') === html.lastIndexOf('</code>');
}
function isCamelCaseName(text) {
return /^[_a-zA-Z]+([A-Z]\w+)+$/.test(text);
}
function isAnchor(html) {
return /^<a id="[^"]*">\s*<\/a>$/.test(html);
}
class TranslationEngine {
batchSize = 50;
_totalBytes = 0;
get totalBytes() {
return this._totalBytes;
}
currentFile;
async setup(currentFile) {
this.currentFile = currentFile;
this._totalBytes = 0;
}
async tearDown() {
console.log('Total bytes: ', this.totalBytes);
}
tasks = [];
// 不要 override 这个方法,只能 override batchTranslate
async translate(original, translation, format) {
if (this.shouldIgnore(original, format)) {
return original;
}
const result$ = new promise_maker_1.PromiseMaker();
const pair = [original, translation];
this.tasks.push({ pair, format, result$ });
return result$.promise;
}
async flush() {
const groups = (0, lodash_1.groupBy)(this.tasks, 'format');
for (let [format, tasks] of Object.entries(groups)) {
const chunks = (0, lodash_1.chunk)(tasks, this.batchSize);
for (let chunk of chunks) {
const pairs = chunk.map(it => it.pair);
this._totalBytes += pairs.map(it => it[0]).join('\n').length;
const translations = await this.batchTranslate((0, lodash_1.cloneDeep)(pairs), format);
chunk.forEach(({ result$ }, index) => {
if (pairs[index][1]) {
result$.resolve(undefined);
}
else {
result$.resolve(translations[index][1]);
}
});
}
}
const tasks = this.tasks.map(({ result$ }) => result$.promise);
return Promise.all(tasks)
.then(() => this.tasks = [])
// add a small delay to ensure that all derived promises(such as Promise.all) are resolved
.then(() => (0, delay_1.delay)(0));
}
shouldIgnore(original, format) {
const text = original?.trim();
if (!text || isPlainUrl(text) || inBlackList(text) || isCamelCaseName(text) || isAnchor(text)) {
return true;
}
const html = sentence_formatter_1.SentenceFormatter.toHtml(original, format).trim().replace(/^<p>(.*)<\/p>$/gs, '$1');
return isCode(html);
}
}
exports.TranslationEngine = TranslationEngine;
//# sourceMappingURL=translation-engine.js.map