/
App.js
70 lines (62 loc) · 1.68 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import React, { Component, Suspense } from 'react';
import { useTranslation, withTranslation, Trans } from 'react-i18next';
import './App.css';
import { MyCompFromOtherLib } from './other-lib/component';
// use hoc for class based components
class LegacyWelcomeClass extends Component {
render() {
const { t } = this.props;
return <h2>{t('title')}</h2>;
}
}
const Welcome = withTranslation()(LegacyWelcomeClass);
// Component using the Trans component
function MyComponent() {
return (
<Trans i18nKey="description.part1">
To get started, edit <code>src/App.js</code> and save to reload.
</Trans>
);
}
// page uses the hook
function Page() {
const { t, i18n } = useTranslation();
const changeLanguage = (lng) => {
i18n.changeLanguage(lng);
};
return (
<div className="App">
<div className="App-header">
<img src="logo.svg" className="App-logo" alt="logo" />
<Welcome />
<button type="button" onClick={() => changeLanguage('de')}>
de
</button>
<button type="button" onClick={() => changeLanguage('en')}>
en
</button>
</div>
<div className="App-intro">
<MyComponent />
</div>
<div>{t('description.part2')}</div>
<hr />
<MyCompFromOtherLib />
</div>
);
}
// loading component for suspense fallback
const Loader = () => (
<div className="App">
<img src="logo.svg" className="App-logo" alt="logo" />
<div>loading...</div>
</div>
);
// here app catches the suspense from page in case translations are not yet loaded
export default function App() {
return (
<Suspense fallback={<Loader />}>
<Page />
</Suspense>
);
}