npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-intl-format

v0.1.2

Published

A react-intl component that provides a alternate way to localize your components.

Downloads

37

Readme

React Intl Format

A react-intl component that provides a alternate way to localize your application.

npm install --save prop-types react-intl react-intl-format

If you are using commonjs or es modules, simply require or import the library.

// commonjs
var format = require('react-intl-format');
// es modules
import * as format from 'react-intl-format';

Umd packages are also included in the bin directory.

Usage

Below is an example of standard react-intl usage. Notice how we wrap the component with the injectIntl hoc to gain access to intlShape. <FormatMessage /> implicitly receives this throught context.

function LoginForm(props) {
  return (
    <form>
      <h1><FormatMessage id="login-form.heading" /></h1>
      <div className="form-group">
        <input
          className="form-control" 
          placeholder={props.intl.formatMessage({ id: 'login-form.email.placeholder' })}
          name="email"
        />
      </div>
      <div className="form-group">
        <input
          className="form-control" 
          placeholder={props.intl.formatMessage({ id: 'login-form.password.placeholder' })}
          name="password"
        />
      </div>
    </form>
  );
}

export default injectIntl(LoginForm);

This example shows basic usage of react-intl-format. Notice how we no longer need to wrap the component with injectIntl to access the intlShape value, its provided as a paramter in the render callback of <Format />.

import { Format } from 'react-intl-format';

export default function LoginForm(props) {
  return (
    <form>
      <Format>
        {intl => (
          <h1><FormatMessage id="login-form.heading" /></h1>
          <div className="form-group">
            <input
              className="form-control" 
              placeholder={intl.formatMessage({ id: 'login-form.email.placeholder' })}
              name="email"
            />
          </div>
          <div className="form-group">
            <input
              className="form-control" 
              placeholder={intl.formatMessage({ id: 'login-form.password.placeholder' })}
              name="password"
            />
          </div>
        )}
      </Format>
    </form>
  );
}

A configure function is avaible to you if you need to customize how format passes down intl down to the render callback. Notice how we wrapped intl with a custom api that we can use during rendering. We cpuld also replace <FormatMessage /> with a simple {_.m('login-form.heading')} if we would like.

import * as format from 'react-intl-format';

const Localize = format.configure((intl, props) => {
  return {
    m: id => intl.formatMessage({ id })
  };
}, {
  displayName: 'Localize'
});

export default function LoginForm(props) {
  return (
    <form>
      <Localize>
        {_ => (
          <h1><FormatMessage id="login-form.heading" /></h1>
          <div className="form-group">
            <input
              className="form-control" 
              placeholder={_.m('login-form.email.placeholder')}
              name="email"
            />
          </div>
          <div className="form-group">
            <input
              className="form-control" 
              placeholder={_.m('login-form.password.placeholder')}
              name="password"
            />
          </div>
        )}
      </Localize>
    </form>
  );
}

Api

configure

Create a custom Format component.

const CustomFormat = configure(
  /** 
   * maps intl and and props provided to the component to a single value that
   * will be injected into the render callback.
   * 
   * @param {intlShape} intl
   * @param {object} props
   * @returns {any}
   */
  apiSelector,

  /**
   * optional values that further configure the component.
   */
  options: {
    /**
     * the displayName that custom component will have
     */
    displayName: 'Format'
  }
);

Format

Default implementation that just passes inltShape through to the render callback.

<Format>
  {intl => {
    /* ... */
  }}
</Format>