Skip to content
Merged
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
6 changes: 3 additions & 3 deletions src/hyperjump-json-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ const isCompatible = (compatibility, versionUnderTest) => {

/** @type (messageId: string, messageParams: MessageParams) => string */
const getMessage = (function () {
const ftl = translations["en-US"];
const resource = new FluentResource(ftl);
const bundle = new FluentBundle("en-US");
const translation = translations["en-US"];
const resource = new FluentResource(translation.ftl);
const bundle = new FluentBundle("en-US", { useIsolating: translation.direction === "rtl" });
bundle.addResource(resource);

const disjunction = new Intl.ListFormat("en-US", { type: "disjunction" });
Expand Down
8 changes: 4 additions & 4 deletions src/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export class Localization {
/** @type (locale: string) => Localization */
static forLocale(locale) {
if (!localizationCache.has(locale)) {
const ftl = translations[locale];
if (!ftl) {
const translation = translations[locale];
if (!translation) {
throw Error(`The ${locale} locale is not supported.`);
}
const resource = new FluentResource(ftl);
const bundle = new FluentBundle(locale);
const resource = new FluentResource(translation.ftl);
const bundle = new FluentBundle(locale, { useIsolating: translation.direction === "rtl" });
bundle.addResource(resource);
localizationCache.set(locale, new Localization(locale, bundle));
}
Expand Down
18 changes: 17 additions & 1 deletion src/localization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import { Localization } from "./localization.js";

describe("Localization", () => {
const fixtureLocale = "fx-TR";
const ltrFixtureLocale = "fx-LT";
const rtlFixtureLocale = "fx-RT";

beforeAll(() => {
translations[fixtureLocale] = `test = unsupported locale`;
translations[fixtureLocale] = { ftl: `test = unsupported locale`, direction: "ltr" };
translations[ltrFixtureLocale] = { ftl: `type-message = Expected a {$expectedTypes}`, direction: "ltr" };
translations[rtlFixtureLocale] = { ftl: `type-message = Expected a {$expectedTypes}`, direction: "rtl" };
});

afterAll(() => {
delete translations[fixtureLocale];
delete translations[ltrFixtureLocale];
delete translations[rtlFixtureLocale];
});

test("unsupported locale", () => {
Expand All @@ -21,4 +27,14 @@ describe("Localization", () => {
const localization = Localization.forLocale(fixtureLocale);
expect(() => localization.getBooleanSchemaErrorMessage()).to.throw(Error);
});

test("ltr locale doesn't isolate placeables", () => {
const localization = Localization.forLocale(ltrFixtureLocale);
expect(localization.getTypeErrorMessage(["string"])).to.equal("Expected a string");
});

test("rtl locale isolates placeables", () => {
const localization = Localization.forLocale(rtlFixtureLocale);
expect(localization.getTypeErrorMessage(["string"])).to.equal("Expected a ⁨string⁩");
});
});
11 changes: 9 additions & 2 deletions src/translations/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import enUS from "./en-US.js";

/** @type Record<string, string> */
/**
* @typedef {{
* ftl: string;
* direction: "ltr" | "rtl";
* }} Translation
*/

/** @type Record<string, Translation> */
export const translations = {
"en-US": enUS
"en-US": { ftl: enUS, direction: "ltr" }
};
Loading