Skip to content

Commit

Permalink
Better log message and better stop condition
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi-nextbit committed Apr 1, 2016
1 parent 30194f5 commit 4088a3c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
35 changes: 19 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var defaults = {
locales: './locales',
delimiter: {
prefix: 'R.',
stopCondition: /[^\.\w]/
stopCondition: /[^\.\w_\-]/
},
filename: '${path}/${name}-${lang}.${ext}',
blacklist: [],
Expand All @@ -42,7 +42,7 @@ var defaults = {

/**
* Loads the dictionaries from the locale directory.
* @param options
* @param {Object} options
*/
function load(options) {
if (cache[options.locales]) {
Expand Down Expand Up @@ -78,8 +78,8 @@ function load(options) {

/**
* Splits a line from an ini file into 2. Any subsequent '=' are ignored.
* @param line
* @returns {*[]}
* @param {string} line
* @returns {string[]}
*/
function splitIniLine(line) {
var separator = line.indexOf('=');
Expand Down Expand Up @@ -126,7 +126,8 @@ function ini2json(iniData) {

/**
* Converts a line of a CSV file to an array of strings, omitting empty fields.
* @param line
* @param {string} line
* @returns {string[]}
*/
function splitCsvLine(line) {
if (!line.trim().length) {
Expand Down Expand Up @@ -162,6 +163,7 @@ function splitCsvLine(line) {
/**
* Simple conversion helper to get a json file from a csv file.
* @param {string} csvData
* @returns {Object}
*/
function csv2json(csvData) {
var result = {};
Expand All @@ -183,12 +185,13 @@ function csv2json(csvData) {

/**
* Performs the actual translation from a tokenized source to the final content.
* @param options
* @param contents
* @param copied
* @returns {{}}
* @param {Object} options
* @param {string} contents
* @param {number} copied
* @param {string} filePath
* @returns {Object}
*/
function translate(options, contents, copied) {
function translate(options, contents, copied, filePath) {
var processed = {};
for (var lang in dictionaries) {
if (!options.whitelist || options.whitelist.indexOf(lang) != -1) {
Expand Down Expand Up @@ -224,7 +227,7 @@ function translate(options, contents, copied) {
if (dictionaries[lang][key]) {
processed[lang] += dictionaries[lang][key];
} else if(options.warn) {
gutil.log('Missing translation in language', lang, 'for key', key);
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 @@ -242,15 +245,15 @@ function translate(options, contents, copied) {

/**
* Performs the actual replacing of tokens with translations.
* @param file
* @param options
* @returns {*}
* @param {File} file
* @param {Object} options
* @returns {File[]}
*/
function replace(file, options) {
var contents = file.contents.toString('utf8');
var copied = 0;

var processed = translate(options, contents, copied);
var processed = translate(options, contents, copied, file.path);

var files = [];
for (var lang in processed) {
Expand Down Expand Up @@ -280,7 +283,7 @@ function replace(file, options) {
/**
* Returns a stream object that gulp can understand and pipe to.
* @param options
* @returns {WritableStream}
* @returns {Stream}
*/
module.exports = function (options) {
if (options) {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Default:
```
{
prefix: 'R.',
stopCondition: /[^\.\w]/
stopCondition: /[^\.\w_\-]/
}
```

Expand Down
4 changes: 3 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ describe('gulp-international', () => {
var content = '<html><body><h1>R.nonExistentToken</h1></body></html>';
var options = { locales: 'test/locales' };

gently.expect(gutil, 'log', 4);
gently.expect(gutil, 'log', 4, function() {
expect(arguments[3]).to.equal('nonExistentToken');
});

helper(options, content, () => {
gently.verify();
Expand Down

0 comments on commit 4088a3c

Please sign in to comment.