React higher-order component for text translation
npm install --save react-translate-text
In component:
import React from 'react'
import {withTranslate} from 'react-translate-text'
function App ({age, message, translate}) {
return (
<div>
<p>{translate('hello')}</p>
<p>{translate('echo', message)}</p>
<p>{translate('more.age', age)}</p>
</div>
)
}
export default withTranslate(App)
In root:
import React from 'react'
import {render} from 'react-dom'
import pluralize from 'numd'
import {TranslateProvider} from 'react-translate-text'
import App from './App'
const translation = {
hello: 'Hello',
echo: 'You say: $1',
more: {
age: 'I\'m a (pluralize $1|year|years) old'
}
}
render(
<TranslateProvider translation={translation} helpers={{pluralize}}>
<App message='Foo' age={25} />
</TranslateProvider>,
document.querySelector('#root')
)
Under the hood react-translate-text
uses translate-text
.
It's a simple translate function with templates precompiling and helpers.
You can find API docs in its repository.
A higher-order component passes translate function by the context.
Type: object
Translation templates.
Type: object
Helper functions that are used in templates.
Type: func
Translate function.
Required
Type: element
Create a higher-order component to connect your component to translate.
Type: element
You can create translate function earlier and pass it into the <TranslateProvider />
.
Read more about createTranslate
in translate-text
's docs.
- translate-text — translate function with templates precompiling and helpers
MIT