11'use client'
2- import { Select } from '@chakra-ui/react'
32import { useLingui } from '@lingui/react/macro'
43import { i18n } from '@lingui/core'
54import { useRouter , usePathname } from 'next/navigation'
6- import { useState , useEffect } from 'react'
5+ import { useEffect } from 'react'
6+ import {
7+ Menu ,
8+ MenuButton ,
9+ MenuList ,
10+ MenuItem ,
11+ Link ,
12+ Button ,
13+ } from '@chakra-ui/react'
14+
15+ function getLocales ( ) {
16+ const { t } = useLingui ( )
17+ return [
18+ { locale : 'en' , label : t `English` } ,
19+ { locale : 'es' , label : t `Spanish` } ,
20+ { locale : 'pt' , label : t `Portuguese` } ,
21+ ]
22+ }
23+
24+ function getLocaleLabel ( locale ) {
25+ const locales = getLocales ( )
26+ const localeObject = locales . find ( ( object ) => object . locale === locale )
27+ return localeObject ? localeObject . label : locale
28+ }
729
830export const LanguageSwitcher = ( ) => {
931 const { t } = useLingui ( )
1032 const router = useRouter ( )
1133 const pathname = usePathname ( )
12- const [ selectedValue , setSelectedValue ] = useState ( '' )
1334
14- async function changeLocale ( event ) {
15- const localeString = event . target . value
16- setSelectedValue ( localeString )
35+ async function changeLocale ( ) {
36+ const localeString = i18n . locale
1737 localStorage . setItem ( 'locale' , localeString )
1838 const catalog = await import ( `../locales/${ localeString } /messages.js` )
1939 i18n . load ( localeString , catalog . messages )
@@ -28,16 +48,19 @@ export const LanguageSwitcher = () => {
2848 }
2949 } , [ ] )
3050 return (
31- < Select
32- name = 'selectedLocale'
33- variant = 'flushed'
34- size = 'xs'
35- value = { selectedValue }
36- onChange = { changeLocale }
37- >
38- < option value = 'en' > { t `English` } </ option >
39- < option value = 'es' > { t `Spanish` } </ option >
40- < option value = 'pt' > { t `Portuguese` } </ option >
41- </ Select >
51+ < Menu >
52+ < MenuButton as = { Button } size = 'xs' variant = 'outline' >
53+ { getLocaleLabel ( i18n . locale ) }
54+ </ MenuButton >
55+ < MenuList >
56+ { getLocales ( ) . map ( ( object ) => (
57+ < Link href = { '/' + object . locale + pathname } onClick = { changeLocale } >
58+ < MenuItem as = { Button } size = 'xs' command = { object . locale } >
59+ { object . label }
60+ </ MenuItem >
61+ </ Link >
62+ ) ) }
63+ </ MenuList >
64+ </ Menu >
4265 )
4366}
0 commit comments