Gathering detailed insights and metrics for react-polyglot-hooks
Gathering detailed insights and metrics for react-polyglot-hooks
Gathering detailed insights and metrics for react-polyglot-hooks
Gathering detailed insights and metrics for react-polyglot-hooks
eslint-plugin-react-hooks
ESLint rules for React Hooks
react-polyglot
Higher order React component for using Polyglot
ra-i18n-polyglot
Polyglot i18n provider for react-admin
@testing-library/react-hooks
Simple and complete React hooks testing utilities that encourage good testing practices.
npm install react-polyglot-hooks
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
26 Stars
1,820 Commits
3 Forks
3 Watching
2 Branches
1 Contributors
Updated on 25 Nov 2024
TypeScript (95.54%)
JavaScript (4.46%)
Cumulative downloads
Total Downloads
Last day
75%
28
Compared to previous day
Last week
34.5%
113
Compared to previous week
Last month
9%
377
Compared to previous month
Last year
-16.9%
5,850
Compared to previous year
2
39
Hooks for using Polyglot.js with React.
React Polyglot Hooks is available as an npm package.
1// with npm 2npm install react-polyglot-hooks 3 4// with yarn 5yarn add react-polyglot-hooks
React is required as a peer dependency. Please install version 16.8.3 or later (minimum requirement for hooks).
React Polyglot Hooks offers 1 wrapper component: <I18n>
, 2 hooks: useLocale
and useT
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
1import React from 'react'; 2import { I18n } from 'react-polyglot-hooks'; 3import Child from './Child'; 4 5// ... or any ways to determine current locale 6const locale = window.locale || 'en'; 7 8// You can put translations in separate files 9const phrases = { 10 en: { hello: 'Hello, World!' }, 11 fr: { hello: 'Bonjour, Monde!' }, 12}; 13 14export default function Parent() { 15 return ( 16 <I18n locale={locale} phrases={phrases[locale]}> 17 <Child /> 18 </I18n> 19 ); 20}
Then, in a child component, call the hooks:
Child.jsx
1import React from 'react'; 2import { T, useLocale } from 'react-polyglot-hooks'; 3 4export default function Child() { 5 const locale = useLocale(); // Current locale: "en" 6 return ( 7 <React.Fragment> 8 <span>{locale}</span> 9 <T phrase="hello" /> 10 </React.Fragment> 11 ); 12}
That's it! For more in-depth examples, check out the examples directory.
Types are baked in as the project is written in TypeScript.
<I18n>
Provides i18n context to the T component and 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 phrase keys in a t call is allowed. |
interpolation | { prefix: string, suffix: string } | ❌ | Controls the prefix and suffix for an interpolation. |
onMissingKey | (key: string, options: InterpolationOptions, locale: string) => string | ❌ | A function called when allowMissing is true and a missing key is encountered. |
pluralRules | { pluralTypes: PluralTypes, pluralTypeToLanguages: PluralTypeToLanguages } | ❌ | Custom pluralization rules to be applied to change language(s) behaviour(s). |
<T>
Renders a phrase according to the props.
Props | Type | Required | Description |
---|---|---|---|
phrase | string | ✅ | Key of the needed phrase. |
count | number | ❌ | A number to be interpolated with smart_count . |
fallback | string | ❌ | A fallback to be rendered if the phrase is missing. |
interpolations | InterpolationOptions | ❌ | See InterpolationOptions below. |
useLocale
Returns the current active locale (string
).
useT
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]: ReactNode } | ❌ | 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 developed to ease the use of Polyglot.js within React, and is highly influenced by react-polyglot
.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
30 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
license file detected
Details
Reason
1 existing vulnerabilities detected
Details
Reason
Found 0/1 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More