Translations can received simple parameters that will be expanded for each locale string
i18n/pt_br.js
exportdefault { values: {'Hi %{user} welcome to our book store':'Olá %{user} bem vindo à nossa loja de livros' }}
The parameters can also be used for a very simple pluralization logic
i18n/en_us.js
exportdefault { values: {'%{count} books were added to cart': [ [1,1,'a book was added to cart'], [2,99,'%{count} books were added to cart'], [100,null,'99+ books were added to cart'] ] }}
Translation values can be functions that will be rendered as React components
i18n/en_us.js
exportdefault { values: {'book-count':functionBookCount ({ count }) {if (count ===0) return <spanstyle={{color:'red'}}>There are no books</span>if (count ===1) return <spanstyle={{color:'green'}}>There is one book</span>return <span> There are {count} books </span> } }}
API
typei18nString= (translation:string) =>string// passing props to function valuestypei18nComponent= (translation:string, props:Record<string,any>) =>React.ReactNodetypei18n=i18nString|i18nComponent
Usage
functionSimpleExample () { return <span>{i18n('book')}</span> }functionGreetings() { return <span> {i18n('Hi %{user} welcome to our book store', { user:'John Doe' })} </span> }functionCartInfo() {return <span>{i18n('%{count} books were added to cart', { count:123 })}</span>}functionBookCounter () {const [ count,setCount ] =React.useState(0)return <divonClick={() =>setCount(s => s +1)} >{i18n('book-count', { count })}</div>}