Created
June 11, 2019 15:30
-
-
Save hadnazzar/9edb38416619557ec53608dad1360d17 to your computer and use it in GitHub Desktop.
example i18next in react
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { withNamespaces } from 'react-i18next'; | |
function TranslatableView(props) { | |
const { t, tReady } = props; | |
// tReady is true if translations were loaded. | |
// Use wait option to not render before loaded | |
// or render placeholder yourself if not tReady=false | |
return ( | |
<div> | |
<h1>{t('keyFromDefault')}</h1> | |
<p>{t('anotherNamespace:key.from.another.namespace', { /* options t options */ })}</p> | |
</div> | |
) | |
} | |
export default withNamespaces([ | |
'defaultNamespace', | |
'anotherNamespace' | |
], { /* additional options see below */ })(TranslatableView); | |
// or: short for only loading one namespace: | |
export default withNamespaces('defaultNamespace')(TranslatableView); | |
// or: short for using defaultNS defined in i18next | |
export default withNamespaces()(TranslatableView); | |
// or: using a function to return namespaces based on props | |
export default withNamespaces((props) => props.namespaces)(TranslatableView); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment