2022-07-26 11:03:10 +00:00
|
|
|
import { FluentBundle, FluentResource } from '@fluent/bundle';
|
2018-11-20 14:50:59 +00:00
|
|
|
|
|
|
|
function makeBundle(locale, ftl) {
|
|
|
|
const bundle = new FluentBundle(locale, { useIsolating: false });
|
2022-07-26 11:03:10 +00:00
|
|
|
bundle.addResource(new FluentResource(ftl));
|
2018-11-20 14:50:59 +00:00
|
|
|
return bundle;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getTranslator(locale) {
|
|
|
|
const bundles = [];
|
|
|
|
const { default: en } = await import('../public/locales/en-US/send.ftl');
|
|
|
|
if (locale !== 'en-US') {
|
2019-07-11 17:47:42 +00:00
|
|
|
const { default: ftl } = await import(
|
|
|
|
`../public/locales/${locale}/send.ftl`
|
|
|
|
);
|
2018-11-20 14:50:59 +00:00
|
|
|
bundles.push(makeBundle(locale, ftl));
|
|
|
|
}
|
|
|
|
bundles.push(makeBundle('en-US', en));
|
|
|
|
return function(id, data) {
|
|
|
|
for (let bundle of bundles) {
|
|
|
|
if (bundle.hasMessage(id)) {
|
2022-07-26 11:03:10 +00:00
|
|
|
return bundle.formatPattern(bundle.getMessage(id).value, data);
|
2018-11-20 14:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|