encapsulate translate function creation
This commit is contained in:
parent
7a237b9b68
commit
914394054e
1 changed files with 16 additions and 14 deletions
|
@ -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;
|
||||
}
|
||||
})();
|
||||
\``;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue