i18next-scanner
Advanced tools
Comparing version 0.5.9 to 0.6.0
@@ -124,3 +124,3 @@ var fs = require('fs'); | ||
*/ | ||
Parser.prototype.parse = function(key, defaultValue) { | ||
Parser.prototype.parse = function(key, defaultValue) { | ||
var fn = this.fn; | ||
@@ -133,3 +133,3 @@ var options = this.options; | ||
if (key.indexOf(options.nsseparator) > -1) { | ||
if (typeof options.nsseparator === 'string' && key.indexOf(options.nsseparator) > -1) { | ||
var parts = key.split(options.nsseparator); | ||
@@ -140,3 +140,7 @@ ns = parts[0]; | ||
var keys = key.split(options.keyseparator); | ||
// keep compatibility with i18next to supoprt disabling keyseparator | ||
// in case you want to have the key as a fallback value. | ||
var keys = typeof options.keyseparator === 'string' ? | ||
key.split(options.keyseparator) : | ||
[key]; | ||
_.each(options.lngs, function(lng) { | ||
@@ -154,5 +158,5 @@ var lookupKey; | ||
lookupKey = '[' + lng + '][' + ns + '][' + keys.join('][') + ']'; | ||
fn.debuglog('Found a value %s associated with the key %s in %s.', | ||
fn.debuglog('Found a value %s associated with the key %s in %s.', | ||
JSON.stringify(_.get(resStore, lookupKey)), | ||
JSON.stringify(keys.join(options.keyseparator)), | ||
JSON.stringify(key), | ||
JSON.stringify(fn.getResourcePath(lng, ns)) | ||
@@ -164,3 +168,3 @@ ); | ||
_.each(keys, function(elem, index) { | ||
if (index >= (keys.length - 1)) { | ||
if (index >= (keys.length - 1)) { | ||
res[elem] = _.isUndefined(defaultValue) ? options.defaultValue : defaultValue; | ||
@@ -175,3 +179,3 @@ } else { | ||
fn.debuglog('Adding a new entry {%s:%s} to %s.', | ||
JSON.stringify(keys.join(options.keyseparator)), | ||
JSON.stringify(key), | ||
JSON.stringify(_.get(resStore, lookupKey)), | ||
@@ -235,3 +239,3 @@ JSON.stringify(fn.getResourcePath(lng, ns)) | ||
* var str = ' id=nav-bar class = "top" foo = "bar\\"baz" '; | ||
* str.match(/([^=,\s]*)\s*=\s*((?:"(?:\\.|[^"\\]+)*"|'(?:\\.|[^'\\]+)*')|[^'"\s]*)/igm) || []; | ||
* str.match(/([^=,\s]*)\s*=\s*((?:"(?:\\.|[^"\\]+)*"|'(?:\\.|[^'\\]+)*')|[^'"\s]*)/igm) || []; | ||
* @param [string] str A string representation of hash arguments | ||
@@ -298,3 +302,3 @@ * @return {object} | ||
* @param {boolean} [opts.sort] True to sort object by key | ||
* @return {object} | ||
* @return {object} | ||
*/ | ||
@@ -301,0 +305,0 @@ Parser.prototype.toObject = function(opts) { |
{ | ||
"name": "i18next-scanner", | ||
"version": "0.5.9", | ||
"version": "0.6.0", | ||
"description": "i18next-scanner is a transfrom stream that can scan your code, extract translation keys/values, and merge them into i18n resource files.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/i18next/i18next-scanner", |
@@ -325,3 +325,3 @@ # i18next-scanner [![build status](https://travis-ci.org/i18next/i18next-scanner.svg?branch=master)](https://travis-ci.org/i18next/i18next-scanner) [![Coverage Status](https://coveralls.io/repos/i18next/i18next-scanner/badge.svg?branch=master&service=github)](https://coveralls.io/github/i18next/i18next-scanner?branch=master) | ||
Type: `String` Default: `':'` | ||
Type: `String` or `false` Default: `':'` | ||
@@ -332,3 +332,3 @@ The namespace separator. | ||
Type: `String` Default: `'.'` | ||
Type: `String` or `false` Default: `'.'` | ||
@@ -335,0 +335,0 @@ The key separator. |
'use strict'; | ||
var _ = require('lodash'); | ||
var fs = require('fs'); | ||
var gulp = require('gulp'); | ||
var gutil = require('gulp-util'); | ||
var path = require('path'); | ||
var test = require('tap').test; | ||
var i18nextScanner = require('../'); | ||
var hash = require('sha1'); | ||
var table = require('text-table'); | ||
var customTransform = require('./utils/transform'); | ||
var customTransform = function _transform(file, enc, done) { | ||
var parser = this.parser; | ||
var extname = path.extname(file.path); | ||
var content = fs.readFileSync(file.path, enc); | ||
var tableData = [ | ||
['Key', 'Value'] | ||
]; | ||
gutil.log('parsing ' + JSON.stringify(file.relative) + ':'); | ||
// Using i18next-text | ||
(function() { | ||
var results = content.match(/i18n\._\(("[^"]*"|'[^']*')\s*[\,\)]/igm) || ''; | ||
_.each(results, function(result) { | ||
var key, value; | ||
var r = result.match(/i18n\._\(("[^"]*"|'[^']*')/); | ||
if (r) { | ||
value = _.trim(r[1], '\'"'); | ||
// Replace double backslash with single backslash | ||
value = value.replace(/\\\\/g, '\\'); | ||
value = value.replace(/\\\'/, '\''); | ||
key = hash(value); // returns a hash value as its default key | ||
parser.parse(key, value); | ||
tableData.push([key, _.isUndefined(value) ? parser.options.defaultValue : value ]); | ||
} | ||
}); | ||
}()); | ||
// i18n function helper | ||
(function() { | ||
var results = content.match(/{{i18n\s+("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')?([^}]*)}}/gm) || []; | ||
_.each(results, function(result) { | ||
var key, value; | ||
var r = result.match(/{{i18n\s+("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')?([^}]*)}}/m) || []; | ||
if ( ! _.isUndefined(r[1])) { | ||
value = _.trim(r[1], '\'"'); | ||
// Replace double backslash with single backslash | ||
value = value.replace(/\\\\/g, '\\'); | ||
value = value.replace(/\\\'/, '\''); | ||
} | ||
var params = parser.parseHashArguments(r[2]); | ||
if (_.has(params, 'defaultKey')) { | ||
key = params['defaultKey']; | ||
} | ||
if (_.isUndefined(key) && _.isUndefined(value)) { | ||
return; | ||
} | ||
if (_.isUndefined(key)) { | ||
key = hash(value); // returns a hash value as its default key | ||
} | ||
parser.parse(key, value); | ||
tableData.push([key, _.isUndefined(value) ? parser.options.defaultValue : value ]); | ||
}); | ||
}()); | ||
// i18n block helper | ||
(function() { | ||
var results = content.match(/{{#i18n\s*([^}]*)}}((?:(?!{{\/i18n}})(?:.|\n))*){{\/i18n}}/gm) || []; | ||
_.each(results, function(result) { | ||
var key, value; | ||
var r = result.match(/{{#i18n\s*([^}]*)}}((?:(?!{{\/i18n}})(?:.|\n))*){{\/i18n}}/m) || []; | ||
if ( ! _.isUndefined(r[2])) { | ||
value = _.trim(r[2], '\'"'); | ||
} | ||
if (_.isUndefined(value)) { | ||
return; | ||
} | ||
key = hash(value); // returns a hash value as its default key | ||
parser.parse(key, value); | ||
tableData.push([key, _.isUndefined(value) ? parser.options.defaultValue : value ]); | ||
}); | ||
}()); | ||
if (_.size(tableData) > 1) { | ||
gutil.log('result of ' + JSON.stringify(file.relative) + ':\n' + table(tableData, {'hsep': ' | '})); | ||
} | ||
done(); | ||
}; | ||
test('setup', function(t) { | ||
@@ -111,0 +9,0 @@ t.end(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
105122
34
1026