-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(web): implement language selection * fix(web): ensure locale cookie synced with URL on browser navigation
- Loading branch information
Showing
14 changed files
with
195 additions
and
36 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
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
35 changes: 35 additions & 0 deletions
35
service/vspo-schedule/web/src/components/Elements/Control/LanguageSelector.tsx
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 { MenuItem, TextField } from "@mui/material"; | ||
import { useTranslation } from "next-i18next"; | ||
import { useLocale } from "@/hooks"; | ||
|
||
const localeLabels: { [localeCode: string]: string } = { | ||
en: "English", | ||
ja: "日本語", | ||
}; | ||
|
||
export const LanguageSelector = () => { | ||
const { t } = useTranslation("common"); | ||
const { locale, setLocale } = useLocale(); | ||
|
||
return ( | ||
locale !== undefined && ( | ||
<TextField | ||
select | ||
size="small" | ||
id="language-select" | ||
label={t("drawer.language")} | ||
value={locale} | ||
onChange={(event) => { | ||
const selectedLocale = event.target.value; | ||
setLocale(selectedLocale); | ||
}} | ||
> | ||
{Object.keys(localeLabels).map((loc) => ( | ||
<MenuItem key={loc} value={loc}> | ||
{localeLabels[loc]} | ||
</MenuItem> | ||
))} | ||
</TextField> | ||
) | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
service/vspo-schedule/web/src/components/Elements/Control/index.ts
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 +1,2 @@ | ||
export * from "./LanguageSelector"; | ||
export * from "./TimeZoneSelector"; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./cookie"; | ||
export * from "./locale"; | ||
export * from "./time-zone"; | ||
export * from "./video-modal"; |
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,30 @@ | ||
import { DEFAULT_LOCALE, LOCALE_COOKIE } from "@/lib/Const"; | ||
import { useCookie } from "./cookie"; | ||
import { useRouter } from "next/router"; | ||
import { useEffect } from "react"; | ||
|
||
export const useLocale = () => { | ||
const router = useRouter(); | ||
const [localeCookie, setLocaleCookie] = useCookie( | ||
LOCALE_COOKIE, | ||
DEFAULT_LOCALE, | ||
); | ||
|
||
const setLocale = (locale: string) => { | ||
if (locale !== router.locale) { | ||
setLocaleCookie(locale); | ||
router.push(router.asPath, undefined, { scroll: false, locale }); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (localeCookie !== router.locale) { | ||
setLocaleCookie(router.locale ?? DEFAULT_LOCALE); | ||
} | ||
}, [router.locale]); | ||
|
||
return { | ||
locale: router.locale ?? DEFAULT_LOCALE, | ||
setLocale, | ||
}; | ||
}; |
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