Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
}
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));
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:
// 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);
}
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();
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);
}
}
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 => {
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)))
);
}
});
});
return decorateErrorWhenMissing(bundle);
}
</bdi>
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)))
);
}
});
});
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;
}
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>