Skip to content

Commit

Permalink
Most options now accept arrays as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi-nextbit committed Apr 4, 2016
1 parent 84a06a2 commit c4025d6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
32 changes: 16 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ var defaults = {
stopCondition: /[^\.\w_\-]/
},
filename: '${path}/${name}-${lang}.${ext}',
blacklist: [],
whitelist: true,
blacklist: false,
warn: true,
cache: true,
ignoreErrors: false,
Expand All @@ -53,11 +54,20 @@ function trueOrMatch(needle, haystack) {
if (needle === true) {
return true;
}
if (needle instanceof RegExp && needle.test(haystack)) {
if (_.isRegExp(needle) && needle.test(haystack)) {
return true;
}
return !!(needle instanceof String && haystack.indexOf(needle) !== -1);

if (_.isString(needle) && haystack.indexOf(needle) !== -1) {
return true;
}
if (needle instanceof Array) {
for (var i in needle) {
if (trueOrMatch(needle[i], haystack)) {
return true;
}
}
}
return false;
}


Expand Down Expand Up @@ -214,10 +224,8 @@ function csv2json(csvData) {
function translate(options, contents, copied, filePath) {
var processed = {};
for (var lang in dictionaries) {
if (!options.whitelist || options.whitelist.indexOf(lang) != -1) {
if (!processed[lang] && options.blacklist.indexOf(lang) == -1) {
processed[lang] = '';
}
if (!processed[lang] && trueOrMatch(options.whitelist, lang) && !trueOrMatch(options.blacklist, lang)) {
processed[lang] = '';
}
}
if (!Object.keys(processed).length) {
Expand Down Expand Up @@ -312,14 +320,6 @@ function replace(file, options) {
* @returns {Stream}
*/
module.exports = function(options) {
if (options) {
if (options.whitelist && !_.isArray(options.whitelist)) {
options.whitelist = [options.whitelist];
}
if (options.blacklist && !_.isArray(options.blacklist)) {
options.blacklist = [options.blacklist];
}
}
options = _.assign({}, defaults, options);
load(options);

Expand Down
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.8",
"version": "1.0.9",
"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
36 changes: 24 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ gulp.task('default', function () {

## Options

This module tries to be very flexible with it's configuration option. For many options you can define either a boolean, a string, a
regular expression or an array with a mixture of string and regular expressions. If a boolean is passed in the option is simply set
to true or false for all files. If the option is a string the filename is searched for this substring. If the option is regular
expression it is tested against each file, and finally if the option is an array the test is executed against every element of the
array and accepted if any of the elements returns true with the previous rules defined. You could even nest arrays if you wanted to.

An example:
```
options.ignoreTokens = [ 'de_DE', /en_*/ ]
// => matches any english language as well the German translation for Germany
```


### locales

Expand Down Expand Up @@ -137,27 +149,27 @@ source.file -> source-lang1.file -> translated content

### whitelist

Type: string|string[]
Default: ```undefined```
Type: boolean|string|string[]|RegExp|RegExp[]
Default: ```true```

This option allows to limit the number of translations that can be used. The names that are whitelisted need to match the filename
(without the extension). Any other files will still be loaded into the list of available dictionaries, but no files will be
generated. The option is ignored if it is missing. You can either define a single locale with a string or multiple with an array
of strings.
generated. The option will allow any locale if it's set to true. You can either define a single locale with a string/RegExp or multiple
with an array of strings and RegExp's.


### blacklist

Type: string|string[]
Default: ```undefined```
Type: boolean|string|string[]|RegExp|RegExp[]
Default: ```false```

The opposite of the whitelist. Any language specified here will be ignored during processing. You can either define a single locale
with a string or multiple with an array of strings.


### encodeEntities

Type: boolean|RegExp|String
Type: boolean|string|string[]|RegExp|RegExp[]
Default: ```true```

Any non utf8 characters that do not map to html are replaced with html entities if this option is enabled. A translation such as "über"
Expand All @@ -167,7 +179,7 @@ escaped. If the option is instead a string then the filename is searched for thi

### warn

Type: boolean|RegExp|String
Type: boolean|string|string[]|RegExp|RegExp[]
Default: ```true```

This enables warnings to be printed out if any tokens are missing. If the setting is a regular expression then only files with a matching
Expand All @@ -185,7 +197,7 @@ on your configuration you might want to disable caching based on how your livere

### ignoreErrors

Type: boolean|RegExp|String
Type: boolean|string|string[]|RegExp|RegExp[]
Default: ```false```

Allows to disable throwing of any errors that might occur so that the pipe will continue executing. If the setting is a regular expression
Expand All @@ -195,7 +207,7 @@ this substring.

### dryRun

Type: boolean|RegExp|String
Type: boolean|string|string[]|RegExp|RegExp[]
Default: ```false```

When set to true the plugin will perform all operations for a translation, but will pass on the original file along the pipe instead
Expand All @@ -210,7 +222,7 @@ source.file -> source.file -> original content

### ignoreTokens

Type: boolean|RegExp
Type: boolean|string|string[]|RegExp|RegExp[]
Default: ```false```

When set to true the plugin will ignore all tokens, but still create new files as if they were different for each language. This
Expand All @@ -225,7 +237,7 @@ source.file -> source-lang1.file -> original content

### includeOriginal

Type: boolean|RegExp
Type: boolean|string|string[]|RegExp|RegExp[]
Default: ```false```

When set to true the original file is passed along the pipe along with all translated files. If the setting is a regular expression then
Expand Down

0 comments on commit c4025d6

Please sign in to comment.