-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core); add locale picker in Header
- Loading branch information
Showing
6 changed files
with
163 additions
and
31 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,5 @@ | ||
--- | ||
"@bigcommerce/catalyst-core": patch | ||
--- | ||
|
||
Add locale picker in header. |
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,118 @@ | ||
'use client'; | ||
|
||
import { useLocale } from 'next-intl'; | ||
import { useMemo, useState } from 'react'; | ||
|
||
import { localeLanguageRegionMap, locales, LocaleType } from '~/i18n'; | ||
import { useRouter } from '~/navigation'; | ||
|
||
import { Button } from '../ui/button'; | ||
import { Popover } from '../ui/popover'; | ||
import { Select } from '../ui/select'; | ||
|
||
type LanguagesByRegionMap = Record< | ||
string, | ||
{ | ||
languages: string[]; | ||
flag: string; | ||
} | ||
>; | ||
|
||
export const LocaleSwitcher = () => { | ||
const locale = useLocale(); | ||
const router = useRouter(); | ||
|
||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
const selectedLocale = localeLanguageRegionMap[locale as LocaleType]; | ||
|
||
const [regionSelected, setRegionSelected] = useState(selectedLocale.region); | ||
const [languageSelected, setLanguageSelected] = useState(selectedLocale.language); | ||
|
||
const languagesByRegionMap = useMemo( | ||
() => | ||
Object.values(localeLanguageRegionMap).reduce<LanguagesByRegionMap>( | ||
(acc, { region, language, flag }) => { | ||
if (!acc[region]) { | ||
acc[region] = { languages: [language], flag }; | ||
} | ||
|
||
if (!acc[region].languages.includes(language)) { | ||
acc[region].languages.push(language); | ||
} | ||
|
||
return acc; | ||
}, | ||
{}, | ||
), | ||
[], | ||
); | ||
|
||
const regions = Object.keys(languagesByRegionMap); | ||
|
||
const handleOnOpenChange = () => { | ||
setRegionSelected(selectedLocale.region); | ||
setLanguageSelected(selectedLocale.language); | ||
}; | ||
|
||
const handleRegionChange = (region: string) => { | ||
setRegionSelected(region); | ||
setLanguageSelected(languagesByRegionMap[region]?.languages[0] || ''); // FIX? | ||
}; | ||
|
||
const handleLanguageChange = (language: string) => { | ||
if (language) { | ||
setLanguageSelected(language); | ||
} | ||
}; | ||
|
||
const handleOnSubmit = () => { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
const keys = Object.keys(localeLanguageRegionMap) as LocaleType[]; | ||
|
||
const newLocale = keys.find( | ||
(key) => | ||
localeLanguageRegionMap[key].language === languageSelected && | ||
localeLanguageRegionMap[key].region === regionSelected, | ||
); | ||
|
||
if (newLocale) { | ||
router.replace('/', { locale: newLocale }); | ||
} | ||
}; | ||
|
||
return ( | ||
Object.keys(locales).length > 1 && ( | ||
<Popover | ||
onOpenChange={handleOnOpenChange} | ||
trigger={ | ||
<button className="flex h-12 items-center p-3 text-2xl">{selectedLocale.flag}</button> | ||
} | ||
> | ||
<form className="flex flex-col gap-4" onSubmit={handleOnSubmit}> | ||
<p>Choose your country and language</p> | ||
<Select | ||
onValueChange={handleRegionChange} | ||
options={regions.map((region) => ({ | ||
value: region, | ||
label: `${languagesByRegionMap[region]?.flag} ${region}`, | ||
}))} | ||
value={regionSelected} | ||
/> | ||
<Select | ||
onValueChange={handleLanguageChange} | ||
options={ | ||
languagesByRegionMap[regionSelected]?.languages.map((language) => ({ | ||
value: language, | ||
label: language, | ||
})) || [] // FIX? | ||
} | ||
value={languageSelected} | ||
/> | ||
<Button className="w-auto" type="submit"> | ||
Go to site | ||
</Button> | ||
</form> | ||
</Popover> | ||
) | ||
); | ||
}; |
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