UNPKG

5.39 kBTypeScriptView Raw
1import type { $Subtract, $Tuple } from './helpers.js';
2import type {
3 ReactOptions,
4 i18n,
5 Resource,
6 FlatNamespace,
7 Namespace,
8 TypeOptions,
9 TFunction,
10 KeyPrefix,
11} from 'i18next';
12import * as React from 'react';
13import { Trans, TransProps, ErrorCode, ErrorArgs } from './TransWithoutContext.js';
14export { initReactI18next } from './initReactI18next.js';
15
16export const TransWithoutContext: typeof Trans;
17export { Trans, TransProps, ErrorArgs, ErrorCode };
18
19export function setDefaults(options: ReactOptions): void;
20export function getDefaults(): ReactOptions;
21export function setI18n(instance: i18n): void;
22export function getI18n(): i18n;
23export function composeInitialProps(ForComponent: any): (ctx: unknown) => Promise<any>;
24export function getInitialProps(): {
25 initialI18nStore: {
26 [ns: string]: {};
27 };
28 initialLanguage: string;
29};
30
31export interface ReportNamespaces {
32 addUsedNamespaces(namespaces: Namespace): void;
33 getUsedNamespaces(): string[];
34}
35
36declare module 'i18next' {
37 // interface i18n {
38 // reportNamespaces?: ReportNamespaces;
39 // }
40 interface CustomInstanceExtensions {
41 reportNamespaces?: ReportNamespaces;
42 }
43}
44
45type ObjectOrNever = TypeOptions['allowObjectInHTMLChildren'] extends true
46 ? Record<string, unknown>
47 : never;
48
49type ReactI18NextChildren = React.ReactNode | ObjectOrNever;
50
51declare module 'react' {
52 namespace JSX {
53 interface IntrinsicAttributes {
54 i18nIsDynamicList?: boolean;
55 }
56 }
57
58 interface HTMLAttributes<T> {
59 // This union is inspired by the typings for React.ReactNode. We do this to fix "This JSX tag's 'children' prop
60 // expects a single child of type 'ReactI18NextChildren', but multiple children were provided":
61 // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/5a1e9f91ed0143adede394adb3f540e650455f71/types/react/index.d.ts#L268
62 children?: ReactI18NextChildren | Iterable<ReactI18NextChildren>;
63 }
64}
65
66type _DefaultNamespace = TypeOptions['defaultNS'];
67
68export function useSSR(initialI18nStore: Resource, initialLanguage: string): void;
69
70export interface UseTranslationOptions<KPrefix> {
71 i18n?: i18n;
72 useSuspense?: boolean;
73 keyPrefix?: KPrefix;
74 bindI18n?: string | false;
75 nsMode?: 'fallback' | 'default';
76 lng?: string;
77 // other of these options might also work: https://github.com/i18next/i18next/blob/master/index.d.ts#L127
78}
79
80export type UseTranslationResponse<Ns extends Namespace, KPrefix> = [
81 t: TFunction<Ns, KPrefix>,
82 i18n: i18n,
83 ready: boolean,
84] & {
85 t: TFunction<Ns, KPrefix>;
86 i18n: i18n;
87 ready: boolean;
88};
89
90// Workaround to make code completion to work when suggesting namespaces.
91// This is a typescript limitation when using generics with default values,
92// it'll be addressed in this issue: https://github.com/microsoft/TypeScript/issues/52516
93export type FallbackNs<Ns> = Ns extends undefined
94 ? _DefaultNamespace
95 : Ns extends Namespace
96 ? Ns
97 : _DefaultNamespace;
98
99export function useTranslation<
100 Ns extends FlatNamespace | $Tuple<FlatNamespace> | undefined = undefined,
101 KPrefix extends KeyPrefix<FallbackNs<Ns>> = undefined,
102>(
103 ns?: Ns,
104 options?: UseTranslationOptions<KPrefix>,
105): UseTranslationResponse<FallbackNs<Ns>, KPrefix>;
106
107// Need to see usage to improve this
108export function withSSR(): <Props>(WrappedComponent: React.ComponentType<Props>) => {
109 ({
110 initialI18nStore,
111 initialLanguage,
112 ...rest
113 }: {
114 initialI18nStore: Resource;
115 initialLanguage: string;
116 } & Props): React.FunctionComponentElement<Props>;
117 getInitialProps: (ctx: unknown) => Promise<any>;
118};
119
120export interface WithTranslation<
121 Ns extends FlatNamespace | $Tuple<FlatNamespace> | undefined = undefined,
122 KPrefix extends KeyPrefix<FallbackNs<Ns>> = undefined,
123> {
124 t: TFunction<FallbackNs<Ns>, KPrefix>;
125 i18n: i18n;
126 tReady: boolean;
127}
128
129export interface WithTranslationProps {
130 i18n?: i18n;
131 useSuspense?: boolean;
132}
133
134export function withTranslation<
135 Ns extends FlatNamespace | $Tuple<FlatNamespace> | undefined = undefined,
136 KPrefix extends KeyPrefix<FallbackNs<Ns>> = undefined,
137>(
138 ns?: Ns,
139 options?: {
140 withRef?: boolean;
141 keyPrefix?: KPrefix;
142 },
143): <
144 C extends React.ComponentType<React.ComponentProps<any> & WithTranslationProps>,
145 ResolvedProps = React.JSX.LibraryManagedAttributes<
146 C,
147 $Subtract<React.ComponentProps<C>, WithTranslationProps>
148 >,
149>(
150 component: C,
151) => React.ComponentType<Omit<ResolvedProps, keyof WithTranslation<Ns>> & WithTranslationProps>;
152
153export interface I18nextProviderProps {
154 children?: React.ReactNode;
155 i18n: i18n;
156 defaultNS?: string | string[];
157}
158
159export const I18nextProvider: React.FunctionComponent<I18nextProviderProps>;
160export const I18nContext: React.Context<{ i18n: i18n }>;
161
162export interface TranslationProps<
163 Ns extends FlatNamespace | $Tuple<FlatNamespace> | undefined = undefined,
164 KPrefix extends KeyPrefix<FallbackNs<Ns>> = undefined,
165> {
166 children: (
167 t: TFunction<FallbackNs<Ns>, KPrefix>,
168 options: {
169 i18n: i18n;
170 lng: string;
171 },
172 ready: boolean,
173 ) => React.ReactNode;
174 ns?: Ns;
175 i18n?: i18n;
176 useSuspense?: boolean;
177 keyPrefix?: KPrefix;
178 nsMode?: 'fallback' | 'default';
179}
180
181export function Translation<
182 Ns extends FlatNamespace | $Tuple<FlatNamespace> | undefined = undefined,
183 KPrefix extends KeyPrefix<FallbackNs<Ns>> = undefined,
184>(props: TranslationProps<Ns, KPrefix>): any;