Git Product home page Git Product logo

react-multi-labels's Introduction

React multi labels

Build Status devDependencies Status Coverage Status FOSSA Status

Provide static labels to your application, whichever language you want

How to use it

React multi labels rely on the React 16.3.*, this means that if you use an earlier version, it won't work.

Setup

Fist of all you have to initialize the provider by giving it the default language of our labels and the labels themselves:

import { LabelsProvider } from 'react-multi-labels';
import { App } from './app';
import React from 'react';
import { render } from 'react-dom';

const labels = {
    en_GB: {
        REMOVE: 'Remove',
        CREATE: 'Create',
        CHANGE: 'Change',
        EDIT: 'Edit'
    },
    it_IT: {
        REMOVE: 'Rimuovi',
        CREATE: 'Crea',
        CHANGE: 'Cambia',
        EDIT: 'Modifica'
    }
}

// 'en_GB' will be the first language
render(
    <LabelsProvider language={'en_GB'} labels={labels}>
        <App />
    </LabelsProvider>
    document.getElementById('app')
)

Consuming

The library provides different high order components to consume your labels.

import { Label } from 'react-multi-labels';

function Button() {
  return (
    <button>
      <Label text={'REMOVE'} />
    </button>
  );
}

// result will be <button>Remove</button> for 'en_GB'
// result will be <button>Rimuovi</button> for 'it_IT'

API

Label

It simply returns the label that you want. It doesn't return any markup. Only text. If it doesn't exist, it will return undefined

GetLabel

High Order Component to return the label, so you can provide it all your components

import { GetLabels } from 'react-multi-labels';
import { Button } from './Button';
function Form() {
  return (
    <GetLabel text={'REMOVE'}>
      {label => {
        return <Button buttonLabel={label} />;
      }}
    </GetLabel>
  );
}

GetLabels

Sometimes you need to retrieve multiple labels in one go. You can use this guy. It's an high order component that requires an array of the labels that you want and it returns an object, so you can identify your labels with their own name.

import { GetLabels } from 'react-multi-labels';
import { Button } from './Button'
import React from 'react'

function Form () {
    return (
        <GetLabels list={['REMOVE', 'CREATE']}>
            {({ REMOVE, CREATE }) => {
                return (
                    <React.Fragment>
                        <Button buttonLabel={CREATE} />
                        <Button buttonLabel={REMOVE} />
                    </React.Fragment>
                )
            }}
        </GetLabel>
    )
}

ChangeLanguage

import { GetLabels, ChangeLanguage } from 'react-multi-labels';
import { Button } from './Button';
import React from 'react';

function Form() {
  return (
    <GetLabels list={['REMOVE', 'CHANGE']}>
      {({ REMOVE, CHANGE }) => {
        return (
          <React.Fragment>
            <Button buttonLabel={REMOVE} />
            <ChangeLanguage>
              {changeLanguage => {
                <Button
                  buttonLabel={CHANGE}
                  onClick={() => changeLanguage('it_IT')}
                />;
              }}
            </ChangeLanguage>
          </React.Fragment>
        );
      }}
    </GetLabels>
  );
}

ChangeLabels

import { GetLabels, ChangeLabels, LabelsProvider } from 'react-multi-labels';
import { Button } from './Button';
import React from 'react';

const LABELS = {
    en_GB: {
        LOGIN: 'Login'
        EMAIL: 'Email'
    }
};

const NEW_LABELS = {
    en_GB: {
        LOGIN: 'Fancy Login'
        EMAIL: 'Fancy Email'
    }
};


<LabelsProvider language="en_GB" labels={LABELS}>
  <React.Fragment>
    <GetLabels list={['LOGIN', 'EMAIL']}>
      {({ LOGIN, EMAIL }) => {
        return (
          <p>
            {LOGIN} - {EMAIL}
          </p>
        );
      }}
    </GetLabels>
    <ChangeLabels>
      {({ changeLabels }) => {
        return (
          <button onClick={() => changeLabels(newLabels)}>
            Click me!
          </button>
        );
      }}
    </ChangeLabels>
  </React.Fragment>
</LabelsProvider>

License

FOSSA Status

react-multi-labels's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

fossabot

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.