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);
}
// Fluent type examples:
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>
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>
// Load all the translation files for each of the folders.
for (const folder of folders) {
// Parse out the language code.
const locale = path.basename(folder);
if (!isLanguageCode(locale)) {
throw new Error(`invalid language code: ${locale}`);
}
// Now we have a language code.
const bundle: 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.
this.bundles[locale] ||
new FluentBundle(locale, { useIsolating: false });
// Load all the translations in the folder.
const files = await fs.readdir(path.join(localesFolder, folder));
for (const file of files) {
const filePath = path.join(localesFolder, folder, file);
logger.debug({ locale, filePath }, "loading messages for locale");
const messages = await fs.readFile(filePath, "utf8");
bundle.addResource(new FluentResource(messages));
}
this.bundles[locale] = bundle;
}
}</bdi>