-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add language selection on settings page
- Loading branch information
1 parent
28b48ae
commit 605a19a
Showing
11 changed files
with
189 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { createForm } from "@modular-forms/solid"; | ||
import { useNavigate } from "@solidjs/router"; | ||
import { createSignal, For, Show } from "solid-js"; | ||
|
||
import { Button, ExternalLink, InfoBox, NiceP, VStack } from "~/components"; | ||
import { useI18n } from "~/i18n/context"; | ||
import { useMegaStore } from "~/state/megaStore"; | ||
import { eify, EN_OPTION, Language, LANGUAGE_OPTIONS, timeout } from "~/utils"; | ||
|
||
type ChooseLanguageForm = { | ||
selectedLanguage: string; | ||
}; | ||
|
||
const COMBINED_OPTIONS: Language[] = [EN_OPTION, ...LANGUAGE_OPTIONS]; | ||
|
||
export function ChooseLanguage() { | ||
const i18n = useI18n(); | ||
const [error, setError] = createSignal<Error>(); | ||
const [state, actions] = useMegaStore(); | ||
const [loading, setLoading] = createSignal(false); | ||
const navigate = useNavigate(); | ||
|
||
function findLanguageByValue(value: string) { | ||
return ( | ||
COMBINED_OPTIONS.find((language) => language.value === value) ?? | ||
EN_OPTION | ||
); | ||
} | ||
|
||
const [_chooseLanguageForm, { Form, Field }] = | ||
createForm<ChooseLanguageForm>({ | ||
initialValues: { | ||
selectedLanguage: findLanguageByValue(state.lang ?? "").value | ||
}, | ||
validate: (values) => { | ||
const errors: Record<string, string> = {}; | ||
if (values.selectedLanguage === undefined) { | ||
errors.selectedLanguage = i18n.t( | ||
"settings.language.error_unsupported_language" | ||
); | ||
} | ||
return errors; | ||
} | ||
}); | ||
|
||
const handleFormSubmit = async (f: ChooseLanguageForm) => { | ||
setLoading(true); | ||
const lang = findLanguageByValue(f.selectedLanguage); | ||
try { | ||
actions.saveLanguage(lang.value); | ||
|
||
await i18n.changeLanguage(lang.shortName); | ||
|
||
await timeout(1000); | ||
navigate("/"); | ||
} catch (e) { | ||
console.error(e); | ||
setError(eify(e)); | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<VStack> | ||
<Form onSubmit={handleFormSubmit} class="flex flex-col gap-4"> | ||
<NiceP>{i18n.t("settings.language.caption")}</NiceP> | ||
<ExternalLink href="https://github.com/MutinyWallet/mutiny-web/issues/new"> | ||
{i18n.t("settings.language.request_language_support_link")} | ||
</ExternalLink> | ||
<div /> | ||
<VStack> | ||
<Field name="selectedLanguage"> | ||
{(field, props) => ( | ||
<select | ||
{...props} | ||
value={field.value} | ||
class="w-full rounded-lg bg-m-grey-750 py-2 pl-4 pr-12 text-base font-normal text-white" | ||
> | ||
<For each={COMBINED_OPTIONS}> | ||
{({ value, shortName }) => ( | ||
<option | ||
selected={field.value === shortName} | ||
value={value} | ||
> | ||
{value} | ||
</option> | ||
)} | ||
</For> | ||
</select> | ||
)} | ||
</Field> | ||
<Show when={error()}> | ||
<InfoBox accent="red">{error()?.message}</InfoBox> | ||
</Show> | ||
<div /> | ||
<Button intent="blue" loading={loading()}> | ||
{i18n.t("settings.language.select_language")} | ||
</Button> | ||
</VStack> | ||
</Form> | ||
</VStack> | ||
); | ||
} |
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
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,35 @@ | ||
import { | ||
BackLink, | ||
Card, | ||
ChooseLanguage, | ||
DefaultMain, | ||
LargeHeader, | ||
MutinyWalletGuard, | ||
NavBar, | ||
SafeArea | ||
} from "~/components"; | ||
import { useI18n } from "~/i18n/context"; | ||
|
||
export function Language() { | ||
const i18n = useI18n(); | ||
return ( | ||
<MutinyWalletGuard> | ||
<SafeArea> | ||
<DefaultMain> | ||
<BackLink | ||
href="/settings" | ||
title={i18n.t("settings.header")} | ||
/> | ||
<LargeHeader> | ||
{i18n.t("settings.language.title")} | ||
</LargeHeader> | ||
<Card title={i18n.t("settings.language.select_language")}> | ||
<ChooseLanguage /> | ||
</Card> | ||
<div class="h-full" /> | ||
</DefaultMain> | ||
<NavBar activeTab="settings" /> | ||
</SafeArea> | ||
</MutinyWalletGuard> | ||
); | ||
} |
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
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,20 @@ | ||
export interface Language { | ||
value: string; | ||
shortName: string; | ||
} | ||
|
||
export const EN_OPTION: Language = { | ||
value: "English", | ||
shortName: "en" | ||
}; | ||
|
||
export const LANGUAGE_OPTIONS: Language[] = [ | ||
{ | ||
value: "Português", | ||
shortName: "pt" | ||
}, | ||
{ | ||
value: "Korean", | ||
shortName: "ko" | ||
} | ||
]; |