1
1
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:
2023-08-01 14:11:46 +02:00
parent e82db952db
commit caa6a90418
21 changed files with 117 additions and 117 deletions

View File

@ -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)

View File

@ -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

View File

@ -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
})