index.ts 634 B

12345678910111213141516171819202122
  1. import Cookies from 'js-cookie'
  2. import { changeLanguage } from '@/i18n/i18next-config'
  3. import { LOCALE_COOKIE_NAME } from '@/config'
  4. import { LanguagesSupported } from '@/i18n/language'
  5. export const i18n = {
  6. defaultLocale: 'en-US',
  7. locales: LanguagesSupported,
  8. } as const
  9. export type Locale = typeof i18n['locales'][number]
  10. export const setLocaleOnClient = (locale: Locale, reloadPage = true) => {
  11. Cookies.set(LOCALE_COOKIE_NAME, locale)
  12. changeLanguage(locale)
  13. reloadPage && location.reload()
  14. }
  15. export const getLocaleOnClient = (): Locale => {
  16. return Cookies.get(LOCALE_COOKIE_NAME) as Locale || i18n.defaultLocale
  17. }