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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-intlstrings

v0.2.0

Published

Effortless internationalization for React.js

Downloads

5

Readme

react-intlstrings

Effortless internationalization for React.js. This library lets you define string translations for an internationalized app, and use them within JSX structures in an easy and flexible way.

Basic usage

import React from 'react';
import * as Intl from 'react-intlstrings';

// Define our strings

Intl.add('en', {
    'WELCOME_MSG': 'Hey there, welcome.'
});

Intl.add('es', {
    'WELCOME_MSG': 'Hola hola. Bienvenido.'
});

// Create an interface for our app

const Str = Intl.lang('en');

// Use it in react
...
render() {
    return (
        <div>
            <h1><Str id="WELCOME_MSG" /></h1>
        </div>
    );
}
...

Lang structure

This lib was created to use with React.js specifically. So, there's some added sugar to make things a bit more fun within our app.

Messages can be JSX structures

You can return JSX structures instead of plain strings:

Intl.add('en', {
    LOGIN: <span><strong>Click here</strong> to login</span>
});

Messages can also be functions

Maybe you want a JSX structure that makes some stuff under the hood before returning the result. You can set a stateless component function:

Intl.add('en', {
    SELECTED_SHIRT: props => `You've selected a ${props.color} shirt!`
});

...
render() {
    return (
        <div className="msg"><Str id="SELECTED_SHIRT" color="red" /></div>
    );
}
...

Plain function call

What if you want to pass a localized string via props? Or maybe mix it somewhere in a string? The function returned by Intl.lang() is actually an overloaded function. It works both as a JSX element, and you can also call it directly passing a plain string id. So, you can:

Intl.add('en', {
    BUY: 'Buy now',
    HINT: 'Click here plz'
});

...
render() {
    return (
        <a onClick={this.doStuff} title={Str('HINT')}><Str id="BUY" /></a>
    );
}
...

Passing arguments to a plain function

If you define your message as a function, JSX props will be passed as the first argument. But, what happens when you want to define a function but you're not calling it via JSX? There's an optional second argument in the plain call to do that.

Intl.add('en', {
    CART: props => `You have ${props.count} item${props.count === 1 ? '' : 's'} in your cart`
});

...
render() {
    const msg = Str('CART', { count: 5 });

    return (
        <div>{msg}</div>
    );
}
...

That's pretty much of it.

Methods

add(string code, object strings)

Adds a lang collection just like in the examples.

extend(string code, string newcode, object strings)

Convenience function that'll copy a lang and mix it with the new strings. Useful for partial or slight translations.

lang(string code)

Returns an overloaded function that can be used as a React component or called directly to retrieve a translated string in the specified language.

As a React Component

Let's say we store the overloaded function with the name Str. We can use it in JSX as with any other component. The only required prop is id (or string) to pick the message: <Str id="HELLO" />. Any other prop is passed to the message if it's a function/component, or ignored otherwise.

As a Plain Function

We can call Str('HELLO') directly and get our string. If the message was defined as a function, we can pass an optional options object to it, as in Str('HELLO', { example: true }).

That's all, folks.