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) {
if (typeof(msg) !== 'string' && !msg.val && msg.attrs) { var msg = ctx.getMessage(id);
msg = msg.attrs.title || msg.attrs.alt if (typeof(msg) !== 'string' && !msg.val && msg.attrs) {
msg = msg.attrs.title || msg.attrs.alt
}
return ctx.format(msg, data);
} }
return fluentContext.format(msg, data); if (typeof window === 'undefined') {
} module.exports = translate;
if (typeof window === 'undefined') { }
module.exports = translate; else {
} window.translate = translate;
else { }
window.translate = translate; })();
}
\``; \``;
}; };