-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #796 from imobachgs/language-switcher
Allow changing the language for the web UI
- Loading branch information
Showing
19 changed files
with
621 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
------------------------------------------------------------------- | ||
Tue Oct 17 10:59:37 UTC 2023 - Imobach González Sosa <[email protected]> | ||
|
||
- Allow changing the language of the user interface | ||
(gh#openSUSE/agama#796). | ||
|
||
------------------------------------------------------------------- | ||
Tue Oct 10 08:50:53 UTC 2023 - Ladislav Slezák <[email protected]> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) [2023] SUSE LLC | ||
* | ||
* All Rights Reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of version 2 of the GNU General Public License as published | ||
* by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, contact SUSE LLC. | ||
* | ||
* To contact SUSE LLC about this file by physical or electronic mail, you may | ||
* find current contact information at www.suse.com. | ||
*/ | ||
|
||
import React, { useCallback, useState } from "react"; | ||
import { Icon } from "../layout"; | ||
import { FormSelect, FormSelectOption } from "@patternfly/react-core"; | ||
import { _ } from "~/i18n"; | ||
import { useL10n } from "~/context/l10n"; | ||
import cockpit from "~/lib/cockpit"; | ||
|
||
export default function LanguageSwitcher() { | ||
const { language, changeLanguage } = useL10n(); | ||
const [selected, setSelected] = useState(null); | ||
const languages = cockpit.manifests.agama?.locales || []; | ||
|
||
const onChange = useCallback((_event, value) => { | ||
setSelected(value); | ||
changeLanguage(value); | ||
}, [setSelected, changeLanguage]); | ||
|
||
const options = Object.entries(languages).map(([id, name]) => { | ||
return <FormSelectOption key={id} value={id} label={name} />; | ||
}); | ||
|
||
return ( | ||
<> | ||
<h3> | ||
<Icon name="translate" size="24" />{_("Display Language")} | ||
</h3> | ||
<FormSelect | ||
id="language" | ||
aria-label={_("language")} | ||
value={selected || language} | ||
onChange={onChange} | ||
> | ||
{options} | ||
</FormSelect> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (c) [2023] SUSE LLC | ||
* | ||
* All Rights Reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of version 2 of the GNU General Public License as published | ||
* by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, contact SUSE LLC. | ||
* | ||
* To contact SUSE LLC about this file by physical or electronic mail, you may | ||
* find current contact information at www.suse.com. | ||
*/ | ||
|
||
import React from "react"; | ||
import { screen } from "@testing-library/react"; | ||
import { plainRender } from "~/test-utils"; | ||
import LanguageSwitcher from "./LanguageSwitcher"; | ||
|
||
const mockLanguage = "es-es"; | ||
let mockChangeLanguageFn; | ||
|
||
jest.mock("~/lib/cockpit", () => ({ | ||
gettext: term => term, | ||
manifests: { | ||
agama: { | ||
locales: { | ||
"de-de": "Deutsch", | ||
"en-us": "English (US)", | ||
"es-es": "Español" | ||
} | ||
} | ||
} | ||
})); | ||
|
||
jest.mock("~/context/l10n", () => ({ | ||
...jest.requireActual("~/context/l10n"), | ||
useL10n: () => ({ | ||
language: mockLanguage, | ||
changeLanguage: mockChangeLanguageFn | ||
}) | ||
})); | ||
|
||
beforeEach(() => { | ||
mockChangeLanguageFn = jest.fn(); | ||
}); | ||
|
||
it("LanguageSwitcher", async () => { | ||
const { user } = plainRender(<LanguageSwitcher />); | ||
expect(screen.getByRole("option", { name: "Español" }).selected).toBe(true); | ||
await user.selectOptions( | ||
screen.getByRole("combobox", { label: "Display Language" }), | ||
screen.getByRole("option", { name: "English (US)" }) | ||
); | ||
expect(mockChangeLanguageFn).toHaveBeenCalledWith("en-us"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.