xtractor 🚜
Simple string extraction
xtractor statically analyzes js files or globs of files and extracts strings within designated function call
Usage
Import the library
var xtractor = require('xtractor');
or
import * as xtractor from 'xtractor';
Extract strings from a file
xtractor.extractFromFile(__dirname + '/source.js', ['_', 'i18n._'], function(err, strings) {
});
A promise-based API is also supported
xtractor.extractFromFile(__dirname + '/source.js', ['_', 'i18n._'])
.then(function(strings) {
})
.catch(function(err) {
});
Extract strings from a glob
xtractor.extractGlob(__dirname + '/*.js', ['_', 'i18n._'], function(err, strings) {
});
Promised-based API
xtractor.extractGlob(__dirname + '/*.js', ['_', 'i18n._'])
.then(function(err, strings) {
})
.catch(function(err) {
});
Specify an object with the glob as the glob
property and a regexp ignore pattern as the ignorePattern
property
xtractor.extractGlob({
glob: __dirname + '/**/*.js',
ignorePattern: /node_modules/
}, ['_', 'i18n._'], function(err, strings) {
});
Expected and output format
The expected format for extraction is a call to the marker function with either one or three arguments, depending on pluralization.
Suppose the marker function is i18n._()
:
Singular
const str = i18n._('You have new messages');
will output
{
"msgid": "You have new messages",
"loc": [
{
"filepath": "sample.js",
"line": 1
}
]
}
Plural
const str = i18n._('You have {{ count }} new message', 'You have {{ count }} new messages', count);
will output
{
"msgid": [
"You have {{ count }} new message",
"You have {{ count }} new messages"
],
"loc": [
{
"filepath": "sample.js",
"line": 1
}
]
}