UNPKG

@scaleway/use-i18n

Version:
67 lines (66 loc) 4.27 kB
import type { NumberFormatOptions } from '@formatjs/ecma402-abstract'; import type { Locale as DateFnsLocale, FormatDistanceToNowOptions, FormatDistanceToNowStrictOptions } from 'date-fns'; import type { BaseLocale } from 'international-types'; import type { ReactElement, ReactNode } from 'react'; import { type FormatDateOptions } from './formatDate'; import { type FormatUnitOptions } from './formatUnit'; import { type IntlListFormatOptions } from './formatters'; import type { ScopedTranslateFn, TranslateFn } from './types'; type SupportedLocalesType<LocalSupportedType extends string> = (locale: string) => locale is LocalSupportedType; type TranslationsByLocales = Record<string, BaseLocale>; type RequiredGenericContext<LocaleParam extends BaseLocale, LocalSupportedType extends string> = keyof LocaleParam extends never ? Omit<Context<LocaleParam, LocalSupportedType>, 't' | 'namespaceTranslation'> & { t: (str: 'You must pass a generic argument to useI18n()') => void; namespaceTranslation: (str: 'You must pass a generic argument to useI18n()') => void; } : Context<LocaleParam, LocalSupportedType>; type Context<LocaleParam extends BaseLocale, LocalSupportedType extends string> = { currentLocale: LocalSupportedType; dateFnsLocale?: DateFnsLocale; datetime: (date: Date | number, options?: Intl.DateTimeFormatOptions) => string; formatDate: (value: Date | number | string, options?: FormatDateOptions) => string; formatList: (listFormat: string[], options?: IntlListFormatOptions) => string; formatNumber: (numb: number, options?: NumberFormatOptions) => string; formatUnit: (value: number, options: FormatUnitOptions) => string; loadTranslations: (namespace: string, load?: LoadTranslationsFn<LocalSupportedType>) => Promise<string>; namespaces: string[]; namespaceTranslation: ScopedTranslateFn<LocaleParam>; relativeTime: (date: Date | number, options?: FormatDistanceToNowOptions) => string; relativeTimeStrict: (date: Date | number, options?: FormatDistanceToNowStrictOptions) => string; setTranslations: React.Dispatch<React.SetStateAction<TranslationsByLocales>>; switchLocale: (locale: LocalSupportedType) => Promise<void>; t: TranslateFn<LocaleParam>; translations: TranslationsByLocales; }; export declare function useI18n<LocaleParam extends BaseLocale = {}, LocalSupportedType extends string = ''>(): RequiredGenericContext<LocaleParam, LocalSupportedType>; export declare function useTranslation<LocaleParam extends BaseLocale = {}, LocalSupportedType extends string = ''>(namespaces?: string[], load?: LoadTranslationsFn<LocalSupportedType> | undefined): RequiredGenericContext<LocaleParam, LocalSupportedType> & { isLoaded: boolean; }; type LoadTranslationsFn<LocalSupportedType extends string> = ({ namespace, locale, }: { namespace: string; locale: LocalSupportedType; }) => Promise<{ default: BaseLocale; }>; type LoadLocaleFn<LocalSupportedType extends string> = (locale: LocalSupportedType) => DateFnsLocale; type LoadLocaleFnAsync<LocalSupportedType extends string> = (locale: LocalSupportedType) => Promise<DateFnsLocale>; type LoadDateLocaleError = (error: Error) => void; declare const I18nContextProvider: <LocalSupportedType extends string>({ children, defaultLoad, defaultLocale, defaultTranslations, enableDebugKey, enableDefaultLocale, loadDateLocale, loadDateLocaleAsync, localeItemStorage, onLoadDateLocaleError, onTranslateError, isLocaleSupported, }: { children: ReactNode; defaultLoad: LoadTranslationsFn<LocalSupportedType>; loadDateLocale?: LoadLocaleFn<LocalSupportedType>; loadDateLocaleAsync: LoadLocaleFnAsync<LocalSupportedType>; onLoadDateLocaleError?: LoadDateLocaleError; defaultLocale: LocalSupportedType; defaultTranslations: TranslationsByLocales; enableDefaultLocale: boolean; enableDebugKey: boolean; localeItemStorage: string; isLocaleSupported: SupportedLocalesType<LocalSupportedType>; onTranslateError?: ({ error, currentLocale, value, key, }: { error: Error; currentLocale: LocalSupportedType; defaultLocale: LocalSupportedType; value: string; key: string; }) => void; }) => ReactElement; export default I18nContextProvider;