2018-11-20 15:50:59 +01:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2022-07-26 13:03:10 +02:00
|
|
|
const { FluentBundle, FluentResource } = require('@fluent/bundle');
|
2018-11-20 15:50:59 +01:00
|
|
|
const localesPath = path.resolve(__dirname, '../public/locales');
|
|
|
|
const locales = fs.readdirSync(localesPath);
|
|
|
|
|
|
|
|
function makeBundle(locale) {
|
|
|
|
const bundle = new FluentBundle(locale, { useIsolating: false });
|
2022-07-26 13:03:10 +02:00
|
|
|
bundle.addResource(
|
|
|
|
new FluentResource(
|
|
|
|
fs.readFileSync(path.resolve(localesPath, locale, 'send.ftl'), 'utf8')
|
|
|
|
)
|
2018-11-20 15:50:59 +01:00
|
|
|
);
|
|
|
|
return [locale, bundle];
|
|
|
|
}
|
|
|
|
|
|
|
|
const bundles = new Map(locales.map(makeBundle));
|
|
|
|
|
|
|
|
module.exports = function getTranslator(locale) {
|
|
|
|
const defaultBundle = bundles.get('en-US');
|
|
|
|
const bundle = bundles.get(locale) || defaultBundle;
|
|
|
|
return function(id, data) {
|
|
|
|
if (bundle.hasMessage(id)) {
|
2022-07-26 13:03:10 +02:00
|
|
|
return bundle.formatPattern(bundle.getMessage(id).value, data);
|
2018-11-20 15:50:59 +01:00
|
|
|
}
|
2022-07-26 13:03:10 +02:00
|
|
|
return defaultBundle.formatPattern(
|
|
|
|
defaultBundle.getMessage(id).value,
|
|
|
|
data
|
|
|
|
);
|
2018-11-20 15:50:59 +01:00
|
|
|
};
|
|
|
|
};
|