Giter Club home page Giter Club logo

react-intl-context-provider's Introduction

react-intl-context-provider styled with prettier

This library exposes a wrapper around react-intl IntlProvider component using a new React Context API. This allows to switch language deep in a component tree in a simple way - without a need to use redux or other external library.

Usage

Just replace the original react-intl IntlProvider component with the wrapper and pass the initial props:

import { IntlProvider, loadLocaleData } from "react-intl-context-provider";

loadLocaleData(['en', 'de']); // use this instead of addLocaleData

ReactDOM.render(
  <IntlProvider initialProps={{ locale: "en", defaultLocale: "en" }}>
    <App />
  </IntlProvider>,
  document.getElementById("container")
);

... and then change the props anywhere in the app using IntlConsumer:

import { IntlConsumer } from "react-intl-context-provider";
import enTranslation from "../../static/en.json";
import deTranslation from "../../static/de.json";

const LanguageSwitcher = () => (
  <IntlConsumer>
    {({ updateProps, ...providerProps }) => (
      <>
        <button
          onClick={() => updateProps({ locale: "en", messages: enTranslation })}
        >
          switch to english
        </button>
        <button
          onClick={() => updateProps({ locale: "de", messages: deTranslation })}
        >
          > switch to deutsch
        </button>
      </>
    )}
  </IntlConsumer>
);

The updateProps will shallowly merge new props to an old ones.

Note: changing locale will rerender everything under IntlProvider. This is desired as it will re-render the page with a newly set language.

loadLocaleData

When you try to add locales in the standard way:

import { IntlProvider } from "react-intl-context-provider";
import { addLocaleData } from "react-intl";
import en from "react-intl/locale-data/en";
import de from "react-intl/locale-data/de";

addLocaleData([...en, ...de]);

ReactDOM.render(
  <IntlProvider initialProps={{ locale: "en", defaultLocale: "en" }}>
    <App />
  </IntlProvider>,
  document.getElementById("container")
);

You will most likely encounter an error when trying to switch language:

Missing locale data for locale: "de". Using default locale: "en" as fallback.

To fix this library exposes loadLocaleData function which accepts an Array of language-codes and is equivalent to the way above.

react-intl-context-provider's People

Contributors

tomekmularczyk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.