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

@davidbarratt/react.i18n

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@davidbarratt/react.i18n

React bindings for banana-i18n

  • 2.0.2-1
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

react.i18n

React i18n provides React bindings for banana-i18n.

Usage

import { IntlProvider, Message } from '@wikimedia/react.i18n';

const locale = 'en-US';

const messages = {
  en: {
    'hello-world': 'Hello $1!',
    'world': 'World',
  }
};

const App = () => (
  <IntlProvider locale={locale} messages={messages}>
    <Message id="hello-world" placeholders={[
      'David'
    ]} />
  </IntlProvider>
);

export default App;

// Rendered Output:
// Hello David!

The placeholders can also be React components:

<Message id="hello-world" placeholders={[
  <strong><Message id="world" /></strong>,
]} />

// Rendered Output:
// Hello <strong>World</strong>!

The BananaContext can also be used directly to return a string as described in React's Context.Consumer documentation.

import { IntlProvider, BananaContext } from '@wikimedia/react.i18n';

const locale = 'en-US';

const messages = {
  en: {
    'hello-world': 'Hello $1!',
    'world': 'World',
  }
};

const App = () => (
  <IntlProvider locale={locale} messages={messages}>
    <BananaContext.Consumer>
      {banana =>
        banana.i18n( 'hello-world', 'David' )
      }
    </BananaContext.Consumer>
  </IntlProvider>
);

export default App;

// Rendered Output:
// Hello David!

or with React's Context Hook:

import { useContext } from 'react';
import { IntlProvider, BananaContext } from '@wikimedia/react.i18n';

const locale = 'en-US';

const messages = {
  en: {
    'hello-world': 'Hello $1!',
    'world': 'World',
  }
};

const App = () => {
  const banana = useContext(BananaContext);

  return (
    <IntlProvider locale={locale} messages={messages}>
      {banana.i18n( 'hello-world', 'David' )}
    </IntlProvider>
  );
};

export default App;

// Rendered Output:
// Hello David!

Migrating from 1.x

  • Message can no longer be used as a function. To use the Banana object directly, get the context consumer as described above.

FAQs

Package last updated on 13 Aug 2019

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