You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
600 B
28 lines
600 B
import { useCallback } from 'react'
|
|
|
|
import { useLocalStore } from 'hooks'
|
|
|
|
import type { Languages } from 'config/languages'
|
|
|
|
import { isSupportedLang } from '../helpers/isSupportedLang'
|
|
|
|
const LANG_KEY = 'lang'
|
|
const DEFAULT_LANG = 'en'
|
|
|
|
export const useLang = () => {
|
|
const [lang, setLang] = useLocalStore<Languages>({
|
|
defaultValue: DEFAULT_LANG,
|
|
key: LANG_KEY,
|
|
validator: isSupportedLang,
|
|
})
|
|
|
|
const changeLang = useCallback(
|
|
(newLang: Languages) => {
|
|
if (newLang === lang) return
|
|
setLang(newLang)
|
|
},
|
|
[lang, setLang],
|
|
)
|
|
|
|
return { changeLang, lang }
|
|
}
|
|
|