Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-i18n-jed

Package Overview
Dependencies
Maintainers
4
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-i18n-jed

React i18n wrapper for jed, based on gettext

  • 3.3.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
4
Created
Source

react-i18n-jed NPM version Build Status Dependency Status Coverage percentage

React i18n for Gettext based on Jed

Installation

# With npm
npm i --save react-i18n-jed jed

# Or with yarn
yarn add react-i18n-jed jed

i18n API:

All the txt, pluralTxt and context should be literal string.

gettext(txt)
pgettext(context, txt)
ngettext(txt, pluralTxt, count)
npgettext(context, txt, pluralTxt, count)

I18nProvider

We should get the Jed instance to . Then we can get it from Component.props in the whole components tree cooperated with translate().

import { I18nProvider } from 'react-i18n-jed';
import Jed from 'jed';

const i18n = new Jed(localeJSON);

<I18nProvider i18n={this.i18n}>
    <WrappedComponent i18n={this.i18n} {...this.props} />
</I18nProvider>

The localeJSON should match the standard Gettext data format, like

{
    domain: 'messages',
    language: 'en-US',
    locale_data: {
        messages: {
            '': {
                domain: 'messages',
            },
            'Ad Expense': ['Test Ad Expense'],
            'App or Publisher': ['App or Publisher'],
            Cat: ['Cat', 'Cats'],
        },
    },
}

Localize a component:

import { translate, type I18nType } from 'react-i18n-jed';

class Comp extends React.Component {
    props: {
        i18n: I18nType,
    };

    render() {
        const { gettext } = this.props.i18n;
        return <div>{gettext('hello')}</div>;
    }
}

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:

import mockI18n from 'react-i18n-jed/mockI18n';
import TranslatedComponent from '.';

const Comp = TranslatedComponent.WrappedComponent;
const tree = shallow(<Comp i18n={mockI18n} />);

License

MIT © App Annie

Keywords

FAQs

Package last updated on 13 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc