attranslate
Version:
Text Translator for Websites and Apps
47 lines (46 loc) • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.instantiateTMatcher = exports.reInsertInterpolations = exports.replaceInterpolations = exports.getTMatcherList = exports.matchNothing = exports.xmlStyleReplacer = void 0;
const icu_1 = require("./icu");
const i18next_1 = require("./i18next");
const sprintf_1 = require("./sprintf");
const xmlStyleReplacer = (index) => `<span>${index}</span>`;
exports.xmlStyleReplacer = xmlStyleReplacer;
const xmlLeftTag = "<span>";
const xmlRightTag = "</span>";
const spaceXmlRightTag = "</ span>";
const matchNothing = () => [];
exports.matchNothing = matchNothing;
function getTMatcherList() {
return Object.keys(matcherMap);
}
exports.getTMatcherList = getTMatcherList;
const matcherMap = {
none: exports.matchNothing,
icu: icu_1.matchIcu,
i18next: i18next_1.matchI18Next,
sprintf: sprintf_1.matchSprintf,
};
const replaceInterpolations = (input, matcher = exports.matchNothing, replacer = exports.xmlStyleReplacer) => {
const replacements = matcher(input, replacer);
const clean = replacements.reduce((acc, cur) => acc.replace(cur.from, cur.to), input);
return { clean, replacements };
};
exports.replaceInterpolations = replaceInterpolations;
function replaceAll(string, search, replace) {
return string.split(search).join(replace);
}
const reInsertInterpolations = (clean, replacements) => {
const c1 = replaceAll(clean, xmlLeftTag + " ", xmlLeftTag);
const c2 = replaceAll(c1, spaceXmlRightTag, xmlRightTag);
const c3 = replaceAll(c2, " " + xmlRightTag, xmlRightTag);
return replacements.reduce((acc, cur) => acc.replace(cur.to, cur.from), c3);
};
exports.reInsertInterpolations = reInsertInterpolations;
function instantiateTMatcher(matcher) {
if (typeof matcherMap[matcher] === "undefined") {
throw new Error(`matcher ${matcher} doesn't exist.`);
}
return matcherMap[matcher];
}
exports.instantiateTMatcher = instantiateTMatcher;