reimplemented l10n using dynamic import() (#1012)
this should greatly reduce the complexity of the l10n code and build pipeline and eliminate the most common error seen in sentry logs (no translate function)
This commit is contained in:
parent
5afa4e5c9b
commit
1e62aa976d
28 changed files with 145 additions and 280 deletions
26
server/locale.js
Normal file
26
server/locale.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { FluentBundle } = require('fluent');
|
||||
const localesPath = path.resolve(__dirname, '../public/locales');
|
||||
const locales = fs.readdirSync(localesPath);
|
||||
|
||||
function makeBundle(locale) {
|
||||
const bundle = new FluentBundle(locale, { useIsolating: false });
|
||||
bundle.addMessages(
|
||||
fs.readFileSync(path.resolve(localesPath, locale, 'send.ftl'))
|
||||
);
|
||||
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)) {
|
||||
return bundle.format(bundle.getMessage(id), data);
|
||||
}
|
||||
return defaultBundle.format(defaultBundle.getMessage(id), data);
|
||||
};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue