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.

1234567891011121314151617181920212223242526272829
  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 = async (locale: Locale, reloadPage = true) => {
  11. Cookies.set(LOCALE_COOKIE_NAME, locale, { expires: 365 })
  12. await changeLanguage(locale)
  13. reloadPage && location.reload()
  14. }
  15. export const getLocaleOnClient = (): Locale => {
  16. return Cookies.get(LOCALE_COOKIE_NAME) as Locale || i18n.defaultLocale
  17. }
  18. export const renderI18nObject = (obj: Record<string, string>, language: string) => {
  19. if (!obj) return ''
  20. if (obj?.[language]) return obj[language]
  21. if (obj?.en_US) return obj.en_US
  22. return Object.values(obj)[0]
  23. }