Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

Commit

Permalink
fixed untranslated strings breaking the site
Browse files Browse the repository at this point in the history
  • Loading branch information
dannycoates committed Dec 19, 2017
1 parent 3a7677b commit 0f8c3ca
Show file tree
Hide file tree
Showing 3 changed files with 459 additions and 71 deletions.
27 changes: 23 additions & 4 deletions build/fluent_loader.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
const { MessageContext } = require('fluent');
const fs = require('fs');

function toJSON(map) {
return JSON.stringify(Array.from(map));
}

function merge(m1, m2) {
const result = new Map(m1);
for (const [k, v] of m2) {
result.set(k, v);
}
return result;
}

module.exports = function(source) {
const localeExp = this.options.locale || /([^/]+)\/[^/]+\.ftl$/;
const result = localeExp.exec(this.resourcePath);
const locale = result && result[1];
// pre-parse the ftl
const context = new MessageContext(locale);
context.addMessages(source);
if (!locale) {
throw new Error(`couldn't find locale in: ${this.resourcePath}`);
}
// load default language and "merge" contexts
// TODO: make this configurable
const en_ftl = fs.readFileSync(
require.resolve('../public/locales/en-US/send.ftl'),
'utf8'
);
const en = new MessageContext('en-US');
en.addMessages(en_ftl);
// pre-parse the ftl
const context = new MessageContext(locale);
context.addMessages(source);

const merged = merge(en._messages, context._messages);
return `
module.exports = \`
if (typeof window === 'undefined') {
var fluent = require('fluent');
}
var ctx = new fluent.MessageContext('${locale}', {useIsolating: false});
ctx._messages = new Map(${toJSON(context._messages)});
ctx._messages = new Map(${toJSON(merged)});
function translate(id, data) {
var msg = ctx.getMessage(id);
if (typeof(msg) !== 'string' && !msg.val && msg.attrs) {
Expand Down
Loading

0 comments on commit 0f8c3ca

Please sign in to comment.