1
0
mirror of https://gitlab.com/timvisee/send.git synced 2024-11-09 12:42:44 +01:00
send/app/locale.js
2022-07-26 11:03:10 +00:00

27 lines
781 B
JavaScript

import { FluentBundle, FluentResource } from '@fluent/bundle';
function makeBundle(locale, ftl) {
const bundle = new FluentBundle(locale, { useIsolating: false });
bundle.addResource(new FluentResource(ftl));
return bundle;
}
export async function getTranslator(locale) {
const bundles = [];
const { default: en } = await import('../public/locales/en-US/send.ftl');
if (locale !== 'en-US') {
const { default: ftl } = await import(
`../public/locales/${locale}/send.ftl`
);
bundles.push(makeBundle(locale, ftl));
}
bundles.push(makeBundle('en-US', en));
return function(id, data) {
for (let bundle of bundles) {
if (bundle.hasMessage(id)) {
return bundle.formatPattern(bundle.getMessage(id).value, data);
}
}
};
}