
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-language-context
Advanced tools
Simple library to support multilingualism in react applications using react context.
Simple library to support multilingualism in react applications using react context.
npm install --save react-language-context
Library is using react context for keeping language information in application.
This library contains three components. LanguageProvider, LanguageConsumer and LanguageContext. Mostly you need to use LanguageProvider and LanguageConsumer for multilang support.
Component LanguageProvider should be placed in "app or top" component - component where you will keep language settings. State of the top component should be expanded with following - lang and defaultLang. This state property could be simply changed with setState function. It is up to you how you implement the language change.
| PROPS | TYPE | INFO |
|---|---|---|
| lang | string | current language of application - "es" |
| defaultLang | string | default language of application - "en" |
| useDefaultLangInstead | bool | true default language is used when translation is missing for current language, false nothing is shown if translation is missing for current language |
Example of usage
export default class App extends Component {
constructor(props) {
super(props);
this.state = { lang: "es", defaultLang: "en" };
this._changeLang = this._changeLang.bind(this);
}
_changeLang(lang) {
this.setState({ lang });
}
render() {
return (
<div>
<LanguageProvider
lang={this.state.lang}
defaultLang={this.state.defaultLang}
useDefaultLangInstead ={false}
>
//example components
<Header/>
<Body/>
<Footer/>
</LanguageProvider>
</div>);
}
}
Component LanguageConsumer is used as the shown text.
| PROPS | TYPE | INFO |
|---|---|---|
| text | object | simple object with keys - values, where key is language and value is text itself |
| replacer | object | simple object with keys - values, where key is regex which you want to replace with value |
Example of prop text
{
en:"Hello world!",
es:"Hola Mundo!",
cs:"Ahoj světe!"
}
Example of prop replacer
{
en:"Hello $1",
es:"Hola $1",
cs:"Ahoj $1"
}
Example of usage
const Texts = {
header:{
en:"Header in english",
es:"Encabezado en español",
cs:"Hlavička v češtině"
},
body:{
en:"Body in english",
es:"Contenido en español",
cs:"Obsah v češtině"
}
}
<h1>
<LanguageConsumer text={Texts.header} />
</h1>
<p>
<LanguageConsumer text={Texts.body} />
</p>
<h2>
<LanguageConsumer text={Texts.dynamicHeader} replacer={{$1: this.state.name}}/>
</h2>
LanguageContext is used in both components LanguageProvider and LanguageConsumer. Most of the time you don't need to use it directly. But sometimes you can have some specific situation. So you can use it as react context and from it you can get 'Consumer'. With this you are able to do pretty much everything. This should be used for components which require string and not component inside them. For example - <option> tag. In context you will find properties from Provider - lang, defaultLang, useDefaultLangInstead.
Example of usage
<select>
<LanguageContext.Consumer>
{({ lang, defaultLang, useDefaultLangInstead }) =>
Object.keys(Texts.select).map(key => (
<option key={key} value={key}>{Texts.select[key]}</option>
))}
</LanguageContext.Consumer>
</select>
import React, { Component } from "react";
import { LanguageProvider, LanguageConsumer,LanguageContext } from "react-language-context";
import { Texts } from "./texts";
export default class App extends Component {
constructor(props) {
super(props);
this.state = { lang: "en", defaultLang: "en", name: "Jar Jar" };
this._handleChange = this._handleChange.bind(this);
this._handleChangeName = this._handleChangeName.bind(this);
}
_handleChange(event) {
this.setState({ lang: event.target.value });
}
_handleChangeName(event) {
this.setState({ name: event.target.value });
}
render() {
return (
<div>
<LanguageProvider
lang={this.state.lang}
defaultLang={this.state.defaultLang}
>
<select value={this.state.lang} onChange={this._handleChange}>
<LanguageContext.Consumer>
{({ lang }) =>
Object.keys(Texts.select[lang]).map(key => (
<option key={lang+key} value={key}>{Texts.select[lang][key]}</option>
))}
</LanguageContext.Consumer>
</select>
<h1>
<LanguageConsumer text={Texts.header} />
</h1>
<label>
Name: <input type="text" value={this.state.name} onChange={this._handleChangeName} />
</label>
<h2>
<LanguageConsumer text={Texts.dynamicHeader} replacer={{$1: this.state.name}}/>
</h2>
<p>
<LanguageConsumer text={Texts.body} />
</p>
</LanguageProvider>
</div>
);
}
}
01.01.2020 - 1.0.0 - first release
04.01.2020 - 1.1.0 - adds props replacer for component LanguageConsumer. This prop is used for dynamic translations. See LanguageConsumer for more info.
FAQs
Simple library to support multilingualism in react applications using react context.
We found that react-language-context demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.