|
| 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