Skip to content

Commit ae50160

Browse files
author
João Moura
committed
Updated readme and example
1 parent c338545 commit ae50160

File tree

4 files changed

+60
-31
lines changed

4 files changed

+60
-31
lines changed

.vscode/settings.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"spellright.language": [
3+
"en"
4+
],
5+
"spellright.documentTypes": [
6+
"markdown",
7+
"latex",
8+
"plaintext"
9+
]
10+
}

README.md

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# react-multi-lang
22

3-
React Multilanguage Higher Order Component.
3+
React Multi-language component.
44

55
Works with React and React Native
66

@@ -10,15 +10,42 @@ Works with React and React Native
1010

1111
## Usage
1212

13+
### Hook
14+
1315
See the example folder for better understanding
1416

15-
```javascript
16-
// @flow
17+
```tsx
18+
import React from 'react'
19+
20+
// Translation Hook
21+
import { setTranslations, setDefaultLanguage, useTranslation } from 'react-multi-lang'
22+
import pt from 'pt.json'
23+
import en from 'en.json'
24+
25+
// Do this two lines only when setting up the application
26+
setTranslations({pt, en})
27+
setDefaultLanguage('en')
28+
29+
const App: React.FC = () => {
30+
const t = useTranslation()
31+
return (
32+
<div>
33+
{t('home.Title')}
34+
{t('Hello', {name: 'João'})}
35+
</div>
36+
)
37+
}
38+
39+
export default App
40+
```
41+
42+
### Higher order component
1743

18-
import * as React from 'react'
44+
```tsx
45+
import React from 'react'
1946

2047
// Translation Higher Order Component
21-
import { setTranslations, setDefaultLanguage, translate } from 'react-multi-lang'
48+
import { setTranslations, setDefaultLanguage, withTranslation } from 'react-multi-lang'
2249
import pt from 'pt.json'
2350
import en from 'en.json'
2451
import type { T } from 'react-multi-lang'
@@ -31,11 +58,9 @@ type Props = {
3158
t: T
3259
}
3360

34-
type State = {}
35-
36-
class SomeComponent extends React.Component<Props, State> {
61+
class SomeComponent extends React.Component<Props> {
3762
render () {
38-
const { translate } = this.props
63+
const { t } = this.props
3964
return (
4065
<div>
4166
{t('home.Title')}
@@ -45,14 +70,10 @@ class SomeComponent extends React.Component<Props, State> {
4570
}
4671
}
4772

48-
export default translate(SomeComponent)
73+
export default withTranslation(SomeComponent)
4974
```
5075

51-
## Injected Method
52-
53-
If using the Higher Order Component `translate(SomeComponent)`
54-
55-
### t(path, params)
76+
## Translation Method t(path, params)
5677

5778
Params | Type | Description
5879
------ | ------ | ------------------------------------------------------------------------------------

example/src/App.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import React from 'react';
2-
import logo from './logo.svg';
2+
import { getLanguage, setLanguage, useTranslation } from 'react-multi-lang'
33
import './App.css';
4-
import { useTranslation, setLanguage, getLanguage } from 'react-multi-lang'
4+
import logo from './logo.svg';
55

66
const App: React.FC = () => {
77

88
const t = useTranslation()
99

1010
return (
11-
<div className="App">
12-
<header className="App-header">
13-
<img src={logo} className="App-logo" alt="logo" />
14-
<h1 className="App-title">
11+
<div className='App'>
12+
<header className='App-header'>
13+
<img src={logo} className='App-logo' alt='logo' />
14+
<h1 className='App-title'>
1515
{t('home.Title', {param: 'react'})}
1616
</h1>
1717
</header>
18-
<p className="App-intro">
18+
<p className='App-intro'>
1919
To change language just press the buttons beneath
2020
</p>
2121
<div>Selected lang <b>{getLanguage()}</b></div>

example/src/index.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import React from 'react';
2-
import ReactDOM from 'react-dom';
3-
import './index.css';
4-
import App from './App';
5-
import * as serviceWorker from './serviceWorker';
1+
import React from 'react'
2+
import ReactDOM from 'react-dom'
3+
import App from './App'
4+
import './index.css'
5+
import * as serviceWorker from './serviceWorker'
66

7-
8-
import { setDefaultTranslations, setDefaultLanguage } from 'react-multi-lang'
9-
import pt from './translations/pt.json'
7+
import { setDefaultLanguage, setDefaultTranslations } from 'react-multi-lang'
108
import en from './translations/en.json'
9+
import pt from './translations/pt.json'
1110

1211
setDefaultTranslations({pt, en})
1312
setDefaultLanguage('pt')
1413

15-
1614
ReactDOM.render(<App />, document.getElementById('root'));
1715

1816
// If you want your app to work offline and load faster, you can change

0 commit comments

Comments
 (0)