How to use @fluent/bundle - 10 common examples

To help you get started, we’ve selected a few @fluent/bundle examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / fluent__bundle / fluent__bundle-tests.ts View on Github external
import { FluentBundle, FluentDateTime, FluentError, FluentNumber, FluentResource, Scope } from '@fluent/bundle';
import * as compat from '@fluent/bundle/compat';

// FluentBundle examples:
const bundle = new FluentBundle(['en-US']);
const compatBundle = new compat.FluentBundle(['en-US']);

// FluentResource examples:
const resource = new FluentResource(`test=Some other message with args arg1={$arg1} and arg2={$arg2}`);
bundle.addResource(resource, { allowOverrides: true });
const msg = bundle.getMessage('test');
if (msg && msg.value) {
    const args = {
        $arg1: "#1",
        $arg2: "#2"
    };
    const errors: Error[] = [];
    const formatted: string = bundle.formatPattern(msg.value, args, errors);
    console.log(formatted);
}
github DefinitelyTyped / DefinitelyTyped / types / fluent__bundle / fluent__bundle-tests.ts View on Github external
import { FluentBundle, FluentDateTime, FluentError, FluentNumber, FluentResource, Scope } from '@fluent/bundle';
import * as compat from '@fluent/bundle/compat';

// FluentBundle examples:
const bundle = new FluentBundle(['en-US']);
const compatBundle = new compat.FluentBundle(['en-US']);

// FluentResource examples:
const resource = new FluentResource(`test=Some other message with args arg1={$arg1} and arg2={$arg2}`);
bundle.addResource(resource, { allowOverrides: true });
const msg = bundle.getMessage('test');
if (msg && msg.value) {
    const args = {
        $arg1: "#1",
        $arg2: "#2"
    };
    const errors: Error[] = [];
    const formatted: string = bundle.formatPattern(msg.value, args, errors);
    console.log(formatted);
}

// Fluent type examples:
const num = new FluentNumber(6);
const err = new FluentError('argh');
const dt = new FluentDateTime(new Date(2000, 0, 1));
github DefinitelyTyped / DefinitelyTyped / types / fluent__bundle / fluent__bundle-tests.ts View on Github external
import { FluentBundle, FluentDateTime, FluentError, FluentNumber, FluentResource, Scope } from '@fluent/bundle';
import * as compat from '@fluent/bundle/compat';

// FluentBundle examples:
const bundle = new FluentBundle(['en-US']);
const compatBundle = new compat.FluentBundle(['en-US']);

// FluentResource examples:
const resource = new FluentResource(`test=Some other message with args arg1={$arg1} and arg2={$arg2}`);
bundle.addResource(resource, { allowOverrides: true });
const msg = bundle.getMessage('test');
if (msg && msg.value) {
    const args = {
        $arg1: "#1",
        $arg2: "#2"
    };
    const errors: Error[] = [];
    const formatted: string = bundle.formatPattern(msg.value, args, errors);
    console.log(formatted);
}

// Fluent type examples:
github DefinitelyTyped / DefinitelyTyped / types / fluent__bundle / fluent__bundle-tests.ts View on Github external
// FluentResource examples:
const resource = new FluentResource(`test=Some other message with args arg1={$arg1} and arg2={$arg2}`);
bundle.addResource(resource, { allowOverrides: true });
const msg = bundle.getMessage('test');
if (msg && msg.value) {
    const args = {
        $arg1: "#1",
        $arg2: "#2"
    };
    const errors: Error[] = [];
    const formatted: string = bundle.formatPattern(msg.value, args, errors);
    console.log(formatted);
}

// Fluent type examples:
const num = new FluentNumber(6);
const err = new FluentError('argh');
const dt = new FluentDateTime(new Date(2000, 0, 1));

// Scope examples:
class DummyScope implements Scope {
    cloneForTermReference(args: object): Scope {
        return this;
    }
    reportError(error: string): void {
    }
    memoizeIntlObject(
        ctor: new (locales: string[], opts: OptsType) => ObjectType,
        opts: OptsType,
    ): ObjectType {
        return new ctor([], opts);
    }
github DefinitelyTyped / DefinitelyTyped / types / fluent__bundle / fluent__bundle-tests.ts View on Github external
bundle.addResource(resource, { allowOverrides: true });
const msg = bundle.getMessage('test');
if (msg && msg.value) {
    const args = {
        $arg1: "#1",
        $arg2: "#2"
    };
    const errors: Error[] = [];
    const formatted: string = bundle.formatPattern(msg.value, args, errors);
    console.log(formatted);
}

// Fluent type examples:
const num = new FluentNumber(6);
const err = new FluentError('argh');
const dt = new FluentDateTime(new Date(2000, 0, 1));

// Scope examples:
class DummyScope implements Scope {
    cloneForTermReference(args: object): Scope {
        return this;
    }
    reportError(error: string): void {
    }
    memoizeIntlObject(
        ctor: new (locales: string[], opts: OptsType) => ObjectType,
        opts: OptsType,
    ): ObjectType {
        return new ctor([], opts);
    }
}
const scope = new DummyScope();
github DefinitelyTyped / DefinitelyTyped / types / fluent__bundle / fluent__bundle-tests.ts View on Github external
const resource = new FluentResource(`test=Some other message with args arg1={$arg1} and arg2={$arg2}`);
bundle.addResource(resource, { allowOverrides: true });
const msg = bundle.getMessage('test');
if (msg && msg.value) {
    const args = {
        $arg1: "#1",
        $arg2: "#2"
    };
    const errors: Error[] = [];
    const formatted: string = bundle.formatPattern(msg.value, args, errors);
    console.log(formatted);
}

// Fluent type examples:
const num = new FluentNumber(6);
const err = new FluentError('argh');
const dt = new FluentDateTime(new Date(2000, 0, 1));

// Scope examples:
class DummyScope implements Scope {
    cloneForTermReference(args: object): Scope {
        return this;
    }
    reportError(error: string): void {
    }
    memoizeIntlObject(
        ctor: new (locales: string[], opts: OptsType) => ObjectType,
        opts: OptsType,
    ): ObjectType {
        return new ctor([], opts);
    }
}
github coralproject / talk / src / core / client / framework / testHelpers / createFluentBundle.ts View on Github external
function createFluentBundle(
  target: string,
  pathToLocale: string
): FluentBundle {
  // `useIsolating: false` will remove bidi characters.
  // https://github.com/projectfluent/fluent.js/wiki/Unicode-Isolation
  // We should be able to use `<bdi>` tags instead to support rtl languages.
  const bundle = new FluentBundle("en-US", { functions, useIsolating: false });
  const files = fs.readdirSync(pathToLocale);
  const prefixes = commonPrefixes.concat(target);
  files.forEach(f =&gt; {
    prefixes.forEach(prefix =&gt; {
      if (f.startsWith(prefix)) {
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        bundle.addResource(
          new FluentResource(require(path.resolve(pathToLocale, f)))
        );
      }
    });
  });
  return decorateErrorWhenMissing(bundle);
}
</bdi>
github coralproject / talk / src / core / client / framework / testHelpers / createFluentBundle.ts View on Github external
prefixes.forEach(prefix => {
      if (f.startsWith(prefix)) {
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        bundle.addResource(
          new FluentResource(require(path.resolve(pathToLocale, f)))
        );
      }
    });
  });
github projectfluent / fluent-web / src / index.js View on Github external
async function createContext(locale, resourceIds) {
  const ctx = new FluentBundle([locale]);

  // First fetch all resources
  const resources = await Promise.all(
    resourceIds.map(id => fetchResource(locale, id)),
  );

  // Then apply them preserving order
  for (const resource of resources) {
    ctx.addResource(resource);
  }
  return ctx;
}
github coralproject / talk / src / core / client / framework / lib / i18n / generateBundles.ts View on Github external
export default async function generateBundles(
  locales: ReadonlyArray,
  data: LocalesData
): Promise {
  const promises = [];

  for (const locale of locales) {
    // `useIsolating: false` will remove bidi characters.
    // https://github.com/projectfluent/fluent.js/wiki/Unicode-Isolation
    // We should be able to use `<bdi>` tags instead to support rtl languages.
    const bundle = new FluentBundle(locale, { functions, useIsolating: false });
    if (locale in data.bundled) {
      bundle.addResource(new FluentResource(data.bundled[locale]));
      promises.push(decorateWarnMissing(bundle));
    } else if (locale in data.loadables) {
      const content = await data.loadables[locale]();
      bundle.addResource(new FluentResource(content));
      promises.push(decorateWarnMissing(bundle));
    } else {
      throw Error(`Locale ${locale} not available`);
    }
  }

  return await Promise.all(promises);
}
</bdi>