React i18n wrapper for jed, based on gettext
npm install react-i18n-jedReact i18n for Gettext based on Jed
``With npm
npm i --save react-i18n-jed jed
$3
All the
txt, pluralTxt and context should be literal string.`js
gettext(txt)
pgettext(context, txt)
ngettext(txt, pluralTxt, count)
npgettext(context, txt, pluralTxt, count)
`$3
We should get the Jed instance to . Then we can get it from Component.props in the whole components tree cooperated with translate().`js
import { I18nProvider } from 'react-i18n-jed';
import Jed from 'jed';const i18n = new Jed(localeJSON);
`The
localeJSON should match the standard Gettext data format, like
`js
{
domain: 'messages',
language: 'en-US',
locale_data: {
messages: {
'': {
domain: 'messages',
},
'Ad Expense': ['Test Ad Expense'],
'App or Publisher': ['App or Publisher'],
Cat: ['Cat', 'Cats'],
},
},
}
`$3
`js
import { translate, type I18nType } from 'react-i18n-jed';class Comp extends React.Component {
props: {
i18n: I18nType,
};
render() {
const { gettext } = this.props.i18n;
return
{gettext('hello')};
}
}export default translate(Comp);
`In test files, you won't be able to use
shallow rendering with the translated component. Instead, you'll want to access the wrapped component directly like so:`js
import mockI18n from 'react-i18n-jed/mockI18n';
import TranslatedComponent from '.';const Comp = TranslatedComponent.WrappedComponent;
const tree = shallow( );
``MIT © App Annie
[npm-image]: https://badge.fury.io/js/react-i18n-jed.svg
[npm-url]: https://npmjs.org/package/react-i18n-jed
[travis-image]: https://travis-ci.org/appannie/react-i18n-jed.svg?branch=master
[travis-url]: https://travis-ci.org/appannie/react-i18n-jed
[daviddm-image]: https://david-dm.org/appannie/react-i18n-jed.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/appannie/react-i18n-jed
[coveralls-image]: https://coveralls.io/repos/appannie/react-i18n-jed/badge.svg
[coveralls-url]: https://coveralls.io/r/appannie/react-i18n-jed