react-simple-i18n
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -32,2 +32,6 @@ "use strict"; | ||
function getLang() { | ||
return currentLang; | ||
} | ||
function setLang(lang) { | ||
@@ -48,2 +52,3 @@ if (lang === currentLang) return; | ||
t: t, | ||
getLang: getLang, | ||
setLang: setLang, | ||
@@ -50,0 +55,0 @@ listen: listen, |
{ | ||
"name": "react-simple-i18n", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "React i18n solution with context API", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
# React Simple I18n | ||
- React i18n solution with context API | ||
- React i18n solution with context API, support React 16.3.0+ | ||
- Simple usage, easy to configure | ||
## Usage | ||
```jsx | ||
import React, { Component } from 'react' | ||
import { render } from 'react-dom' | ||
import { createI18n, I18nProvider, withI18n } from 'react-simple-i18n' | ||
const langData = { | ||
enUS: { | ||
projects: 'Projects', | ||
}, | ||
zhCN: { | ||
projects: '项目', | ||
}, | ||
} | ||
class Demo extends Component { | ||
handleSetEnglish = () => { | ||
this.props.i18n.setLang('enUS') | ||
} | ||
handleSetChinese = () => { | ||
this.props.i18n.setLang('zhCN') | ||
} | ||
render() { | ||
const { t } = this.props | ||
return ( | ||
<div> | ||
<p>{t('projects')}</p> | ||
<button onClick={this.handleSetEnglish}>English</button> | ||
<button onClick={this.handleSetChinese}>中文</button> | ||
</div> | ||
) | ||
} | ||
} | ||
const DemoWithI18n = withI18n(Demo) | ||
const App = () => ( | ||
<I18nProvider i18n={createI18n(langData, { lang: 'enUS' })}> | ||
<DemoWithI18n /> | ||
</I18nProvider> | ||
) | ||
render(<App />, document.getElementById('app-root')) | ||
``` | ||
## Top Level API | ||
### createI18n(data, options) | ||
Creates an `i18n` object for `I18nProvider` | ||
#### Arguments | ||
- `data` Language data object, see usage above | ||
- `options` | ||
- `lang` Initial language to use | ||
#### Return | ||
- `i18n` | ||
- `t` Get translation by language name | ||
- `getLang` Get current language | ||
- `setLang` Set language by language name | ||
- `listen` Add listener to language change | ||
- `unlisten` Unbind a listener of language change | ||
### \<I18nProvider i18n\> | ||
Makes `i18n` available to `withI18n` HOC | ||
#### Props | ||
- `i18n`: I18n object created by `createI18n` | ||
### withI18n(Component) | ||
Connects a React component to `i18n` object. | ||
Adds `t` and `i18n` to props of wrapped component. | ||
# License | ||
MIT |
@@ -24,2 +24,6 @@ let langData | ||
function getLang() { | ||
return currentLang | ||
} | ||
function setLang(lang) { | ||
@@ -39,2 +43,3 @@ if (lang === currentLang) return | ||
t, | ||
getLang, | ||
setLang, | ||
@@ -41,0 +46,0 @@ listen, |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
15520
16
317
92
0