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

View File

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