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-db

v0.0.7

Published

Load app translations automatically partitioned by domain

Downloads

15

Readme

react-intl-db

Introduction

This package contains i18n domain and message id support for the React integration of Format.js. It also makes it possible to load translation messages dynamically from a backend or other source.

Usage

First we can define a loader that shows how to load the messages from the server. It should return a Promise:

function loader(localeId, domainId) {
   // return a promise with the messages
}

The loader gets the id of the locale to be loaded and the id of the domain. The locale is an ISO locale; something like en-US. The domain id you make up yourself. Typically each UI application would use its own separate domain string.

The loader should return a promise that resolves to an object with message id keys and message values, for this particular locale and domain. The loader could for instance load its information from the server:

function loader(localeId, domainId) {
    return fetch(`http://example.com/i18n/${localeId}/${domainId}`);
}

The next step is to create an IntDomainDatabase with this loader:

import {IntlDomainDatabase} from 'react-intl-db';

i18ndb = new IntlDomainDatabase(loader);

You can supply the i18ndb with default messages as well. These messages are used when no loader argument is passed in, or if the loader returns a promise that resolves to null or undefined for that domain, or when the loader doesn't define a particular message.

i18ndb.defaultMessages({
   domainId: 'mydomain',
   messages: {
       'hello': "Hello world!",
       'photos': ('{name} took {numPhotos, plural,' +
                  ' =0 {no photos}' +
                  ' =1 {one photo}' +
                  ' other {# photos}' +
                  '} on {takenDate, date, long}.')
   }
})

We can now start using this i18ndb with React. Before we can start the React.render though, we need to make sure we have loaded the information for the right domain:

i18ndb.setLocale('en-US').then(() => {
    React.render(<App />, document.body);
});

We can now create a Format component for a particular domain that a UI application can then use:

const Format = i18ndb.makeFormat('myapp')

The argument to makeFormat is the name of the application's domain.

Once you have the Format component you can use it with a messageId prop:

<Format messageId="hello" />

When you use this in code, the system automatically looks up the message id hello for the domain myapp and inserts it.

Some messages take variables. You can pass them in like you do for FormattedMessage from react-intl:

Sometimes you need to be able render a formatted message in code, for instance when you want to set the value of a prop. You can do by creating a helper function:

const formatStr = i18ndb.makeFormatStr('myapp')

You use it by passing the messageId as the first argument:

render() {
    return <input value={formatStr('myMessageId')} />;
}

You can also put in variables:

render() {
    return <input value={formatStr('photos',
       { name: 'Annie', numPhotos=1000, takenDate=Date.now()})} />;
}

Limitation

Currently react-intl-db only properly supports a single locale per application, not a list of locales. You can instead make sure the right fallback is happening in the loader.

It also has no support for custom formats yet.

I'm happy to receive code that lifts these limitations!

Example application

There is example code included in src/example.jsx.

First install the required dependencies:

$ npm install

To try this out can use the webpack-dev-server, which automatically rebuilds the bundle and serves the content in build like this:

$ webpack-dev-server --progress --colors --content-base build

You can then access the example app on http://localhost:8080