wuchale
Version:
Protobuf-like i18n from normal code
56 lines • 1.66 kB
JavaScript
export function defaultHeuristic(text, details) {
if (text.search(/\p{L}/u) === -1) {
return false;
}
if (details.scope === 'markup') {
return true;
}
// script and attribute
// only allow non lower-case English letter beginnings
if (!/\p{L}/u.test(text[0]) || /[a-z]/.test(text[0])) {
return false;
}
if (details.scope !== 'script') {
return true;
}
if (details.declaring === 'expression' && !details.funcName) {
return false;
}
return !details.call?.startsWith('console.');
}
// only allow inside function definitions for script scope
export const defaultHeuristicFuncOnly = (text, details) => {
return defaultHeuristic(text, details) && (details.scope !== 'script' || details.funcName != null);
};
export const defaultGenerateLoadID = (filename) => filename.replace(/[^a-zA-Z0-9_]+/g, '_');
export class NestText {
text; // array for plurals
plural = false;
scope;
context;
constructor(txt, scope, context) {
if (typeof txt === 'string') {
this.text = [txt];
}
else {
this.text = txt;
}
this.scope = scope;
this.context = context ?? null;
}
toKey = () => `${this.text.slice(0, 2).join('\n')}\n${this.context ?? ''}`.trim();
}
export class IndexTracker {
indices = {};
nextIndex = 0;
get = (txt) => {
if (txt in this.indices) {
return this.indices[txt];
}
const index = this.nextIndex;
this.indices[txt] = index;
this.nextIndex++;
return index;
};
}
//# sourceMappingURL=adapters.js.map