talkr
Version:
Talkr is the lightest i18n provider for React applications. It supports Typescript, provides autocompletion, has 0 dependencies, and is very easy to use.
40 lines (35 loc) • 1.7 kB
text/typescript
import * as React from 'react';
import { PropsWithChildren } from 'react';
interface TProps extends PropsWithChildren {
languages: Record<string, any>;
defaultLanguage: string;
detectBrowserLanguage?: boolean;
}
interface TContext {
locale: string;
setLocale: (language: string) => void;
languages: Record<string, any>;
defaultLanguage: string;
}
type TrContext = Omit<TContext, "setLocale">;
type KeyPrefix<T extends string> = T extends "" ? "" : `.${T}`;
type Suffix = "zero" | "one" | "two" | "few" | "many" | "female" | "male";
type DynamicSuffix = Partial<Record<Suffix, string>>;
type KeyPath<T> = (T extends DynamicSuffix ? "" : T extends Array<any> ? "" : T extends object ? {
[K in Exclude<keyof T, symbol>]: `${K}${KeyPrefix<KeyPath<T[K]>>}`;
}[Exclude<keyof T, symbol>] : "") extends infer D ? Extract<D, string> : never;
type TParams = Record<string, any> & {
count?: number;
gender?: "m" | "f";
listType?: "conjunction" | "disjunction";
listStyle?: "long" | "narrow";
};
type Autocomplete<schema> = KeyPath<schema>;
interface UseT extends TContext {
T: <Key extends string, Params extends TParams>(key: Key, params?: Params) => string;
}
declare function tr<Key extends string, Params extends TParams>({ locale, languages, defaultLanguage }: TrContext, key: Key, params?: Params): string;
declare const TalkrContext: React.Context<TContext>;
declare function Talkr({ children, languages, defaultLanguage, detectBrowserLanguage, }: TProps): React.JSX.Element;
declare function useT(): UseT;
export { type Autocomplete, type KeyPath, type TContext, type TParams, type TProps, Talkr, TalkrContext, type TrContext, type UseT, tr, useT };