Skip to content

Commit

Permalink
Improve language logic matching
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Oct 19, 2023
1 parent 857ff05 commit 90d7280
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 10 additions & 1 deletion web/src/context/l10n.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,16 @@ function findSupportedLanguage(languages) {
const supported = Object.keys(cockpit.manifests.agama?.locales || {});

for (const candidate of languages) {
const match = supported.find(s => s.startsWith(candidate));
const [language, country] = candidate.split("-");

const match = supported.find(s => {
const [supportedLanguage, supportedCountry] = s.split("-");
if (language === supportedLanguage) {
return country === undefined || country === supportedCountry;
} else {
return false;
}
});
if (match) return match;
}
}
Expand Down
6 changes: 4 additions & 2 deletions web/src/context/l10n.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jest.mock("~/lib/cockpit", () => ({
manifests: {
agama: {
locales: {
"es-ar": "Español (Argentina)",
"cs-cz": "čeština",
"en-us": "English (US)",
"es-es": "Español"
Expand All @@ -59,6 +60,7 @@ const TranslatedContent = () => {
"cs-cz": "ahoj",
"en-us": "hello",
"es-es": "hola",
"es-ar": "hola!",
};

const regexp = /CockpitLang=([^;]+)/;
Expand Down Expand Up @@ -176,7 +178,7 @@ describe("L10nProvider", () => {
window.navigator = { languages: ["es", "cs-cz"] };
});

it("sets the first language the matches", async () => {
it("sets the first which language matches", async () => {
render(
<InstallerClientProvider client={client}>
<L10nProvider><TranslatedContent /></L10nProvider>
Expand All @@ -189,7 +191,7 @@ describe("L10nProvider", () => {
<L10nProvider><TranslatedContent /></L10nProvider>
</InstallerClientProvider>
);
await waitFor(() => screen.getByText("hola"));
await waitFor(() => screen.getByText("hola!"));
});
});
});
Expand Down

0 comments on commit 90d7280

Please sign in to comment.