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

with-react-intl

v0.0.8

Published

Internationalize React apps with a simple HOC and retrieve localized message either by a Component (with yahoo/react-intl) or as a String.

Downloads

234

Readme

with-react-intl

Internationalize React apps with a simple HOC and retrieve localized message either by a Component (with yahoo/react-intl) or as a String

Internationalize React apps with react-intl and intl-messageformat with a simple HOC - withReactIntl. From react-intl we can load locale messages through a Component. With the use of intl-messageformat we can load locale messages as a String.

Features:

  • Internationalize with one simple HOC
  • Figure the user locale from the browser or from the user input
  • Inject props: locale and setLocale, to get and set locale manually
  • Get formatted message as a String
  • Load and add locale data and messages dynamically, no need for hard-coding anything

TL;DR steps:

  1. Add ${locale}.js files to translations directory
  2. Wrap App with withReactIntl HOC Done!

Install

npm i react-intl with-react-intl --save

Or:

yarn add react-intl with-react-intl

Step 1 - Create translations files

Create js files containing an object with all the translated messages. requirements:

  • files should be *.js
  • files should be at the directory ROOT/translations/
  • files should be named after the locale you want (e.g. en.js, es.js)
  • files should contain an exported object called translation example:
// en.js
const NUM_OF_PERSONS = `{numOfPersons, plural, 
    =undefined {0} 
    =0 {0}
    one {, 1 Person} 
    other {, # Persons} 
}`
export const translation = {
    welcome: `Hello {name}`,
    CommentList: {
        numberOfComments: `There {numOfComments, plural, one {is one comment} other {are {numOfComments} comments}}`,
        numberOfPersons: `Total: ${NUM_OF_PERSONS}`
    }
    // ...
}

recommended conventions:

  • message ids that are not generic and should be used only in a specific component (aka numberOfComments in CommentList) should be nested in an id with the name of the component (aka CommentList PascalCase).
  • message ids should not start with an Uppercase, unless it is a component, obviously for confusion reasons.

Step 2 - Wrap your App

Wrap your app component with the HOC - withReactIntl

// App.js
import React from 'react'
import withReactIntl from 'with-react-intl'

const App = () =>
  <div>
    // ...
  </div>

export default withReactIntl(App)

Start coding

Now you can simply get a locale message, either as a String or as a Component: example for getting a Component:

import { FormattedMessage } from 'react-intl'

const FormattedComponent = () => 
    <FormattedMessage id='CommentList.numberOfPersons' values={{ numOfPersons: 8 }} />

example for getting a String:

import { formatIntlLiteral } from 'with-react-intl'

const formattedString = formatIntlLiteral({ id: 'CommentList.numberOfPersons', values: { numOfPersons: 8 } })

Extra details

  • locales are loaded dynamically from react-intl/locale-data/${locale} with addLocaleData from react-intl
  • supported locales from your translations directory are also loaded dynamically
  • locale is resolved from the browser by navigator.language and gets the locale without region code (i.e. 'fr-Fr' is resolved to 'fr')
  • ${locale}.js files are flatten with { flatten } from 'flat'
  • formatIntlLiteral has the following fallback algorithm:
    1. Lookup and format the translated message at id, passed to <IntlProvider> with values.
    2. Fallback to formatting the defaultMessage.
    3. Fallback to source of translated message at id.
    4. Fallback to the literal message id.
  • how locale is defined?
    • Lookup for user's locale input
    • Fallback to navigator.language (user preferred language in the browser)
    • Fallback to user's defaultLocale input
    • Fallback to "en"