1.1.3 • Published 4 years ago

react-simple-localization v1.1.3

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
4 years ago

react-simple-localization

A simple library consisting of a Translate component using React's context API to update content dynamically.

Installation

npm i --save react-simple-localization

Usage

At the root of your app, set up your localization context provider:

import { useState } from 'react';
import { Languages, Locales, LocalizationContext } from 'react-simple-localization';

const App = () => {
    const [currentLanguage, setCurrentLanguage] = useState<Languages>(Languages.en);
    const [currentLocale, setCurrentLocale] = useState<Locales>(Locales.enUS);

    return (
        <LocalizationContext.Provider value={{
            language: currentLanguage,
            locale: currentLocale,
            update: (language: Languages, locale?: Locales) => {
                setCurrentLanguage(language);
                setCurrentLocale(locale);
            }
        }}>
            ...
        </LocalizationContext.Provider>
    )
}

You can then use the Translate component which will automatically update it's text to the proper language/locale whenever the context is updated.

import React, { useContext } from 'react';
import { LocalizationContext, Translate } from 'react-simple-localization';

const MyComponent = () => {
    return (
        <div>
            <Translate en="Hello" es="Hola" de="Guten Tag" fr="Bonjour" it="Salve" ru="Zdravstvuyte" />
        </div>
    )
}
1.1.4

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.2

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago