Dynamically load date-fns locale data (#9535)

This commit is contained in:
Samuel Newman
2026-02-18 17:30:32 +00:00
committed by GitHub
parent 512ce3d5d0
commit 41b3742365
4 changed files with 315 additions and 214 deletions
+24 -2
View File
@@ -1,10 +1,32 @@
import {createContext, useContext} from 'react'
import {i18n} from '@lingui/core'
import {I18nProvider as DefaultI18nProvider} from '@lingui/react'
import {type Locale} from 'date-fns'
import type React from 'react'
import {useLocaleLanguage} from './i18n'
const DateLocaleContext = createContext<Locale | undefined>(undefined)
DateLocaleContext.displayName = 'DateLocaleContext'
export default function I18nProvider({children}: {children: React.ReactNode}) {
useLocaleLanguage()
return <DefaultI18nProvider i18n={i18n}>{children}</DefaultI18nProvider>
const dateLocale = useLocaleLanguage()
return (
<DateLocaleContext value={dateLocale}>
<DefaultI18nProvider i18n={i18n}>{children}</DefaultI18nProvider>
</DateLocaleContext>
)
}
/**
* Returns a `date-fns` locale corresponding to the current app language
*/
export function useDateLocale() {
const ctx = useContext(DateLocaleContext)
if (!ctx) {
throw new Error('useDateLocale must be used within an I18nProvider')
}
return ctx
}