diff --git a/types/react-i18next/index.d.ts b/types/react-i18next/index.d.ts index 9e731fe5e6..9831c0b049 100644 --- a/types/react-i18next/index.d.ts +++ b/types/react-i18next/index.d.ts @@ -1,11 +1,21 @@ -// Type definitions for react-i18next 7.0 +// Type definitions for react-i18next 7.3 // Project: https://github.com/i18next/react-i18next // Definitions by: Giedrius Grabauskas +// Simon Baumann // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.4 -import { i18n as I18n, TranslationFunction } from "i18next"; +import { TranslationFunction } from "i18next"; +import { + setDefaults, + getDefaults, + setI18n, + getI18n, + ReactI18NextOptions, + reactI18nextModule +} from "./src/context"; +import I18n from "./src/I18n"; import I18nextProvider from "./src/I18nextProvider"; import Interpolate from "./src/interpolate"; import loadNamespaces from "./src/loadNamespaces"; @@ -13,12 +23,18 @@ import Trans from "./src/trans"; import translate from "./src/translate"; export { + setDefaults, + getDefaults, + setI18n, + getI18n, + ReactI18NextOptions, + reactI18nextModule, + I18n, I18nextProvider, Interpolate, loadNamespaces, Trans, translate, - // Exports for TypeScript only TranslationFunction }; diff --git a/types/react-i18next/src/I18n.d.ts b/types/react-i18next/src/I18n.d.ts new file mode 100644 index 0000000000..122249c6e8 --- /dev/null +++ b/types/react-i18next/src/I18n.d.ts @@ -0,0 +1,21 @@ +import * as React from "react"; +import { i18n, TranslationFunction } from "i18next"; + +export interface Options { + i18n: i18n; + t: TranslationFunction; +} + +export interface i18nProps { + wait?: boolean; + ns: string | string[]; + nsMode?: string; + bindI18n?: string; + bindStore?: string; + i18n?: i18n; + initialI18nStore?: any; + initialLanguage?: string; + children: (t: TranslationFunction, options: Options) => JSX.Element; +} + +export default class I18n extends React.Component { } diff --git a/types/react-i18next/src/I18nextProvider.d.ts b/types/react-i18next/src/I18nextProvider.d.ts index 2ecb87f4a1..2cd4e333e3 100644 --- a/types/react-i18next/src/I18nextProvider.d.ts +++ b/types/react-i18next/src/I18nextProvider.d.ts @@ -4,9 +4,9 @@ import { i18n } from "i18next"; // tslint:disable-next-line:interface-name export interface I18nextProviderProps { i18n: i18n; - children: React.ReactElement; initialI18nStore?: any; initialLanguage?: string; + children: JSX.Element; } export default class I18nextProvider extends React.Component { } diff --git a/types/react-i18next/src/context.d.ts b/types/react-i18next/src/context.d.ts index 8e11a4d7a3..f772e8c1da 100644 --- a/types/react-i18next/src/context.d.ts +++ b/types/react-i18next/src/context.d.ts @@ -11,4 +11,16 @@ export interface ReactI18NextOptions { export function setDefaults(options: ReactI18NextOptions): void; +export function getDefaults(): ReactI18NextOptions; + export function setI18n(instance: i18n): void; + +export function getI18n(): i18n; + +export interface i18NextModule { + type: string; + + init: (instance: i18n) => void; +} + +export const reactI18nextModule: i18NextModule; diff --git a/types/react-i18next/src/interpolate.d.ts b/types/react-i18next/src/interpolate.d.ts index e6f07893ed..d6a4e64420 100644 --- a/types/react-i18next/src/interpolate.d.ts +++ b/types/react-i18next/src/interpolate.d.ts @@ -1,5 +1,5 @@ import * as React from "react"; -import { InterpolationOptions } from "i18next"; +import { i18n, InterpolationOptions, TranslationFunction } from "i18next"; export type InterpolateValue = string | JSX.Element; @@ -12,6 +12,8 @@ export interface InterpolatePropsBase { i18nKey?: string; className?: string; style?: React.CSSProperties; + i18n?: i18n; + t?: TranslationFunction; } export interface OtherInterpolateProps { @@ -20,4 +22,4 @@ export interface OtherInterpolateProps { export type InterpolateProps = InterpolatePropsBase & OtherInterpolateProps; -export default class Interpolate extends React.Component { } +export default class Interpolate extends React.PureComponent { } diff --git a/types/react-i18next/src/trans.d.ts b/types/react-i18next/src/trans.d.ts index 384e1605f9..8b12c81fc9 100644 --- a/types/react-i18next/src/trans.d.ts +++ b/types/react-i18next/src/trans.d.ts @@ -1,8 +1,12 @@ import * as React from "react"; +import { i18n, TranslationFunction } from "i18next"; export interface TransProps { i18nKey?: string; - [name: string]: any; + count?: number; + parent?: string; + i18n?: i18n; + t?: TranslationFunction; } export default class Trans extends React.Component { } diff --git a/types/react-i18next/test/react-i18next-tests.tsx b/types/react-i18next/test/react-i18next-tests.tsx index 5ddb17c10c..46d212e680 100644 --- a/types/react-i18next/test/react-i18next-tests.tsx +++ b/types/react-i18next/test/react-i18next-tests.tsx @@ -1,20 +1,32 @@ import * as React from 'react'; import * as i18n from 'i18next'; -import { translate, I18nextProvider, Interpolate, InjectedTranslateProps, TranslationFunction, loadNamespaces, Trans } from 'react-i18next'; +import { + setDefaults, + reactI18nextModule, + translate, + I18nextProvider, + Interpolate, + InjectedTranslateProps, + TranslationFunction, + loadNamespaces, + Trans, + I18n, + ReactI18NextOptions +} from 'react-i18next'; import { InjectedI18nProps } from 'react-i18next/src/props'; interface InnerAnotherComponentProps { - _: TranslationFunction; + _: TranslationFunction; } class InnerAnotherComponent extends React.Component { - render() { - const _ = this.props._; - return

{_('content.text', { /* options t options */ })}

; - } + render() { + const _ = this.props._; + return

{_('content.text', {/* options t options */})}

; + } } -const AnotherComponent = translate('view', { wait: true, translateFuncName: '_' })(InnerAnotherComponent); +const AnotherComponent = translate('view', {wait: true, translateFuncName: '_'})(InnerAnotherComponent); const instanceWithoutRef = new AnotherComponent({}); instanceWithoutRef.componentWillReceiveProps!({ i18n, @@ -35,45 +47,46 @@ instanceWithRef.componentWillReceiveProps!({ }, {}); class InnerYetAnotherComponent extends React.Component { - render() { - const t = this.props.t; - return

{t('usingDefaultNS', { /* options t options */ })}

; - } + render() { + const t = this.props.t; + return

{t('usingDefaultNS', {/* options t options */})}

; + } } const YetAnotherComponent = translate()(InnerYetAnotherComponent); const YetAnotherComponentWithRef = translate(undefined, { withRef: true })(InnerYetAnotherComponent); new YetAnotherComponentWithRef({}).getWrappedInstance(); - class TranslatableView extends React.Component { - render() { - const t = this.props.t; - const interpolateComponent = "a interpolated component"; - - return ( -
-

{t('common:appName')}

- - - - - {t('nav:link1')} -
- ); - } + render() { + const t = this.props.t; + const interpolateComponent = "a interpolated component"; + const options: i18n.InterpolationOptions = {}; + return ( +
+

{t('common:appName')}

+ + + + + {t('nav:link1')} +
+ ); + } } const TranslatedView = translate(["view", "nav"] as Key[], { wait: true })(TranslatableView); @@ -90,29 +103,38 @@ class App extends React.Component { } } - - + + ; -loadNamespaces({ components: [App], i18n }).then(() => { }).catch(error => { }); +loadNamespaces({components: [App], i18n}).then(() => { +}).catch(error => { +}); -; -; +; +; +; +; +; - + ; type Key = "view" | "nav"; class GenericsTest extends React.Component { - render() { return null; } + render() { + return null; + } } const TranslatedGenericsTest = translate(["view", "nav"] as Key[])(GenericsTest); ; class GenericsTest2 extends React.Component { - render() { return null; } + render() { + return null; + } } const TranslatedGenericsTest2 = translate("view" as Key)(GenericsTest2); @@ -135,3 +157,25 @@ const TranslatedStatlessComponent = translate()(StatlessComponent); interface CustomTranslateFunctionProps { _: TranslationFunction; } + + + { + (t, {i18n}) => ( +
+

{t('keyFromDefault')}

+

{t('anotherNamespace:key.from.another.namespace', {/* options t options */})}

+
+ ) + } +
; + +const defaults: ReactI18NextOptions = { + wait: true, + withRef: true, + bindI18n: 'string', + bindStore: 'string' +}; +setDefaults(defaults); + +reactI18nextModule.init(i18n.init()); diff --git a/types/react-i18next/tsconfig.json b/types/react-i18next/tsconfig.json index c38c15b504..e38c27a93e 100644 --- a/types/react-i18next/tsconfig.json +++ b/types/react-i18next/tsconfig.json @@ -23,10 +23,12 @@ "files": [ "index.d.ts", "test/react-i18next-tests.tsx", + "src/context.d.ts", + "src/I18n.d.ts", "src/I18nextProvider.d.ts", "src/interpolate.d.ts", "src/loadNamespaces.d.ts", "src/trans.d.ts", "src/translate.d.ts" ] -} \ No newline at end of file +} diff --git a/types/react-i18next/v4/src/I18nextProvider.d.ts b/types/react-i18next/v4/I18nextProvider.d.ts similarity index 100% rename from types/react-i18next/v4/src/I18nextProvider.d.ts rename to types/react-i18next/v4/I18nextProvider.d.ts diff --git a/types/react-i18next/v4/index.d.ts b/types/react-i18next/v4/index.d.ts index d7bb05a30f..da6a12396d 100644 --- a/types/react-i18next/v4/index.d.ts +++ b/types/react-i18next/v4/index.d.ts @@ -6,11 +6,11 @@ import { TranslationFunction } from "i18next"; -import I18nextProvider from "./src/I18nextProvider"; -import Interpolate from "./src/interpolate"; -import loadNamespaces from "./src/loadNamespaces"; -import Trans from "./src/trans"; -import translate from "./src/translate"; +import I18nextProvider from "./I18nextProvider"; +import Interpolate from "./interpolate"; +import loadNamespaces from "./loadNamespaces"; +import Trans from "./trans"; +import translate from "./translate"; export { I18nextProvider, diff --git a/types/react-i18next/v4/src/interpolate.d.ts b/types/react-i18next/v4/interpolate.d.ts similarity index 100% rename from types/react-i18next/v4/src/interpolate.d.ts rename to types/react-i18next/v4/interpolate.d.ts diff --git a/types/react-i18next/v4/src/loadNamespaces.d.ts b/types/react-i18next/v4/loadNamespaces.d.ts similarity index 100% rename from types/react-i18next/v4/src/loadNamespaces.d.ts rename to types/react-i18next/v4/loadNamespaces.d.ts diff --git a/types/react-i18next/v4/test/react-i18next-tests.tsx b/types/react-i18next/v4/react-i18next-tests.tsx similarity index 100% rename from types/react-i18next/v4/test/react-i18next-tests.tsx rename to types/react-i18next/v4/react-i18next-tests.tsx diff --git a/types/react-i18next/v4/src/trans.d.ts b/types/react-i18next/v4/trans.d.ts similarity index 100% rename from types/react-i18next/v4/src/trans.d.ts rename to types/react-i18next/v4/trans.d.ts diff --git a/types/react-i18next/v4/src/translate.d.ts b/types/react-i18next/v4/translate.d.ts similarity index 100% rename from types/react-i18next/v4/src/translate.d.ts rename to types/react-i18next/v4/translate.d.ts diff --git a/types/react-i18next/v4/tsconfig.json b/types/react-i18next/v4/tsconfig.json index bd85233525..1e7a8d9eea 100644 --- a/types/react-i18next/v4/tsconfig.json +++ b/types/react-i18next/v4/tsconfig.json @@ -8,27 +8,29 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, + "strictFunctionTypes": true, "baseUrl": "../../", "typeRoots": [ "../../" ], "paths": { - "react-i18next": [ "react-i18next/v4" ] + "react-i18next": [ + "react-i18next/v4" + ] }, "types": [], "noEmit": true, "forceConsistentCasingInFileNames": true, - "strictFunctionTypes": true, "jsx": "react", "experimentalDecorators": true }, "files": [ "index.d.ts", - "test/react-i18next-tests.tsx", - "src/I18nextProvider.d.ts", - "src/interpolate.d.ts", - "src/loadNamespaces.d.ts", - "src/trans.d.ts", - "src/translate.d.ts" + "react-i18next-tests.tsx", + "I18nextProvider.d.ts", + "interpolate.d.ts", + "loadNamespaces.d.ts", + "trans.d.ts", + "translate.d.ts" ] }