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-i18n-wrapper

v18.1.0

Published

Use your favorite i18n library in React.

Downloads

593

Readme

i18n tools for React

NPM version Build Status Coverage Status Greenkeeper badge semantic-release

This library is meant to be used as glue code between React and your favorite i18n library. You can use it standalone, too.

Features

  • Compatible with any i18n library.
  • Compatible with isomorphic/server-side rendering.

Example

import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { I18nProvider, Translate } from 'react-i18n-wrapper';

const translations = {
    en: {
        'hello-world': 'Hello World!',
    },
    fr: {
        'hello-world': 'Bonjour Monde!',
    },
};

const MyComponent = props => (
    <div>
        <Translate message="hello-world" />
    </div>
);

console.log(
    ReactDOMServer.renderToString(
        // Wrap our top-level component in an `I18nProvider`
        <I18nProvider language="en" translations={translations}>
            <MyComponent />
        </I18nProvider>
    )
);

API

I18nProvider

Component used to provide i18n context to child components.

Props:

  • translate({translations, language, message, params}) - A function to call to translate a message. If you don't provide translate(), a simple default implementation is used. See below for details.
  • translations - An arbitrary object which will be passed along to your translate() function. Using the default translate() function, this should be an object where keys are locales, and values are maps of message keys to translated strings.
  • language - The locale to translate things into. This is a default and can be changed via useI18n().setLanguage().
  • noEscape - If true, then all Translate components will behave like noEscape was set by default.

Translate

Props:

  • message - The message key to translate. This is passed on to your translate() function.
  • params - Parameters to pass on to the translate() function. This is an arbitrary object.
  • noEscape - If true, the translated message will be rendered without escaping the result.
  • tagName - Controls the element created by Translate. By default, Translate will return a bare string, setting this will wrap the returned value in the specified type. Note that if noEscape is true, this will default to 'span'.
  • className - Class name to add to the generated element. Only used if tagName is set.

withI18n

This is a higher order component which provides the i18n context object via props. In most cases, you probably want to use Translate to translate messages, but in some cases you may want to access this directly:

import { withI18n } from 'react-i18n-wrapper';

class MyForm extends React.Component {
    render() {
        <textarea placeholder={this.props.i18n.translate('placeholder_text')} />;
    }
}

export default withI18n(MyForm);

Here, this.props.i18n.translate(message, params) is a function that can translate a string, and this.props.i18n.language is the current locale.

useI18n

An alternative to withI18n, useI18n provides a React hook which lets you call into the translate function directly within your code:

import { useI18n } from 'react-i18n-wrapper';

export function MyForm(props) {
    const i18n = useI18n();
    return <textarea placeholder={i18n.translate('placeholder_text')} />;
}

Default translate() function

The default translate() function expects translations to be an object where keys are locales, and values are maps of message keys to translated strings. The default translate() can also do some simple substitutions:

const translations = {
    en: {
        hello: 'Hello {name}!',
    },
};

const MyComponent = props => <Translate message="hello" params={{ name: 'Jason' }} />;

The default translate() can also accept a message that is an object with locales as keys:

const translateableObject = {
    en: 'here',
    fr: 'ici',
};

const MyComponent = props => <Translate message={translateableObject} />;