UNPKG

@restorecommerce/handlebars-helperized

Version:

Opinionated handlebars based templating engine for rendering e-mail like content

21 lines (18 loc) 799 B
const tripleStache = /\{\{\{\s*(.*?)\s*\}\}\}/g; const doubleStache = /\{\{\s*(.*?)\s*\}\}/g; let localizationHandlebarsExtension = (hbs: any, opts: any) => { hbs.registerHelper('t', (key: string, hash = {} as any) => { const locale = opts.locale; let result = opts.texts[key] || key; result = (typeof result === 'object') ? result[locale] : result; if (!result) return 'Missing translation for ' + key; const data = hash.data.root; return result.replace(doubleStache, (i: any, match: any) => { return data[match] ? data[match] : ('{{' + match + '}}'); }).replace(tripleStache, (i: any, match: any) => { // TODO: escaping return data[match] ? data[match] : ('{{{' + match + '}}}'); }); }); }; module.exports = localizationHandlebarsExtension;