
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
react-gettext
Advanced tools
Tiny React library for implementing gettext localization in your application. It provides HOC function to enhance your application by exposing gettext functions in the context scope.
React Gettext requires React 15.0 or later. You can add this package using following commands:
npm install react-gettext --save
yarn add react-gettext
Let's assume you have following React application:
// app.js
import React, { Component } from 'react';
import Header from './Header';
import Footer from './Footer';
export default class App extends Component {
render() {
return (
<div id="app">
<Header />
...
<Footer />
</div>
);
}
}
// Header.js
import React, { Component } from 'react';
export default class Header extends Component {
render() {
return (
<h1>Welcome to my application!</h1>
);
}
}
To make it translatable you need to update your app.js
file to use HOC function and export higher-order component:
// app.js
import React, { Component } from 'react';
+ import withGettext from 'react-gettext';
import Header from './Header';
import Footer from './Footer';
- export default class App extends Component {
+ class App extends Component {
...
}
+ export default withGettext({...}, 'n != 1')(App);
After doing it you can start using gettext
, ngettext
, xgettext
and nxgettext
functions in your descending components:
// Header.js
- import React, { Component } from 'react';
+ import React, { Component } from 'react';
+ import PropTypes from 'prop-types';
export default class Header extends Component {
render() {
return (
- <h1>Welcome to my application!</h1>
+ <h1>{this.context.gettext('Welcome to my application!')}</h1>
);
}
}
+ Header.contextTypes = {
+ gettext: PropTypes.func.isRequired,
+ ngettext: PropTypes.func.isRequired,
+ xgettext: PropTypes.func.isRequired,
+ nxgettext: PropTypes.func.isRequired,
+ };
See an example application to get better understanding how to use it.
Higher-order function which is exported by default from react-gettext
package. It accepts two arguments and returns function to create higher-order component.
Example:
const translations = {
'Some text': 'Some translated text',
...
};
const pluralForms = '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'; // 3 plural forms for Russian, Belarusian, Bosnian, Croatian, Serbian, Ukrainian, etc.
const HOC = withGettext(translations, pluralForms)(App);
function getTranslations() {
return {
'Some text': 'Some translated text',
...
};
}
function getPluralForms(n) {
return n > 1 ? 1 : 0;
}
const HOC = withGettext(getTranslations, getPluralForms)(App);
As an alternative you can pass translations and plural form as properties to higher-order-component, like this:
function getTranslations() {
return {
'Some text': 'Some translated text',
...
};
}
function getPluralForms(n) {
return n > 1 ? 1 : 0;
}
const HOC = withGettext()(App);
...
ReactDOM.render(<HOC translations={getTranslations} plural={getPluralForms}>...</HOC>, ...);
One more alternative is to not create HOC, but use Textdomain component directly. You can import it using import { Textdomain } from 'react-gettext'
and use it as a regular component which will provide context functions to translate your messages. Just don't forget to pass translations
and plural
props to this component when you render it.
The function to translate a string. Accepts original message and returns translation if it exists, otherwise original message.
Example:
// somewhere in your jsx component
this.context.gettext('Some text');
The function to translate plural string. Accepts singular and plural messages along with a number to calculate plural form against. Returns translated message based on plural form if it exists, otherwise original message based on n value.
Example:
// somewhere in your jsx component
this.context.ngettext('day ago', 'days ago', numberOfDays);
The function to translate a string based on a specific context. Accepts a message to translate and a translation context string. Returns translated message if it exists, otherwise original string.
Example:
// somewhere in your jsx component
this.context.xgettext('some text', 'context where this message is used');
The function to translate plural string based on a specific context. Accepts singular and plural messages along with a number to calculate plural form against and context string. Returns translated message based on plural form if it exists, otherwise original message based on n value.
Example:
// somewhere in your jsx component
this.context.nxgettext('day ago', 'days ago', numberOfDays, 'Article publish date');
If you use Poedit app to translate your messages, then you can use gettext;ngettext:1,2;xgettext:1,2c;nxgettext:1,2,4c
as keywords list to properly parse and extract strings from your javascript files.
Here is an example of a POT file which you can start with:
msgid ""
msgstr ""
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Project-Id-Version: \n"
"POT-Creation-Date: \n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=iso-8859-1\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Poedit-Basepath: ./src\n"
"X-Poedit-KeywordsList: gettext;ngettext:1,2;xgettext:1,2c;nxgettext:1,2,4c\n"
"X-Poedit-SourceCharset: UTF-8\n"
What to help or have a suggestion? Open a new ticket and we can discuss it or submit pull request. Please, make sure you run npm test
before submitting a pull request.
MIT
v0.3.3 (2017-08-12)
Implemented enhancements:
FAQs
Gettext implementation for React based project.
The npm package react-gettext receives a total of 375 weekly downloads. As such, react-gettext popularity was classified as not popular.
We found that react-gettext 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
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.