Skip to content

Commit 71fdd5b

Browse files
committed
Merge branch 'master' of github.com:i18next/react-i18next
2 parents 2557ba1 + 450303d commit 71fdd5b

File tree

3 files changed

+91
-7
lines changed

3 files changed

+91
-7
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"eslint-plugin-jsx-a11y": "6.1.1",
5959
"eslint-plugin-prettier": "2.7.0",
6060
"eslint-plugin-react": "7.11.1",
61-
"i18next": "13.1.0",
61+
"i18next": "^13.1.4",
6262
"jest": "23.6.0",
6363
"jest-cli": "23.6.0",
6464
"mkdirp": "0.5.1",
@@ -79,7 +79,7 @@
7979
"yargs": "12.0.2"
8080
},
8181
"peerDependencies": {
82-
"i18next": ">= 6.0.1",
82+
"i18next": ">= 13.1.3",
8383
"react": ">= 16.3.0"
8484
},
8585
"scripts": {

src/index.d.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ export function setI18n(instance: i18next.i18n): void
2424

2525
export function getI18n(): i18next.i18n
2626

27-
export interface I18nContextValues {
27+
export interface I18nContextValues extends i18next.WithT {
2828
i18n: i18next.i18n
29-
t: i18next.TranslationFunction
3029
defaultNS?: string
3130
reportNS?: string
3231
lng?: string
@@ -74,7 +73,7 @@ export interface NamespacesConsumerProps extends ReactI18NextOptions {
7473
initialI18nStore?: {}
7574
initialLanguage?: string
7675
children: (
77-
t: i18next.TranslationFunction,
76+
t: i18next.WithT['t'],
7877
options: {
7978
i18n: i18next.i18n
8079
lng: string
@@ -94,12 +93,11 @@ export interface I18nextProviderProps {
9493

9594
export const I18nextProvider: React.ComponentClass<I18nextProviderProps>
9695

97-
export interface TransProps {
96+
export interface TransProps extends Partial<i18next.WithT>{
9897
i18nKey?: string
9998
count?: number
10099
parent?: React.ReactNode
101100
i18n?: i18next.i18n
102-
t?: i18next.TranslationFunction
103101
defaults?: string
104102
values?: {}
105103
components?: React.ReactNode[]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import * as React from "react";
2+
import {
3+
NamespacesConsumer,
4+
Trans,
5+
withNamespaces,
6+
WithNamespaces,
7+
} from 'react-i18next';
8+
9+
type TKeys = "title" | "text";
10+
11+
function NamespacesConsumerTest() {
12+
return (
13+
<NamespacesConsumer>
14+
{(t, { i18n }) =>
15+
<div>
16+
<h2>{t<TKeys>("title")}</h2>
17+
<span>{t("any")}</span>
18+
<span>{t("any", {anyObject: {}})}</span>
19+
<span>{t<TKeys>("text")}</span>
20+
<span>{t<TKeys, {key: string}>("text", {key: "foo"})}</span>
21+
<span>{t<TKeys, {key: "bar"}, string>("text", {key: "bar"})}</span>
22+
</div>
23+
}
24+
</NamespacesConsumer>
25+
);
26+
}
27+
28+
function TransComponentTest({ t }: WithNamespaces) {
29+
return (
30+
<div>
31+
<Trans i18nKey="description.part1">
32+
To get started, edit <code>src/App.js</code> and save to reload.
33+
</Trans>
34+
<Trans i18nKey="description.part1">
35+
To get started, <strong title={t<TKeys>("title")}>{{name}}</strong>and save to reload.
36+
</Trans>
37+
</div>
38+
);
39+
}
40+
41+
const MyComponentWrapped = withNamespaces()(TransComponentTest);
42+
43+
type ArticleKeys = "article.part1" | "article.part2";
44+
type AnotherArticleKeys = "anotherArticle.part1" | "anotherArticle.part2";
45+
46+
/**
47+
* Overload makes completion of arguments by without specifying type parameters
48+
*/
49+
interface OverloadedWithNamespaces extends WithNamespaces {
50+
t(key: ArticleKeys, b?: object): any;
51+
// NOTION: disable no-unnecessary-generics for generic test
52+
// tslint:disable-next-line:no-unnecessary-generics
53+
t<T extends AnotherArticleKeys>(key: T, b: {name: string}): any;
54+
}
55+
56+
class App extends React.Component<OverloadedWithNamespaces> {
57+
render() {
58+
const { t, i18n } = this.props;
59+
60+
const changeLanguage = (lng: string) => {
61+
i18n.changeLanguage(lng);
62+
};
63+
64+
return (
65+
<div className="App">
66+
<div className="App-header">
67+
<NamespacesConsumerTest />
68+
<button onClick={() => changeLanguage("de")}>de</button>
69+
<button onClick={() => changeLanguage("en")}>en</button>
70+
</div>
71+
<div className="App-intro">
72+
<MyComponentWrapped />
73+
</div>
74+
<article>
75+
<div>{t("article.part1", {name: "foo"})}</div>
76+
<div>{t("article.part2")}</div>
77+
</article>
78+
<article>
79+
<div>{t<AnotherArticleKeys>("anotherArticle.part1", {name: "foo"})}</div>
80+
<div>{t<AnotherArticleKeys>("anotherArticle.part2", {name: "bar"})}</div>
81+
</article>
82+
</div>
83+
);
84+
}
85+
}
86+
export default withNamespaces("translation")(App);

0 commit comments

Comments
 (0)