UNPKG

@restorecommerce/handlebars-helperized

Version:

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

21 lines 936 B
import hbs from 'handlebars'; const tripleStache = /\{\{\{\s*(.*?)\s*\}\}\}/g; const doubleStache = /\{\{\s*(.*?)\s*\}\}/g; const localizationHandlebarsExtension = (opts) => { hbs.registerHelper('t', (key, hash) => { const locale = opts.locale; let result = opts.texts[key] ?? key; result = (typeof result === 'object') ? result[locale] : result; hash = hash?.hash ?? hash; if (!result) return `Missing translation for ${key}`; return result.replace(doubleStache, (i, match) => { return hash?.[match]?.value ?? hash?.[match]?.default ?? hash?.[match] ?? `{{${match}}}`; }).replace(tripleStache, (i, match) => { // TODO: escaping return hash?.[match]?.value ?? hash?.[match]?.default ?? hash?.[match] ?? `{{{${match}}}}`; }); }); }; export { localizationHandlebarsExtension }; //# sourceMappingURL=l10n-helpers.js.map