encapsulate translate function creation

This commit is contained in:
Danny Coates 2018-03-02 13:40:29 -08:00
parent 7a237b9b68
commit 914394054e
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
1 changed files with 16 additions and 14 deletions

View File

@ -42,20 +42,22 @@ if (typeof window === 'undefined') {
require('babel-polyfill'); require('babel-polyfill');
var fluent = require('fluent/compat'); var fluent = require('fluent/compat');
} }
var fluentContext = new fluent.MessageContext('${locale}', {useIsolating: false}); (function () {
fluentContext._messages = new Map(${toJSON(merged)}); var ctx = new fluent.MessageContext('${locale}', {useIsolating: false});
function translate(id, data) { ctx._messages = new Map(${toJSON(merged)});
var msg = fluentContext.getMessage(id); function translate(id, data) {
var msg = ctx.getMessage(id);
if (typeof(msg) !== 'string' && !msg.val && msg.attrs) { if (typeof(msg) !== 'string' && !msg.val && msg.attrs) {
msg = msg.attrs.title || msg.attrs.alt msg = msg.attrs.title || msg.attrs.alt
} }
return fluentContext.format(msg, data); return ctx.format(msg, data);
} }
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
module.exports = translate; module.exports = translate;
} }
else { else {
window.translate = translate; window.translate = translate;
} }
})();
\``; \``;
}; };