Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding translations #49

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/fluent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,14 @@ export class Fluent {
);
// Find the best match for the specified locale
const matchedLocales = negotiateLanguages(locales, availableLocales);
// For matched locales, find the first bundle they're in.
const matchedBundles = matchedLocales.map((locale) => {
return bundles.find((bundle) => bundle.locales.includes(locale));
}).filter((bundle) => bundle !== undefined) as FluentBundle[];
// For matched locales, find the bundles they're in.
const matchedBundles = matchedLocales.reduce<FluentBundle[]>(
(acc, locale) => [
...acc,
...bundles.filter((bundle) => bundle.locales.includes(locale)),
],
[],
);

// Add the default bundle to the end, so it'll be used if other bundles fails.
if (this.defaultBundle) matchedBundles.push(this.defaultBundle);
Expand Down
28 changes: 28 additions & 0 deletions tests/fluent_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,31 @@ Deno.test("warnings", async (t) => {
},
);
});

Deno.test("multiple bundles", async () => {
const fluent = new Fluent({
warningHandler: () => {},
});

await fluent.addTranslation({ locales: "en", source: "hi = hello" });
await fluent.addTranslation({
locales: "en",
source: "planet = world\nhi = error",
});
await fluent.addTranslation({ locales: "en", source: "less = more" });

await fluent.addTranslation({ locales: "it", source: "hi = ciao" });
await fluent.addTranslation({
locales: "it",
source: "planet = mondo\nhi = errore",
});
await fluent.addTranslation({ locales: "it", source: "less = più" });

assertEquals(fluent.translate("en", "hi"), "hello");
assertEquals(fluent.translate("en", "planet"), "world");
assertEquals(fluent.translate("en", "less"), "more");

assertEquals(fluent.translate("it", "hi"), "ciao");
assertEquals(fluent.translate("it", "planet"), "mondo");
assertEquals(fluent.translate("it", "less"), "più");
});