mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 18:57:03 +02:00
40 lines
1010 B
TypeScript
40 lines
1010 B
TypeScript
import { DEFAULT_LOCALE, unwrapper } from '@blitzkit/i18n';
|
|
import { createContext, useContext, type ReactNode } from 'react';
|
|
import { getStrings } from '../core/i18n/getStrings';
|
|
|
|
const LocaleContext = createContext<{
|
|
locale: string;
|
|
localeRaw: string | undefined;
|
|
} | null>(null);
|
|
|
|
interface LocaleProviderProps extends LocaleAcceptorProps {
|
|
children: ReactNode;
|
|
}
|
|
|
|
export function LocaleProvider({ locale, children }: LocaleProviderProps) {
|
|
return (
|
|
<LocaleContext.Provider
|
|
value={{ locale: locale ?? DEFAULT_LOCALE, localeRaw: locale }}
|
|
>
|
|
{children}
|
|
</LocaleContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useLocale() {
|
|
const context = useContext(LocaleContext);
|
|
|
|
if (!context) {
|
|
throw new Error('useLocale must be used within a LocaleProvider');
|
|
}
|
|
|
|
const strings = getStrings(context.locale);
|
|
const unwrap = unwrapper(context.locale);
|
|
|
|
return { locale: context.locale, strings, unwrap };
|
|
}
|
|
|
|
export interface LocaleAcceptorProps {
|
|
locale: string | undefined;
|
|
}
|