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-multi-language

v0.4.2

Published

Helps you create multi-language pages

Downloads

46

Readme

In the Name of Allah

react-multi-language

Helps you create multi-language pages.

  • No server needed.
  • Async support.
  • No dependencies.
  • Universal.
  • Micro library.

Installation

Via npm:

npm install react-multi-language --save

Via yarn:

yarn add react-multi-language

API Reference

Determinator

returns a string or component.

Props:

  • children: union(function, string, object):
    • function: can be an async function, the function will receive two arguments:
      • First is the current language, the function will be called anytime language changes(by MultiLang).
      • Second is a function which updates the currently displayed content by Determinator to the content that it receives according to the new language.
    • string: it will return the string with no mutation.
    • object: it must follow below pattern and returns the value of the matched key (based on props.lang of MultiLang).
  {
    [language]: [content]
  }
  // e.g.
  {
    fr: "Bonjour le monde",
    en: "Hello World"
  }
  • till: union(component, string): Determinator renders the value of props.till untill the async function that is passed as props.children to Determinator update the currently displayed content.

MultiLang

must be after all the Determinators.

Props: lang: string: the current language will be applied to the Determinators.

withLang

A higher-order component, it is very similar to Determinator

withLang(children)(Component)
  • children: union(function, object):
    • function: can be an async function, the function will receive two arguments:
      • First is the current language, the function will be called anytime language changes(by MultiLang).
      • Second is a function which updates the currently displayed content to the content that it receives according to the new language.
    • object: it must follow below pattern and returns the value of the matched key (based on props.lang of MultiLang).
  {
    [language]: [content]
  }
  // e.g.
  {
    fr: {
      Hello: "Bonjour",
      World: "monde", 
      imageURL: "https://example.com/200x200DB570BC6-F1CA-101F-C227-37B227673AD6.jpg?lang=fr"
    },
    en: {
      Hello: "Hello",
      World: "world",
      imageURL: "https://example.com/200x200DB570BC6-F1CA-101F-C227-37B227673AD6.jpg?lang=en"
    }
  }

The wrapped component receives three props:

  • lang: the current language.
  • langProps: the content of the current language.
  • determinator: is similar to Determinator but it is not component and does not support functions.
determinator({children, till});

using props.determinator rather than Determinator is recommended.

Examples

import React, { Component } from "react";
import { MultiLang, Determinator } from "react-multilang";

class App extends Component {
  state = {
    lang: "en",
  };

  changeLang = () => {
    this.setState(state => ({ lang: state.lang === "en" ? "fr" : "en" }));
  };

  render() {
    return (
      <div onClick={this.changeLang}>
        <Determinator>
          {{
            fr: "Bonjour le monde",
            en: "Hello World"
          }}
        </Determinator>
        <Determinator till={<span>Loading...</span>}>
          {async (lang, add) => {
            setTimeout(() => {
              if (lang == 'fr') 
                add("Bonjour le monde")
              if (lang == 'en') 
                add("Hello World");
              }
            , 1000);
          }}
        </Determinator>
        <Determinator>
          {"Hello World"}
        </Determinator>
        {/*MultiLang component must be after all the Determinators*/}
        <MultiLang lang={this.state.lang}/>
      </div>
    );
  }
}

export default App;

Fetching data when needed is one of the most common use cases for async functions:

import React, { Component } from "react";
import { MultiLang, Determinator } from "react-multilang";

class App extends Component {
  state = {
    lang: "en",
  };

  changeLang = () => {
    this.setState(state => ({ lang: state.lang === "en" ? "fr" : "en" }));
  };

  render() {
    return (
      <div onClick={this.changeLang}>
        <Determinator till={<span>Loading...</span>}>
          {async (lang, add) => {
            fetch(`http://example.com/${lang}.json`)
              .then(response => {
                return response.json();
              })
              .then(json => {
                add(JSON.stringify(json).content);
              });
          }}
        </Determinator>
        {/*MultiLang component must be after all the Determinators*/}
        <MultiLang lang={this.state.lang}/>
      </div>
    );
  }
}

export default App;

Using withLang:

import React, {Component} from "react";
import {MultiLang, withLang} from "react-multilang";

const MyComponent = withLang({
  fr: {
    HW: "Bonjour le monde",
    imageURL: "http://example.com/200x200DB570BC6-F1CA-101F-C227-37B227673AD6.jpg?lang=fr"
  },
  en: {
    HW: "Hello World",
    imageURL: "http://example.com/200x200DB570BC6-F1CA-101F-C227-37B227673AD6.jpg?lang=en"
  }
})(props => (
  <div>
    <p>{props.lang}</p>
    <p>{props.langProps.HW}</p>
    <img src={props.langProps.imageURL}/>
    <img src={"http://example.com/200x200DB570BC6-F1CA-101F-C227-37B227673AD6.jpg?lang=" + props.lang}/>
  </div>
));

class App extends Component {
  state = {
    lang: "en"
  };

  changeLang = () => {
    this.setState(state => ({
      lang: state.lang === "en"
        ? "fr"
        : "en"
    }));
  };

  render() {
    return (
      <div onClick={this.changeLang}>
        <MyComponent/>
        <MultiLang lang={this.state.lang}/>
      </div>
    );
  }
}

export default App;

License

MIT License