Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
react-polyglot-hooks
Advanced tools
Hooks for using Polyglot.js with React.
React Polyglot Hooks is available as an npm package.
// with npm
npm install react-polyglot-hooks
// with yarn
yarn add react-polyglot-hooks
React is required as a peer dependency. Please install version 16.8.0 or later (minimum requirement for hooks).
React Polyglot Hooks offers 1 wrapper component: <I18n>
, 2 hooks: useLocale
and useTranslate
and 1 helper component: <T>
The hooks and the helper component have to be wrapped with the <I18n>
component to work properly.
Here is a quick example to get you started:
First, wrap a parent component with <I18n>
and provide locale
and phrases
.
Parent.jsx
import React from 'react';
import { I18n } from 'react-polyglot-hooks';
import Child from './Child';
// ... or any ways to determine current locale
const locale = window.locale || 'en';
// You can put translations in separate files
const phrases = {
en: { hello: 'Hello, World!' },
fr: { hello: 'Bonjour, Monde!' },
};
export default function Parent() {
return (
<I18n locale={locale} phrases={phrases[locale]}>
<Child />
</I18n>
);
}
Then, in a child component, call the hooks:
Child.jsx
import React from 'react';
import { T, useLocale } from 'react-polyglot-hooks';
export default function Child() {
const locale = useLocale(); // Current locale: "en"
return (
<React.Fragment>
<span>{locale}</span>
<T phrase="hello" />
</React.Fragment>
);
}
That's it!
Types are baked in as the project is written in TypeScript.
<I18n>
Provides i18n context to the hooks. Accepts all options supported by Polyglot.js.
Prop | Type | Required | Description |
---|---|---|---|
children | Node | ✅ | Any node(s) accepted by React. |
locale | string | ✅ | Current locale, used for pluralization. |
phrases | { [key: string]: string } | ✅ | Key-value pair of translated phrases, can be nested. |
allowMissing | boolean | ❌ | Controls whether missing keys in a t call is allowed. |
onMissingKey | (key: string, options: InterpolationOptions, locale: string) => string | ❌ | A function called when allowMissing is true and a missing key is encountered. |
interpolation | { prefix: string, suffix: string } | ❌ | Controls the prefix and suffix for an interpolation. |
<T>
Renders a phrase according to the props.
Props | Type | Required | Description |
---|---|---|---|
phrase | string | ✅ | Key of the needed phrase. |
options | number or { [key: string]: string } | ❌ | See InterpolationOptions below. |
useLocale
Returns the current active locale (string
).
useTranslate
Returns a special function (t
) which takes in parameters and returns a phrase.
t(phrase, InterpolationOptions)
Param | Type | Required | Description |
---|---|---|---|
phrase | string | ✅ | Key of the needed phrase. |
InterpolationOptions | number or { [key: string]: string } | ❌ | A number to be interpolated with smart_count , or a key-value pair to interpolate values into the phrase. |
For more details, please visit the documentation of Polyglot.js.
The changelog is available here.
This project is licensed under the terms of the MIT License.
This project is a wrapper to ease the use of Polyglot.js within React, and is highly influenced by react-polyglot
.
FAQs
Hooks for using Polyglot.js with React.
The npm package react-polyglot-hooks receives a total of 68 weekly downloads. As such, react-polyglot-hooks popularity was classified as not popular.
We found that react-polyglot-hooks 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.