Skip to content

Commit

Permalink
naming
Browse files Browse the repository at this point in the history
  • Loading branch information
HalvorHaugan committed Nov 20, 2024
1 parent 4c75955 commit 3067857
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions @navikt/core/react/src/util/i18n/locales.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,45 @@ import en from "./locales/en";
import nb from "./locales/nb";
import nn from "./locales/nn";

type NestedValue = string | Record<string, string | Record<string, string>>;
type NestedObject = Record<string, string | Record<string, string>>;

function checkValues(obj: Translations | NestedValue) {
Object.entries(obj).forEach(([key, value]) => {
if (key === "dateLocale") {
return;
}
if (typeof value === "object") {
checkValues(value);
} else {
expect(value).toBeTypeOf("string");
expect(value).not.toBe("");
}
});
function checkValues(obj: Translations | NestedObject) {
Object.entries(obj).forEach(
([key, value]: [string, string | NestedObject]) => {
if (key === "dateLocale") {
return;
}
if (typeof value === "object") {
checkValues(value);
} else {
expect(value).toBeTypeOf("string");
expect(value).not.toBe("");
}
},
);
}

function checkPlaceholders(
baseTrans: Translations | NestedValue, // Translations to check against
trans: Translations | NestedValue, // Translations to check
function checkTranslationPlaceholders(
baseTranslations: Translations | NestedObject, // Translations to check against
translations: Translations | NestedObject, // Translations to check
) {
Object.entries(baseTrans).forEach(([key, baseVal]: [string, NestedValue]) => {
if (key === "dateLocale") {
return;
}
if (typeof baseVal === "object") {
checkPlaceholders(baseVal, trans[key]);
} else {
const correctPlaceholders = baseVal.match(/{[^}]*}/g) || [];
const transPlaceholders = trans[key].match(/{[^}]*}/g) || [];
expect(
transPlaceholders.sort(),
`Wrong placeholders in "${trans[key]}" (key=${key})`,
).toEqual(correctPlaceholders.sort());
}
});
Object.entries(baseTranslations).forEach(
([key, baseVal]: [string, string | NestedObject]) => {
if (key === "dateLocale") {
return;
}
if (typeof baseVal === "object") {
checkTranslationPlaceholders(baseVal, translations[key]);
} else {
const correctPlaceholders = baseVal.match(/{[^}]*}/g) || [];
const transPlaceholders = translations[key].match(/{[^}]*}/g) || [];
expect(
transPlaceholders.sort(),
`Wrong placeholders in "${translations[key]}" (key=${key})`,
).toEqual(correctPlaceholders.sort());
}
},
);
}

describe("Locale", () => {
Expand All @@ -48,7 +52,7 @@ describe("Locale", () => {
test("EN should have no empty strings", () => checkValues(en));

test("NN should have the same placeholders as NB", () =>
checkPlaceholders(nb, nn));
checkTranslationPlaceholders(nb, nn));
test("EN should have the same placeholders as NB", () =>
checkPlaceholders(nb, en));
checkTranslationPlaceholders(nb, en));
});

0 comments on commit 3067857

Please sign in to comment.