2.0.0 • Published 2 months ago
@rcp/hoc.i18n v2.0.0
@rcp/hoc.i18n
React Component's high order component about internationalization
Installation
npm install @rcp/hoc.i18n
# or use yarn
yarn add @rcp/hoc.i18n
Usage
import i18n from '@rcp/hoc.i18n'
const currentLang = 'en-us'
@i18n(
{
'en-us': {
hello: 'hello, ${1}'
},
'zh-cn': {
hello: '你好, ${1}'
}
},
currentLang
)
class View extends React.Component {
render() {
return <p>{this.i('hello', this.props.title)}</p>
}
}
// `<View title="Jilly" />`
// equals to `<p>hello, Jilly</p>`
// `<View title="Jilly" locale={{ hello: 'hello from locale, ${1}' }} />`
// equals to `<p>hello from locale, Jilly</p>`
// `<View title="Jilly" language="zh-cn" />`
// equals to `<p>你好, Jilly</p>`
Customize Component's i18n
import i18n from '@rcp/hoc.i18n'
const currentLang = 'en-us'
@i18n(
{
'en-us': {
hello: 'hello, ${1}'
}
},
currentLang
)
class View extends React.Component {
render() {
return <p>{this.i('hello', this.props.title)}</p>
}
}
// Customize view's dictionaries
View.i18n // See https://github.com/imcuttle/tiny-i18n/tree/master/packages/tiny-i18n
View.i18n.setLanguage('zh-cn')
View.i18n.setDictionary(
{
hello: '你好, ${1}'
},
'zh-cn'
)
Preset dictionary by default
import i18n from '@rcp/hoc.i18n'
@i18n({
hello: 'hello, ${1}',
tip: 'awesome'
})
class View extends React.Component {
render() {
return (
<p>
{this.i('hello', this.props.title)}
<span>{this.i('tip')}</span>
</p>
)
}
}
// Customize dictionary from `props.locale`
// `<View title="Jilly" locale={{ hello: 'hello from locale, ${1}' }} />`
// equals to `<p>hello from locale, Jilly<span>awesome</span></p>`
Preset multiple dictionaries
import i18n from '@rcp/hoc.i18n'
@i18n({
zh: {
hello: '你好, ${1}',
tip: '酷'
},
en: {
hello: 'hello, ${1}',
tip: 'awesome'
}
})
class View extends React.Component {
render() {
return (
<p>
{this.i('hello', this.props.title)}
<span>{this.i('tip')}</span>
</p>
)
}
}
// `<View title="Jilly" language="zh" />`
// => '<p>你好 Jilly<span>酷</span></p>'
Related
- tiny-i18n - Tiny yet useful i18n library
Authors
This library is written and maintained by imcuttle, moyuyc95@gmail.com.
License
MIT
2.0.0
2 months ago
1.0.31
3 years ago
1.0.30
3 years ago
1.0.28
4 years ago
1.0.27
4 years ago
1.0.25
4 years ago
1.0.23
4 years ago
1.0.21
4 years ago
1.0.20
4 years ago
1.0.19
4 years ago
1.0.17
4 years ago
1.0.15
4 years ago
1.0.14
4 years ago
1.0.13
5 years ago
1.0.12
5 years ago
1.0.11
5 years ago
1.0.10
6 years ago
1.0.9
6 years ago
1.0.8
6 years ago
1.0.7
6 years ago
1.0.6
6 years ago
1.0.5
6 years ago