Skip to content

Commit

Permalink
Added verbose logging option
Browse files Browse the repository at this point in the history
  • Loading branch information
mallocator committed Apr 10, 2016
1 parent 1c1d1e4 commit 15d7e57
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
34 changes: 31 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ var defaults = {
dryRun: false,
includeOriginal: false,
ignoreTokens: false,
encodeEntities: true
encodeEntities: true,
verbose: false
};

/**
Expand Down Expand Up @@ -77,28 +78,41 @@ function trueOrMatch(needle, haystack) {
*/
function load(options) {
if (cache[options.locales]) {
options.verbose && gutil.log('Skip loading cached translations from', options.locales);
return dictionaries = cache[options.locales];
}
try {
options.verbose && gutil.log('Loading translations from', options.locales);
var files = fs.readdirSync(options.locales);
var count = 0;
for (var i in files) {
var file = files[i];
switch (path.extname(file)) {
case '.json':
case '.js':
dictionaries[path.basename(file, path.extname(file))] = flat(require(path.join(process.cwd(), options.locales, file)));
options.verbose && gutil.log('Added translations from', file);
count++;
break;
case '.ini':
var iniData = fs.readFileSync(path.join(process.cwd(), options.locales, file));
dictionaries[path.basename(file, path.extname(file))] = flat(ini2json(iniData));
options.verbose && gutil.log('Added translations from', file);
count++;
break;
case '.csv':
var csvData = fs.readFileSync(path.join(process.cwd(), options.locales, file));
dictionaries[path.basename(file, path.extname(file))] = csv2json(csvData);
options.verbose && gutil.log('Added translations from', file);
count++;
break;
default:
options.verbose && gutil.log('Ignored file', file);
}
}
options.verbose && gutil.log('Loaded', count, 'translations from', options.locales);
if (options.cache) {
options.verbose && gutil.log('Cashing translations from', options.locales);
cache[options.locales] = dictionaries;
}
} catch (e) {
Expand Down Expand Up @@ -248,7 +262,12 @@ function translate(options, contents, copied, filePath) {
if (!Object.keys(processed).length) {
throw new Error('No translation dictionaries available to create any files!');
}
if (!trueOrMatch(options.ignoreTokens, filePath) && !isBinary(contents)) {
options.verbose && gutil.log('Starting translation for', processed.length, 'languages');
if (trueOrMatch(options.ignoreTokens, filePath)) {
options.verbose && gutil.log('Ignoring file', filePath, 'because of ignoreTokens option');
} else if(isBinary(contents)) {
options.verbose && gutil.log('Ignoring file', filePath, 'because file is binary');
} else {
contents = contents.toString('utf8');
var i = contents.indexOf(options.delimiter.prefix);
while ((i !== -1)) {
Expand Down Expand Up @@ -276,7 +295,7 @@ function translate(options, contents, copied, filePath) {
} else {
processed[lang] += dictionaries[lang][key];
}
} else if (trueOrMatch(options.warn, filePath)) {
} else if (options.verbose || trueOrMatch(options.warn, filePath)) {
gutil.log('Missing translation of language', lang, 'for key', key, 'in file', filePath);
}
processed[lang] += contents.substring(i + length, next == -1 ? contents.length : next);
Expand All @@ -287,6 +306,7 @@ function translate(options, contents, copied, filePath) {
}
for (var lang in processed) {
if (!processed[lang].length) {
options.verbose && gutil.log('Copying original content to target language', lang, 'because no replacements have happened');
processed[lang] = contents;
}
}
Expand Down Expand Up @@ -337,9 +357,12 @@ function replace(file, options) {
* @returns {Stream}
*/
module.exports = function(options) {
options.verbose && gutil.log('gulp-international is starting');
options = _.assign({}, defaults, options);
load(options);

options.verbose && gutil.log('Parsed options:', JSON.toString(options));

module.exports.options = options;
module.exports.dictionaries = dictionaries;

Expand All @@ -350,18 +373,22 @@ module.exports = function(options) {
}

if (file.isStream()) {
options.verbose && gutil.log('gulp-international is skipping stream processing as it only supports buffered files.');
return cb(new gutil.PluginError('gulp-international', 'Streaming not supported'));
}

try {
var files = replace(file, options);
if (trueOrMatch(options.dryRun, file.path)) {
options.verbose && gutil.log('Ignoring all translations and passing on original file because "dryRun" was set');
this.push(file);
} else {
if (trueOrMatch(options.includeOriginal, file.path)) {
options.verbose && gutil.log('Passing on original file because "includeOriginal" was set');
this.push(file);
}
for (var i in files) {
options.verbose && gutil.log('Passing on translated file', files[i].path);
this.push(files[i]);
}
}
Expand All @@ -371,6 +398,7 @@ module.exports = function(options) {
}
}

options.verbose && gutil.log('gulp-international has finished');
cb();
});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-international",
"version": "1.0.11",
"version": "1.0.12",
"description": "A gulp plugin that creates multi language versions of your source files",
"license": "Apache-2.0",
"homepage": "http://github.com/mallocator/gulp-international",
Expand Down
26 changes: 23 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
[![Dependency Status](https://david-dm.org/mallocator/gulp-international.svg)](https://david-dm.org/mallocator/gulp-international)

This is a plugin for gulp that allows you to replace your custom placeholders for different language versions of your translations.

There are already a few plugins for i18n out there, but none of them seem to work properly. Rather than dig in their code I wanted
to use this opportunity to learn how to write plugins for gulp.
Call it i18n, localization, translation or whatever you want, in the end it's basically a token replacer with multiple source
files to create multiple versions of your original.

There are a few other plugins out there that I've tried but couldn't quite get to do what I wanted. Maybe they work better for you:
[gulp-i18n](https://www.npmjs.com/package/gulp-i18n)
[gulp-localize](https://www.npmjs.com/package/gulp-localize)
[gulp-static-i18n](https://www.npmjs.com/package/gulp-static-i18n)
[gulp-i18n-localize](https://www.npmjs.com/package/gulp-i18n-localize)
[gulp-l10n](https://www.npmjs.com/package/gulp-l10n)
Rather than dig in their code I wanted to use this opportunity to learn how to write plugins for gulp. So if none of those do the
trick you can try this little plugin.

Features (cause we all love features):

Expand Down Expand Up @@ -235,6 +243,7 @@ source.file -> source-lang1.file -> original content
-> source-lang2.file -> original content
```


### includeOriginal

Type: boolean|string|string[]|RegExp|RegExp[]
Expand All @@ -252,6 +261,15 @@ source.file -> source.file -> original content
```


### verbose

Type: boolean
Default: ```false```

This option will log a lot more information to the console. Enable this if you can't quite get the plugin to do what you want it to and
need some more details.



## Translation Files

Expand Down Expand Up @@ -395,3 +413,5 @@ Maybe I'll implement these one day, maybe not.
* Support streams... although that seems like a pain to implement
* Support printing of token trees if they are nested (as a json object that can e.g. be parsed by another script... although at this
point you might just as well include the original source file and not have it run through here.)
* Look for language files recursively
* Merge multiple language files (Using some sort of merge pattern? or by just merging files that have the same filename without ext & path)
15 changes: 15 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,23 @@ describe('gulp-international', () => {
done();
});
});


it('should log a lot more when verbose is enabled', done => {
var content = '<html><body><h1>R.emptyToken</h1></body></html>';
var options = {
locales: 'test/locales',
verbose: true
};
gently.expect(gutil, 'log', 12, function () {});
helper(options, content, () => {
gently.verify();
done();
});
});
});


describe('Error cases', () => {

it('should work with A number of delimiters', done => {
Expand Down

0 comments on commit 15d7e57

Please sign in to comment.