Skip to content

Commit

Permalink
Persists language in local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
abtestingalpha committed Sep 4, 2024
1 parent c51e319 commit 8d60e51
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/synapse-interface/components/LanguageSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useState, useRef } from 'react'
import React, { useState, useRef, useEffect } from 'react'
import { useRouter } from 'next/router'
import { GlobeAltIcon } from '@heroicons/react/outline'

import useCloseOnOutsideClick from '@/utils/hooks/useCloseOnOutsideClick'
import { useCloseOnEscape } from '@/utils/hooks/useCloseOnEscape'

Expand All @@ -13,14 +12,27 @@ const languages = [

export const LanguageSelector = () => {
const router = useRouter()
const { pathname, asPath, query, locale: currentLocale } = router
const { pathname, asPath, query } = router
const [isOpen, setIsOpen] = useState(false)
const [currentLocale, setCurrentLocale] = useState(router.locale)
const dropdownRef = useRef(null)

useEffect(() => {
const storedLanguage = localStorage.getItem('selectedLanguage')
if (storedLanguage && storedLanguage !== router.locale) {
router.push({ pathname, query }, asPath, { locale: storedLanguage })
}
}, [])

useEffect(() => {
setCurrentLocale(router.locale)
}, [router.locale])

const toggleDropdown = () => setIsOpen(!isOpen)

const handleLanguageChange = (lang) => {
router.push({ pathname, query }, asPath, { locale: lang.code })
localStorage.setItem('selectedLanguage', lang.code)
}

const closeDropdown = () => setIsOpen(false)
Expand Down

0 comments on commit 8d60e51

Please sign in to comment.