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

v3.1.2

Published

Internationalise your app, extending config or factory new internationalised components.

Downloads

15

Readme

react-i18n-base

Internationalise your app, extending config or factory new internationalised components.

NPM NPM Build Status

Docs

Current core dependencies versions

  • node: ^8.10.0 || ^9.10.0
  • yarn (version may be check at package.json)

Peer dependencies

"prop-types": "^15.7.2",
"react": "^16.8.4",
"uuid": "^3.3.2"

Getting started

Internationalise your app by creating your locale file. Use the localise function to convert your component to localised one and, then, wrap your app with the I18nProvider.

npm install react-i18n-base
yarn add react-i18n-base

Dev code

The new version of react-i18n-base brings a extra feature. So, localised components now will receive a component I18nTranslate capable of translate content given path and modifiers. Thus, the library still retro compatible.

The difference can be seen with the following Greeting component.

Old way to translate content

Greeting

//----------------------------------------------//
// Creating LocalisedComponent
//----------------------------------------------//
import { localise, decorate } from 'react-i18n-base';

const Italic = ({ children }) => <i>{children}</i>;

const localeGreeting = {
  en: {
    greeting: 'Hi Guest!',
    message: decorate('You are <0>so</0> <1>Awesome</1>!'),
  },
  pt: {
    greeting: 'Oi Convidado!',
    message: decorate('<1>Você é</1> <0>tão</0> Fantástico!'),
  },
};

const Greeting = localise(localeGreeting)(({ i18n }) => (
  <div>
    <h2>{i18n.greeting}</h2>
    <div>{i18n.message('strong', Italic)}</div>
  </div>
));

New way to translate content

Greeting

//----------------------------------------------//
// Creating LocalisedComponent
//----------------------------------------------//
import { localise, decorate } from 'react-i18n-base';

const Italic = ({ children }) => <i>{children}</i>;

const localeGreeting = {
  en: {
    greeting: 'Hi Guest!',
    message: decorate('You are <0>so</0> <1>Awesome</1>!'),
  },
  pt: {
    greeting: 'Oi Convidado!',
    message: decorate('<1>Você é</1> <0>tão</0> Fantástico!'),
  },
};

const Greeting = localise(localeGreeting)(({ I18nTranslate }) => (
  <div>
    <h2>
      <I18nTranslate path="greeting" />
    </h2>
    <div>
      <I18nTranslate modifiers={['strong', Italic]} path="message" />
    </div>
  </div>
));

LabelForm

const localeLabel = {
  en: {
    title: 'Creating label',
    description: 'Description',
    color: 'Color',
    message: {
      error: decorate('<0>Error</0>: label has not been created successfully.'),
      success: decorate('<0>Success</0>: label has been created successfully!'),
    },
    button: 'Save',
  },
  pt: {
    title: 'Criando label',
    description: 'Descrição',
    color: 'Cor',
    message: {
      error: decorate('<0>Erro</0>: label não foi criado com <1>sucesso</1>.'),
      success: decorate('<0>Sucesso</0>: label foi criado com <1>sucesso</1>!'),
    },
    button: 'Gravar',
  },
};
 *
const LabelForm = localise(localeLabel)(({ I18nTranslate, isError, isSuccess }) => (
  <div>
    <h2>
      <I18nTranslate path="title" />
    </h2>
    <form>
      <div>
        <I18nTranslate path="description" />
      </div>
      <input type="text" />
      <div>
        <I18nTranslate path="color" />
      </div>
      <input type="text" />
      {isError && (
        <div>
          <I18nTranslate modifiers={[Italic, 'b']} path="message.error" />
        </div>
      )}
      {isSuccess && (
        <div>
          <I18nTranslate modifiers={[Italic, 'b']} path="message.success" />
        </div>
      )}
      <button type="submit">
        <I18nTranslate path="button" />
      </button>
    </form>
  </div>
));

App

//----------------------------------------------//
// Initialising the app
//----------------------------------------------//
import { I18nProvider } from 'react-i18n-base';

const App = () => (
  <div>
    {/* If only defaultLanguage is set, then, initialLanguage will be the defaultLanguage value  */}
    <I18nProvider defaultLanguage="en">
      <Greeting /> {/* it will get the correct i18n object */}
      <LabelForm isError />
      <LabelForm isSuccess />
    </I18nProvider>
    {/*
    // The elements below will always display in portuguese, unless
    // you have internal components change the provider value
    */}
    <I18nProvider defaultLanguage="en" initialLanguage="pt">
      <Greeting /> {/* it will get the correct i18n object */}
      <LabelForm isError />
      <LabelForm isSuccess />
    </I18nProvider>
  </div>
);

Features from version ^3.0.0

  • Added methods to localised components
    • extend and factory
    • extend: change globally the localisation json data for a specific component
    • factory: creates new localised component with extension of localisation json data

Example

import { localise } from 'react-i18n-base';

const Greeting = ({ name, i18n }) => (
  <div>
    {i18n.greeting} {name}!
  </div>
);
const jsonData = {
  en: { greeting: 'Mr/Mrs' },
};
const LocalisedGreeting = localise(jsonData)(Greeting);
// The code below extend the localisation to all`LocalisedGreeting` instances
LocalisedGreeting.extend({
  pt: { greeting: 'Sr/Sra' },
});

// The code below creates a new component localised based on existing one not affecting the other `LocalisedGreeting` instances
const NewSupportLocalisedGreeting = LocalisedGreeting.factory({
  es: { greeting: 'Sr/Sra' },
});

Contributions rules

  • Changes must be approved;
  • Changes must have tests passing on Travis-CI;
  • Changes must have coverage of 95% on Travis-CI for: statements, branches, functions and lines;
  • Last commit message must have attribute [release=major|minor|patch|no-release];