UNPKG

next-translate-scanner

Version:

Scan next-translate code for translations and update json files.

91 lines (90 loc) 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ACCENTED_MAP = { a: 'ȧ', A: 'Ȧ', b: 'ƀ', B: 'Ɓ', c: 'ƈ', C: 'Ƈ', d: 'ḓ', D: 'Ḓ', e: 'ḗ', E: 'Ḗ', f: 'ƒ', F: 'Ƒ', g: 'ɠ', G: 'Ɠ', h: 'ħ', H: 'Ħ', i: 'ī', I: 'Ī', j: 'ĵ', J: 'Ĵ', k: 'ķ', K: 'Ķ', l: 'ŀ', L: 'Ŀ', m: 'ḿ', M: 'Ḿ', n: 'ƞ', N: 'Ƞ', o: 'ǿ', O: 'Ǿ', p: 'ƥ', P: 'Ƥ', q: 'ɋ', Q: 'Ɋ', r: 'ř', R: 'Ř', s: 'ş', S: 'Ş', t: 'ŧ', T: 'Ŧ', v: 'ṽ', V: 'Ṽ', u: 'ŭ', U: 'Ŭ', w: 'ẇ', W: 'Ẇ', x: 'ẋ', X: 'Ẋ', y: 'ẏ', Y: 'Ẏ', z: 'ẑ', Z: 'Ẑ' }; const defaultOptions = { prefix: '', postfix: '', map: ACCENTED_MAP, elongate: true }; const pseudoLocalizeString = (string, options = {}) => { const mergedOptions = { ...defaultOptions, ...options, }; let pseudoLocalizedText = ''; for (const character of string) { if (mergedOptions.map[character]) { const cl = character.toLowerCase(); // duplicate "a", "e", "o" and "u" to emulate ~30% longer text if (mergedOptions.elongate && (cl === 'a' || cl === 'e' || cl === 'o' || cl === 'u')) { pseudoLocalizedText += mergedOptions.map[character] + mergedOptions.map[character]; } else { pseudoLocalizedText += mergedOptions.map[character]; } } else { pseudoLocalizedText += character; } } // If this string is from the DOM, it should already contain the pre- and postfix if (pseudoLocalizedText.startsWith(mergedOptions.prefix) && pseudoLocalizedText.endsWith(mergedOptions.postfix)) { return pseudoLocalizedText; } return mergedOptions.prefix + pseudoLocalizedText + mergedOptions.postfix; }; exports.default = pseudoLocalizeString;