mirror of
https://github.com/theoludwig/theoludwig.git
synced 2025-05-29 22:37:44 +02:00
refactor: implement light/dark themes using cookies
This commit is contained in:
@ -1,9 +1,9 @@
|
||||
import UniversalCookie from 'universal-cookie'
|
||||
import type { I18n } from 'i18n-js'
|
||||
|
||||
import { i18n } from './i18n'
|
||||
import type { CookiesStore } from '@/utils/constants'
|
||||
|
||||
export type CookiesStore = string | object | null | undefined
|
||||
import { i18n } from './i18n'
|
||||
|
||||
export const useI18n = (cookiesStore: CookiesStore): I18n => {
|
||||
const universalCookie = new UniversalCookie(cookiesStore)
|
||||
|
@ -3,11 +3,12 @@
|
||||
import { cookies } from 'next/headers'
|
||||
import type { I18n } from 'i18n-js'
|
||||
|
||||
import type { Locale } from '@/utils/constants'
|
||||
import { COOKIE_MAX_AGE } from '@/utils/constants'
|
||||
|
||||
import { i18n } from './i18n'
|
||||
|
||||
export const setLocale = (locale: string): void => {
|
||||
export const setLocale = (locale: Locale): void => {
|
||||
cookies().set('locale', locale, {
|
||||
path: '/',
|
||||
maxAge: COOKIE_MAX_AGE
|
||||
|
40
i18n/i18n.ts
40
i18n/i18n.ts
@ -1,6 +1,7 @@
|
||||
import { I18n } from 'i18n-js'
|
||||
|
||||
import { DEFAULT_LOCALE, AVAILABLE_LOCALES } from '@/utils/constants'
|
||||
import type { Locale } from '@/utils/constants'
|
||||
import { DEFAULT_LOCALE, LOCALES } from '@/utils/constants'
|
||||
|
||||
import commonEnglish from './translations/en-US/common.json'
|
||||
import errorsEnglish from './translations/en-US/errors.json'
|
||||
@ -9,24 +10,21 @@ import commonFrench from './translations/fr-FR/common.json'
|
||||
import errorsFrench from './translations/fr-FR/errors.json'
|
||||
import homeFrench from './translations/fr-FR/home.json'
|
||||
|
||||
export const i18nOptions = {
|
||||
defaultLocale: DEFAULT_LOCALE,
|
||||
availableLocales: AVAILABLE_LOCALES.slice(),
|
||||
enableFallback: true
|
||||
}
|
||||
|
||||
export const i18n = new I18n(
|
||||
{
|
||||
'en-US': {
|
||||
common: commonEnglish,
|
||||
errors: errorsEnglish,
|
||||
home: homeEnglish
|
||||
},
|
||||
'fr-FR': {
|
||||
common: commonFrench,
|
||||
errors: errorsFrench,
|
||||
home: homeFrench
|
||||
}
|
||||
const translations = {
|
||||
'en-US': {
|
||||
common: commonEnglish,
|
||||
errors: errorsEnglish,
|
||||
home: homeEnglish
|
||||
},
|
||||
i18nOptions
|
||||
)
|
||||
'fr-FR': {
|
||||
common: commonFrench,
|
||||
errors: errorsFrench,
|
||||
home: homeFrench
|
||||
}
|
||||
} satisfies Record<Locale, Record<string, unknown>>
|
||||
|
||||
export const i18n = new I18n(translations, {
|
||||
defaultLocale: DEFAULT_LOCALE,
|
||||
availableLocales: LOCALES.slice(),
|
||||
enableFallback: true
|
||||
})
|
||||
|
Reference in New Issue
Block a user