Provides translation API using context interfaces.
Translation provider, use it to broadcast your translation map. You can have multiple nested
translations providers, where child provider will overwrite parent's translations. Also, you
can have different translation providers at the same time, using the namespace ns
prop.
import {Translations} from 'libreact/lib/translate';
<Translations map={{
key: 'value',
bye: 'ciao'
}}>
<App />
</Translations>
interface ITranslationsProps {
map: {[key: string]: string | ((T, ...args) => string)};
ns?: string;
}
, where
map
- required, object, a dictionary of translations.ns
- optional, string, namespace.
Translation consumer render prop component that receives translation function T
, which has the following signature
T: (key: string, ...args) => string;
<Translate>
component has also a shorthand alias <T>
component.
import {Translations, T} from 'libreact/lib/translate';
<Translations map={{
key: 'value',
bye: 'ciao'
}}>
<T>{(t) =>
<span>Goodbye is ${t('bye')}</span>
}</T>
</Translations>
interface ITranslateProps {
ns?: string;
}
, where
ns
- optional, string, namespace.
withT
is an enhancer that injects T
function into your component.
import {withT} from 'libreact/lib/translate';
const MyCompWithT = withT(MyComp);
Component class decorator that injects T
function into your component.
import {withT} from 'libreact/lib/translate';
@withT
class MyComp extends Component {
}