Gathering detailed insights and metrics for react-polyglot
Gathering detailed insights and metrics for react-polyglot
Gathering detailed insights and metrics for react-polyglot
Gathering detailed insights and metrics for react-polyglot
npm install react-polyglot
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
56 Stars
96 Commits
15 Forks
3 Watching
16 Branches
8 Contributors
Updated on 16 Feb 2024
JavaScript (97.01%)
HTML (2.99%)
Cumulative downloads
Total Downloads
Last day
88.2%
7,051
Compared to previous day
Last week
34.8%
28,531
Compared to previous week
Last month
48.1%
95,668
Compared to previous month
Last year
-13.6%
876,035
Compared to previous year
2
2
Provides higher order component for using Polyglot with React
npm install --save react-polyglot
react-polyglot
exports consists for one wrapper component called I18n
, one decorator called
translate
and one hook called useTranslate
. The decorator provides a prop t
which is instance of Polyglot
.
You are required to wrap your root component with I18n
and pass on a locale
like en
or fr
.
And messages
object containing the strings.
1import React from 'react'; 2import { render } from 'react-dom'; 3import { I18n } from 'react-polyglot'; 4import App from './components/app'; 5 6const locale = window.locale || 'en'; 7const messages = { 8 "hello_name": "Hello, %{name}.", 9 "num_cars": "%{smart_count} car |||| %{smart_count} cars", 10} 11 12render( 13 <I18n locale={locale} messages={messages}> 14 <App /> 15 </I18n>, 16 document.getElementById('app') 17);
Then inside App
or a child component of App
you can do:
1import React from 'react'; 2import { translate } from 'react-polyglot'; 3 4const Greeter = ({ name, t }) => ( 5 <h3>{t('hello_name', { name })}</h3> 6); 7 8Greeter.propTypes = { 9 name: React.PropTypes.string.isRequired, 10 t: React.PropTypes.func.isRequired, 11}; 12 13export default translate()(Greeter);
or with React Hooks:
1import React from 'react'; 2import { useTranslate } from 'react-polyglot'; 3 4export default const Greeter = ({ name }) => { 5 const t = useTranslate(); 6 7 return ( 8 <h3>{t('hello_name', { name })}</h3> 9 ); 10}; 11 12Greeter.propTypes = { 13 name: React.PropTypes.string.isRequired 14}; 15
https://codesandbox.io/s/mq76ojk228
https://codesandbox.io/s/px8n63v0m
Use a simple helper to wrap your components in a context.
1export const wrapWithContext = function (component, context, contextTypes) { 2 const wrappedComponent = React.createClass({ 3 childContextTypes: contextTypes, 4 getChildContext() { 5 return context; 6 }, 7 render() { 8 return component; 9 }, 10 }); 11 return React.createElement(wrappedComponent); 12}
Then use it inside your tests.
1import React from 'react'; 2import { renderToString } from 'react-dom/server'; 3import Polyglot from 'node-polyglot'; 4import Greeter from './greeter'; 5import { wrapWithContext } from './helpers'; 6 7const polyglot = new Polyglot({ 8 locale: 'en', 9 phrases: {"hello_name": "Hello, %{name}."}, 10}); 11 12const greeterWithContext = wrapWithContext( 13 <Greeter name="Batsy" />, 14 { t: polyglot.t.bind(polyglot) }, 15 { t: React.PropTypes.func } 16); 17 18// use greeterWithContext in your tests 19// here it is shown how to use it with renderToString 20console.log(renderToString(greeterWithContext));
Check the Releases tab.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/24 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
47 existing vulnerabilities detected
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