@scaleway/use-i18n
Version:
A small hook to handle i18n
71 lines • 4.63 kB
TypeScript
import { FormatDateOptions } from "./formatDate.js";
import { IntlListFormatOptions } from "./formatters.js";
import { FormatUnitOptions } from "./formatUnit.js";
import { ScopedTranslateFn, TranslateFn } from "./types.js";
import { FormatDistanceToNowOptions, FormatDistanceToNowStrictOptions, FormatDurationOptions, Locale } from "date-fns";
import { ReactElement, ReactNode } from "react";
import { NumberFormatOptions } from "@formatjs/ecma402-abstract";
import { BaseLocale } from "international-types";
//#region src/usei18n.d.ts
type SupportedLocalesType<LocalSupportedType extends string> = (locale: string) => locale is LocalSupportedType;
type TranslationsByLocales = Record<string, BaseLocale>;
type LoadTranslationsFn<LocalSupportedType extends string> = ({ namespace, locale }: {
namespace: string;
locale: LocalSupportedType;
}) => Promise<{
default: BaseLocale;
}>;
type LoadLocaleFn<LocalSupportedType extends string> = (locale: LocalSupportedType) => Locale;
type LoadLocaleFnAsync<LocalSupportedType extends string> = (locale: LocalSupportedType) => Promise<Locale>;
type LoadDateLocaleError = (error: Error) => void;
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 FormatDurationOptions$1 = Omit<FormatDurationOptions, 'locale'> | 'clock' | 'clock-milliseconds';
type Context<LocaleParam extends BaseLocale, LocalSupportedType extends string> = {
currentLocale: LocalSupportedType;
dateFnsLocale?: Locale;
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;
formatDuration: (value: number, options?: FormatDurationOptions$1) => 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;
};
declare function useI18n<LocaleParam extends BaseLocale = BaseLocale, LocalSupportedType extends string = ''>(): RequiredGenericContext<LocaleParam, LocalSupportedType>;
declare function useTranslation<LocaleParam extends BaseLocale = BaseLocale, LocalSupportedType extends string = ''>(namespaces: [string, ...string[]], load?: LoadTranslationsFn<LocalSupportedType>): RequiredGenericContext<LocaleParam, LocalSupportedType> & {
isLoaded: boolean;
};
declare const I18nContextProvider: <LocalSupportedType extends string>({ children, defaultLoad, defaultLocale, defaultTranslations, enableDebugKey, enableDefaultLocale, loadDateLocale, loadDateLocaleAsync, localeItemStorage, onLoadDateLocaleError, onTranslateError, onLoadTranslationError, 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>;
onLoadTranslationError: (error: unknown | Error) => void;
onTranslateError?: ({ error, currentLocale, value, key }: {
error: Error;
currentLocale: LocalSupportedType;
defaultLocale: LocalSupportedType;
value: string;
key: string;
}) => void;
}) => ReactElement;
//#endregion
export { Context, LoadDateLocaleError, LoadLocaleFn, LoadLocaleFnAsync, LoadTranslationsFn, RequiredGenericContext, SupportedLocalesType, TranslationsByLocales, I18nContextProvider as default, useI18n, useTranslation };