|
| 1 | +/* eslint-disable no-undef */ |
| 2 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 3 | +/* eslint-disable no-unused-vars */ |
| 4 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 5 | +import React, { Component } from "react"; |
| 6 | +import { useTranslation, withTranslation, Trans } from "react-i18next"; |
| 7 | +import "./App.css"; |
| 8 | + |
| 9 | +// use hoc for class based components |
| 10 | +class LegacyWelcomeClass extends Component { |
| 11 | + render() { |
| 12 | + const { t } = this.props; |
| 13 | + return ( |
| 14 | + <div> |
| 15 | + <h2>Plain Text</h2> |
| 16 | + <h2>{t("translation.title")}</h2> |
| 17 | + </div> |
| 18 | + ); |
| 19 | + } |
| 20 | +} |
| 21 | +const Welcome = withTranslation()(LegacyWelcomeClass); |
| 22 | + |
| 23 | +// Component using the Trans component |
| 24 | +function MyComponent() { |
| 25 | + return ( |
| 26 | + <Trans i18nKey="translation:description.part1"> |
| 27 | + To get started, edit <code>src/App.js</code> and save to reload. |
| 28 | + </Trans> |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +// page uses the hook |
| 33 | +function Page() { |
| 34 | + const { t, i18n } = useTranslation(); |
| 35 | + |
| 36 | + const changeLanguage = lng => { |
| 37 | + i18n.changeLanguage(lng); |
| 38 | + }; |
| 39 | + |
| 40 | + return ( |
| 41 | + <div className="App"> |
| 42 | + <div className="App-header"> |
| 43 | + <Welcome /> |
| 44 | + <button onClick={() => changeLanguage("de")}>de</button> |
| 45 | + <button onClick={() => changeLanguage("en")}>en</button> |
| 46 | + </div> |
| 47 | + <div className="App-intro"> |
| 48 | + <MyComponent /> |
| 49 | + </div> |
| 50 | + <div>{t("translation.description.part2")}</div> |
| 51 | + {/* plain <Trans>, #423 */} |
| 52 | + <Trans i18nKey="translation.description.part2">Fallback text</Trans> |
| 53 | + <p>{t("translation.count", { count: 1 })}</p> |
| 54 | + </div> |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +// hook with scope |
| 59 | +function Page2() { |
| 60 | + const { t } = useTranslation(["translation.foo"]); |
| 61 | + |
| 62 | + // inside default namespace ("foo.bar") |
| 63 | + t("bar"); |
| 64 | + |
| 65 | + // explicit namespace |
| 66 | + t("pages.home:title"); |
| 67 | + t("pages/home:title"); |
| 68 | +} |
| 69 | + |
| 70 | +// hook with another scope |
| 71 | +function Page3() { |
| 72 | + const { t } = useTranslation("pages/home"); |
| 73 | + |
| 74 | + t("title"); |
| 75 | + |
| 76 | + // explicit namespace |
| 77 | + t("translation:title"); |
| 78 | +} |
| 79 | + |
| 80 | +// hook with scope in options |
| 81 | +function Page4() { |
| 82 | + const { t } = useTranslation("pages/home"); |
| 83 | + |
| 84 | + t("title"); |
| 85 | + |
| 86 | + // explicit namespace |
| 87 | + t("title", { ns: "translation" }); |
| 88 | +} |
| 89 | + |
| 90 | +// component with scope in props |
| 91 | +function Page5() { |
| 92 | + const { t } = useTranslation("pages/home"); |
| 93 | + |
| 94 | + return ( |
| 95 | + <div className="App"> |
| 96 | + <Trans t={t} i18nKey="title"></Trans> |
| 97 | + {/* explicit namespace */} |
| 98 | + <Trans t={t} i18nKey="title" ns="translation"></Trans> |
| 99 | + </div> |
| 100 | + ); |
| 101 | +} |
0 commit comments