
Security News
Feross on TBPN: How North Korea Hijacked Axios
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.
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 |
Example of prop text
{
en:"Hello world!",
es:"Hola Mundo!",
cs:"Ahoj světe!"
}
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>
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 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" };
this._handleChange = this._handleChange.bind(this);
}
_handleChange(event) {
this.setState({ lang: 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>
<p>
<LanguageConsumer text={Texts.body} />
</p>
</LanguageProvider>
</div>
);
}
}
I see [object Object] instead of valid text -> please see section LanguageContext
FAQs
Simple library to support multilingualism in react applications using react context.
The npm package react-language-context receives a total of 1 weekly downloads. As such, react-language-context popularity was classified as not popular.
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
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.

Security News
OpenSSF has issued a high-severity advisory warning open source developers of an active Slack-based campaign using impersonation to deliver malware.

Research
/Security News
Malicious packages published to npm, PyPI, Go Modules, crates.io, and Packagist impersonate developer tooling to fetch staged malware, steal credentials and wallets, and enable remote access.