Skip to content

Add positional arguments to the list of files to process #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A command line interface for [angular-gettext-tools](https://github.com/rubenv/a

## Extraction:

`angular-gettext-cli --files './app/*.+(js|html)' --exclude '**/*.spec.js' --dest './extract.pot' --marker-name i18n`
`angular-gettext-cli --files './app/*.+(js|html)' --exclude '**/*.spec.js' --dest './extract.pot' --marker-name i18n file3.html file4.html`

### Parameters:
* `--files` - a [glob](https://github.com/isaacs/node-glob) pattern to specify files to process
Expand All @@ -19,15 +19,17 @@ A command line interface for [angular-gettext-tools](https://github.com/rubenv/a
* `--marker-name` - (optional) a name of marker to search in js-files (see [angular-gettext-tools](https://github.com/rubenv/angular-gettext-tools#options))
* `--marker-names` - (optional) comma separated string of marker names to search for in the js-files (see [angular-gettext-tools](https://github.com/rubenv/angular-gettext-tools#options))
* `--attributes` - (optional) comma separated string of marker names to search for in the html-files (see [angular-gettext-tools](https://github.com/rubenv/angular-gettext-tools#options))
* Any remaining positional arguments are added to the list of files to process, in addition to the glob files. If the filename starts with @ its contents are read and added to the list of files to process.

## Compilation:

`angular-gettext-cli --compile --files 'test/*.po' --dest 'test/output.js' --format javascript --module gettext-test`
`angular-gettext-cli --compile --files 'test/*.po' --dest 'test/output.js' --format javascript --module gettext-test test3.po test4.po @list.po.in`

### Parameters:
* `--files` - a [glob](https://github.com/isaacs/node-glob) pattern to specify files to process
* `--dest` - path to file to write extracted words.
* `--format` - `javascript` or `json`. Default is `javascript`
* `--module` - For the `javascript` output format, the angular module name to use in the output file. Default is `gettext`
* Any remaining positional arguments are added to the list of files to process, in addition to the glob files.

For more options, see [angular-gettext-tools](https://github.com/rubenv/angular-gettext-tools#options).
82 changes: 44 additions & 38 deletions bin/gettext
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

var glob = require("glob");
var fs = require('fs');
var argv = require('minimist')(process.argv);
var argv = require('minimist')(process.argv.slice(2));
var Extractor = require('angular-gettext-tools').Extractor;
var Compiler = require('angular-gettext-tools').Compiler;

Expand Down Expand Up @@ -38,55 +38,61 @@ function objToUpperCase(obj) {
return obj;
}

function expandFileNames(options) {
var globFileNames;
if (options.files)
globFileNames = glob.sync(options.files, options.exclude ? { ignore: options.exclude } : null);
else
globFileNames = [];
var positionalFileNames = options._;
// Still need to expand @filename -> read content of filename
var expandedAt = [];
function add(filename) {
if (filename) // ignore empty strings
expandedAt.push(filename);
}
for (let i = 0; i < positionalFileNames.length; i++) {
let fileName = positionalFileNames[i];
if (fileName[0] !== '@') {
// filename does not start with @
add(fileName);
} else {
// filename starts with @, read content
let content = fs.readFileSync(fileName.substr(1), options.encoding || 'utf-8').split(/\s+/);
for (let j = 0; j < content.length; j++)
add(content[j]);
}
}
return globFileNames.concat(expandedAt);
}

function compile(options) {
var gettextCompiler = new Compiler(objToUpperCase(options));

var fileNames = expandFileNames(options);
if (options.format && !Compiler.hasFormat(options.format)) {
throw new Error('There is no "' + options.format + '" output format.');
}

var output;
glob(options.files, options.exclude ? { ignore: options.exclude } : null, function (err, fileNames) {
if (err) {
throw err;
}

output = gettextCompiler.convertPo(fileNames.map(function(fileName) {
return fs.readFileSync(fileName, options.encoding || 'utf-8');
}));
}).on('end', function finish(fileNames) {
console.log('Finished compiling ' + options.files +', read '+fileNames.length+' files.');

fs.writeFile(options.dest, output, null, function(err) {
if (err) {
throw err;
}
var output = gettextCompiler.convertPo(fileNames.map(function(fileName) {
return fs.readFileSync(fileName, options.encoding || 'utf-8');
}));

console.log('All files processed, stored in a file ' + options.dest);
});
});
console.log('Finished compiling, read '+fileNames.length+' files.');
fs.writeFileSync(options.dest, output, null);
console.log('All files processed, stored in a file ' + options.dest);
}

function extract(options) {
var gettextExtractor = new Extractor(objToUpperCase(options));

glob(options.files, options.exclude ? { ignore: options.exclude } : null, function (err, fileNames) {
if (err) {
throw err;
}
fileNames.forEach(function (filename) {
var content = fs.readFileSync(filename, 'utf8');

gettextExtractor.parse(filename, content);
});
}).on('end', function finish(fileNames) {
console.log('Finished parsing ' + options.files +', read '+fileNames.length+' files.');

fs.writeFile(options.dest, gettextExtractor.toString(), null, function (err) {
if (err){
throw err;
}
console.log('All files processed, stored in a file ' + options.dest);
});
var fileNames = expandFileNames(options);
fileNames.forEach(function (filename) {
var content = fs.readFileSync(filename, 'utf8');
gettextExtractor.parse(filename, content);
});

console.log('Finished parsing, read '+fileNames.length+' files.');
fs.writeFileSync(options.dest, gettextExtractor.toString(), null);
console.log('All files processed, stored in a file ' + options.dest);
}
4 changes: 4 additions & 0 deletions example/extract-reference.pot
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ msgstr ""
msgid "Hello {{name}}"
msgstr ""

#: example/positional.htm:1
msgid "Positional"
msgstr ""

#: example/example.js:3
msgid "hi"
msgstr ""
1 change: 1 addition & 0 deletions example/positional.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="/" translate>Positional</a>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"scripts": {
"test": "bash acceptance-test.sh",
"example-extract": "./bin/gettext --files 'example/*.+(html|js)' --dest example/dist/extract.pot --marker-names 'i18n, __' --attributes 'custom-i18n'",
"example-extract": "./bin/gettext --files 'example/*.+(html|js)' --dest example/dist/extract.pot --marker-names 'i18n, __' --attributes 'custom-i18n' example/positional.htm",
"example-compile": "./bin/gettext --compile --files 'example/*.po' --dest example/dist/compiled.js --module gettext-test"
},
"repository": {
Expand Down