24
1
Fork 0

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');
var fluent = require('fluent/compat');
}
var fluentContext = new fluent.MessageContext('${locale}', {useIsolating: false});
fluentContext._messages = new Map(${toJSON(merged)});
function translate(id, data) {
var msg = fluentContext.getMessage(id);
if (typeof(msg) !== 'string' && !msg.val && msg.attrs) {
msg = msg.attrs.title || msg.attrs.alt
(function () {
var ctx = new fluent.MessageContext('${locale}', {useIsolating: false});
ctx._messages = new Map(${toJSON(merged)});
function translate(id, data) {
var msg = ctx.getMessage(id);
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;
}
else {
window.translate = translate;
}
if (typeof window === 'undefined') {
module.exports = translate;
}
else {
window.translate = translate;
}
})();
\``;
};