Gathering detailed insights and metrics for react-gettext-parser
Gathering detailed insights and metrics for react-gettext-parser
Gathering detailed insights and metrics for react-gettext-parser
Gathering detailed insights and metrics for react-gettext-parser
babel-plugin-react-gettext-parser
Babel plugin for react-gettext-parser
gettext-parser
Parse and compile gettext po and mo files to/from json, nothing more, nothing less
node-gettext
A JavaScript implementation of gettext, a localization framework
jed
Gettext Style i18n for Modern JavaScript Apps
npm install react-gettext-parser
55.3
Supply Chain
95.4
Quality
73.5
Maintenance
50
Vulnerability
96.4
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
43 Stars
220 Commits
27 Forks
8 Watching
4 Branches
21 Contributors
Updated on 10 Jul 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
16.3%
950
Compared to previous day
Last week
-8.3%
4,116
Compared to previous week
Last month
9.3%
18,524
Compared to previous month
Last year
13.5%
189,531
Compared to previous year
13
react-gettext-parser
is a tool that searches your code for strings that are meant to be translated, extracts them and puts them into a well-formatted Gettext .pot file. Simply configure what your translation functions and/or React components are named and what parameters they accept, and then use the CLI or JavaScript API to collect your app or website's translatable contents in seconds.
gettext('Foo ' + 'Bar')
(useful for wrapping into multiple lines)Providing a config, using a single glob string:
1react-gettext-parser --config path/to/config.js --output messages.pot 'src/**/{*.js,*.jsx,*.ts,*.tsx}'
Using an array of glob strings, which is passed to glob-all
:
1react-gettext-parser --output messages.pot 'src/*.js' '!src/test.js'
The entire help section for ya:
1react-gettext-parser <options> glob [, glob, ...] 2 3Options: 4 -h, --help Show help [boolean] 5 -o, --output Path to output .pot file 6 -c, --config Path to a react-gettext-parser config file 7 --trim Trims extracted strings from surrounding whitespace[boolean] 8 --trim-lines Trims each line in extracted strings from surrounding 9 whitespace [boolean] 10 --trim-newlines Trims extracted strings from new-lines [boolean] 11 --disable-line-numbers Disables line number ouput in .pot file [boolean] 12 --no-wrap Does not break long strings into several lines [boolean] 13 --header Sets a POT header value with the syntax "Some-Header: 14 some value". You can specify more than one header. Add 15 a -- after your --header argument(s). [array]
1// Script somewhere 2 3import { parseGlob } from 'react-gettext-parser'; 4 5// Parse a file and put it into a pot file 6parseGlob(['src/**/{*.js,*.jsx}'], { output: 'messages.pot' }, () => { 7 // Done! 8}); 9 10// You can also get extracted strings as a list of message objects 11import { extractMessagesFromGlob } from 'react-gettext-parser'; 12const messages = extractMessagesFromGlob(['src/**/{*.js,*.jsx}']); 13 14/* 15Results in something like: 16 17[ 18 { 19 msgctxt: "", 20 msgid: "Translate me" 21 msgstr: [""], 22 comments: { 23 extracted: ["A comment to translators"], 24 reference: [{ 25 filename:"MyComponent.jsx", 26 line:13, 27 column:1 28 }] 29 } 30 }, 31 // And so on... 32] 33*/
babel-plugin-react-gettext-parser
1babel --plugins react-gettext-parser src
1// .babelrc 2{ 3 "presets": ["es2015", "react"], 4 "plugins": [ 5 ["react-gettext-parser", { 6 // Options 7 }] 8 ] 9}
1{ 2 "scripts": { 3 "build:pot": "react-gettext-parser --config path/to/config.js --output messages.pot 'src/**/*.js*'" 4 } 5}
1var reactGettextParser = require('react-gettext-parser').gulp; 2 3gulp.task('build:pot', function() { 4 return gulp.src('src/**/*.js*') 5 .pipe(reactGettextParser({ 6 output: 'messages.pot', 7 // ...more options 8 })) 9 .pipe(gulp.dest('translations')); 10});
extractMessages(codeStr, [options])
Parses a string with JS(X) or Typescript source code for translatable strings and returns a list of message objects.
When use with typescript source code, specify option sourceType
as TYPESCRIPT
extractMessagesFromFile(filePath, [options])
Parses a JS(X) or Typescript file for translatable strings and returns a list of message objects.
extractMessagesFromGlob(globStr, [options])
Parses JS(X) or Typescript files matching a glob for translatable strings and returns a list of message objects.
parse(code, [options], [callback])
Parses a string with JS(X) source code for translatable strings and writes a .pot file containing those strings.
When use with typescript source code, specify option sourceType
as TYPESCRIPT
parseFile(filePath, [options], [callback])
Parses a JS(X) file for translatable strings and writes a .pot file containing those strings.
parseGlob(globStr, [options], [callback])
Parses JS(X) files matching a glob for translatable strings and writes a .pot file containing those strings.
toPot(messages, [opts])
Turns an array of messages into a POT string.
opts.transformHeaders
- A function that takes an object containing default POT headers and returns an object containing transformed POT headers. The default is to return the default headers as is.Converts an array of message objects into a POT string.
outputPot(filePath, contents, [callback])
Writes contents
to filePath
if filePath
is truthy, i.e. a string. If filePath
is falsy, contents
is logged to the console.
output
The destination path for the .pot file. If omitted, the .pot output will be logged to the console.
componentPropsMap
A two-level object of prop-to-gettext mappings.
The defaults are:
1{ 2 GetText: { 3 message: 'msgid', 4 messagePlural: 'msgid_plural', 5 context: 'msgctxt', 6 comment: 'comment', 7 } 8}
The above would make this component...
1// MyComponent.jsx 2<GetText 3 message="One item" 4 messagePlural="{{ count }} items" 5 count={numItems} 6 context="Cart" 7 comment="The number of items added to the cart" 8/>
...would result in the following translation block:
1# The number of items added to the cart 2#: MyComponent.jsx:2 3msgctxt "Cart" 4msgid "One item" 5msgid_plural "{{ count }} items" 6msgstr[0] "" 7msgstr[1] ""
funcArgumentsMap
An object of function names and corresponding arrays of strings that matches arguments against gettext variables.
Defaults:
1{ 2 gettext: ['msgid'], 3 dgettext: [null, 'msgid'], 4 ngettext: ['msgid', 'msgid_plural'], 5 dngettext: [null, 'msgid', 'msgid_plural'], 6 pgettext: ['msgctxt', 'msgid'], 7 dpgettext: [null, 'msgctxt', 'msgid'], 8 npgettext: ['msgctxt', 'msgid', 'msgid_plural'], 9 dnpgettext: [null, 'msgid', 'msgid_plural'], 10}
This configs means that this...
1// Menu.jsx 2<Link to="/inboxes"> 3 { npgettext('Menu', 'Inbox', 'Inboxes') } 4</Link>
...would result in the following translation block:
1#: Menu.jsx:13 2msgctxt "Menu" 3msgid "Inbox" 4msgid_plural "Inboxes" 5msgstr[0] "" 6msgstr[1] ""
trim
(--trim
)Trims extracted strings from surrounding whitespace.
Default: false
trimLines
(--trim-lines
)Trims each line in extracted strings from surrounding whitespace.
Default: false
trimNewlines
(--trim-newlines
)Trims extracted strings from new-lines.
Default: false
disableLineNumbers
(--disable-line-numbers
)Disables line number ouput in .pot file
Default: false
noWrap
(--no-wrap
)Does not break long strings into several lines
Default: false
The react-gettext-parser
CLI accepts a --config <file path>
argument. This should point to a JavaScript or JSON file that exports an object with any or all of the available options as root properties. Here's an example:
1// react-gettext-parser.config.js 2module.exports = { 3 componentPropsMap: { 4 Translate: { 5 one: 'msgid', 6 many: 'msgid_plural', 7 context: 'msgctxt', 8 comment: 'comment', 9 } 10 }, 11 funcArgumentsMap: { 12 translate: ['msgid', 'msgid_plural', 'msgctxt'], 13 }, 14 trim: true, 15}
Get react-gettext-parser
up and running:
1npm i && npm run build && npm link
Running the Mocha test suite:
1npm test
Dev mode, running build
in watch mode:
1npm run dev
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
4 existing vulnerabilities detected
Details
Reason
Found 6/21 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
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