locale.js 781 B

1234567891011121314151617181920212223242526
  1. import { FluentBundle, FluentResource } from '@fluent/bundle';
  2. function makeBundle(locale, ftl) {
  3. const bundle = new FluentBundle(locale, { useIsolating: false });
  4. bundle.addResource(new FluentResource(ftl));
  5. return bundle;
  6. }
  7. export async function getTranslator(locale) {
  8. const bundles = [];
  9. const { default: en } = await import('../public/locales/en-US/send.ftl');
  10. if (locale !== 'en-US') {
  11. const { default: ftl } = await import(
  12. `../public/locales/${locale}/send.ftl`
  13. );
  14. bundles.push(makeBundle(locale, ftl));
  15. }
  16. bundles.push(makeBundle('en-US', en));
  17. return function(id, data) {
  18. for (let bundle of bundles) {
  19. if (bundle.hasMessage(id)) {
  20. return bundle.formatPattern(bundle.getMessage(id).value, data);
  21. }
  22. }
  23. };
  24. }