|
| 1 | +/* eslint-disable no-undef */ |
| 2 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 3 | +/* eslint-disable no-unused-vars */ |
| 4 | +// eslint-disable-next-line no-use-before-define |
| 5 | +import React from 'react' |
| 6 | +import { Trans } from 'react-i18next' |
| 7 | +import { customScopeRangeFn } from './customScopeRange' |
| 8 | +import './App.css' |
| 9 | + |
| 10 | +// Component using the Trans component |
| 11 | +function MyComponent() { |
| 12 | + return ( |
| 13 | + <Trans i18nKey="translation:description.part1"> |
| 14 | + To get started, edit <code>src/App.js</code> and save to reload. |
| 15 | + </Trans> |
| 16 | + ) |
| 17 | +} |
| 18 | + |
| 19 | +// page uses the hook |
| 20 | +function Page() { |
| 21 | + const { t } = customScopeRangeFn() |
| 22 | + |
| 23 | + return ( |
| 24 | + <div className="App"> |
| 25 | + <div className="App-intro"> |
| 26 | + <MyComponent /> |
| 27 | + </div> |
| 28 | + <div>{t('translation:description.part2')}</div> |
| 29 | + {/* plain <Trans>, #423 */} |
| 30 | + <Trans i18nKey="translation:description.part2">Fallback text</Trans> |
| 31 | + </div> |
| 32 | + ) |
| 33 | +} |
| 34 | + |
| 35 | +// function with scope |
| 36 | +function Page2() { |
| 37 | + const { t } = customScopeRangeFn(['translation:foo']) |
| 38 | + |
| 39 | + // inside default namespace ("foo.bar") |
| 40 | + t('bar') |
| 41 | + |
| 42 | + // explicit namespace |
| 43 | + t('pages.home:title') |
| 44 | + t('pages/home:title') |
| 45 | +} |
| 46 | + |
| 47 | +// function with another scope |
| 48 | +function Page3() { |
| 49 | + const { t } = customScopeRangeFn('pages/home') |
| 50 | + |
| 51 | + t('title') |
| 52 | + |
| 53 | + // explicit namespace |
| 54 | + t('translation:title') |
| 55 | +} |
| 56 | + |
| 57 | +// function with scope in options |
| 58 | +function Page4() { |
| 59 | + const { t } = customScopeRangeFn('pages/home') |
| 60 | + |
| 61 | + t('title') |
| 62 | + |
| 63 | + // explicit namespace |
| 64 | + t('title', { ns: 'translation' }) |
| 65 | +} |
| 66 | + |
| 67 | +// component with scope in props |
| 68 | +function Page5() { |
| 69 | + const { t } = customScopeRangeFn('pages/home') |
| 70 | + |
| 71 | + return ( |
| 72 | + <div className="App"> |
| 73 | + <Trans t={t} i18nKey="title"></Trans> |
| 74 | + {/* explicit namespace */} |
| 75 | + <Trans t={t} i18nKey="title" ns="translation"></Trans> |
| 76 | + </div> |
| 77 | + ) |
| 78 | +} |
0 commit comments