Simple i18n in react.
npm i @blunck/react-i18n
First load translations & set locale to translate to
import { I18n } from '@blunck/react-i18n'
I18n.setTranslations({
en: {
noun: {
language: 'Language',
country: 'Country'
}
},
de: {
noun: {
language: 'Sprache',
country: 'Land'
}
}
})
I18n.setLocale('en')
Use the Translate
component to add translations to your app
import { Translate } from '@blunck/react-i18n'
const MyComponent = () => {
return <Translate value="noun.country" />
}
You can use the I18n
class to translate a key manually
const str = I18n.translate('noun.country')
Alternatively you can use the __
helper function
import { __ } from '@blunck/react-i18n'
const str = __('noun.country')
Use the I18nProvider
component to access the locale from within your own components
import { I18nProvider } from '@blunck/react-i18n'
<I18nProvider render={locale => (
<span>The current locale is {locale}</span>
)} />
Architecture inspired by react-i18nify