Skip to content

Commit

Permalink
Added optional caching
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi-nextbit committed Apr 1, 2016
1 parent 3c870f8 commit c6f0686
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 190 deletions.
51 changes: 32 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ var through = require('through2');
*/
var dictionaries = {};

/**
* A cache for previously loaded dictionaries so we don't have to load them again
* @type {Array}
*/
var cache = [];

/**
* Defauls options that are used if they are not overwritten by the user.
* @type {Object}
Expand All @@ -23,37 +29,44 @@ var defaults = {
locales: './locales',
delimiter: {
prefix: 'R.',
stopCondition: /[;,<>\{}()\[\]"'\s$]/
stopCondition: /[\-;,<>\{}()\[\]"'\s$]/
},
filename: '${path}/${name}-${lang}.${ext}',
blacklist: [],
warn: true
warn: true,
cache: true
};

/**
* Loads the dictionaries from the locale directory.
* @param options
*/
function load(options) {
var files = fs.readdirSync(options.locales);
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)));
break;
case '.ini':
var iniData = fs.readFileSync(path.join(process.cwd(), options.locales, file));
dictionaries[path.basename(file, path.extname(file))] = flat(ini2json(iniData));
break;
case '.csv':
var csvData = fs.readFileSync(path.join(process.cwd(), options.locales, file));
dictionaries[path.basename(file, path.extname(file))] = csv2json(csvData);
break;
if (cache[options.locales]) {
dictionaries = cache[options.locales];
} else {
var files = fs.readdirSync(options.locales);
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)));
break;
case '.ini':
var iniData = fs.readFileSync(path.join(process.cwd(), options.locales, file));
dictionaries[path.basename(file, path.extname(file))] = flat(ini2json(iniData));
break;
case '.csv':
var csvData = fs.readFileSync(path.join(process.cwd(), options.locales, file));
dictionaries[path.basename(file, path.extname(file))] = csv2json(csvData);
break;
}
if (options.cache) {
cache[options.locales] = dictionaries;
}
}
}

if (!Object.keys(dictionaries).length) {
throw new Error('No translation dictionaries have been found!');
}
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": "0.0.4",
"version": "0.0.5",
"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
11 changes: 10 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Default:
```
{
prefix: 'R.',
stopCondition: /[;,\.<>\{}()\[\]"'\s$]/
stopCondition: /[\-;,\.<>\{}()\[\]"'\s$]/
}
```

Expand Down Expand Up @@ -118,6 +118,15 @@ Default: ```true```
This enables warning to be printed out if any tokens are missing.


### cache

Type: boolean
Default: ```true```

This enables caching of dictionaries, so that reruns of the plugin will not have to read in the translation tokens again. Depending
on your configuration you might want to disable caching based on how your livereloads are configured.


## Feature Ideas for the future

Maybe I'll implement these one day, maybe not.
Expand Down
Loading

0 comments on commit c6f0686

Please sign in to comment.