i18next-scanner
Advanced tools
Comparing version 2.6.1 to 2.6.2
@@ -17,2 +17,6 @@ 'use strict'; | ||
var _acornJsxWalk = require('acorn-jsx-walk'); | ||
var _acornJsxWalk2 = _interopRequireDefault(_acornJsxWalk); | ||
var _chalk = require('chalk'); | ||
@@ -56,5 +60,5 @@ | ||
var _jsxToString = require('./jsx-to-string'); | ||
var _nodesToString = require('./nodes-to-string'); | ||
var _jsxToString2 = _interopRequireDefault(_jsxToString); | ||
var _nodesToString2 = _interopRequireDefault(_nodesToString); | ||
@@ -250,9 +254,2 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var getStringFromAttribute = function getStringFromAttribute(attr) { | ||
if (attr[0] === '"' || attr[0] === '\'') { | ||
return attr.slice(1, -1); | ||
} | ||
throw new Error('attribute value must be a string'); | ||
}; | ||
/** | ||
@@ -479,2 +476,4 @@ * Creates a new parser | ||
value: function parseTransFromString(content) { | ||
var _this3 = this; | ||
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
@@ -492,53 +491,62 @@ var customHandler = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; | ||
var reTrans = new RegExp('<' + component + '([^]*?)((/>)|(>([^]*?)</\\s*' + component + '\\s*>))', 'gim'); | ||
var reAttribute = /\b(\S+)\s*=\s*({.*?}|".*?"|'.*?')/gm; | ||
try { | ||
(0, _acornJsxWalk2.default)(content, { | ||
JSXElement: function JSXElement(node) { | ||
if (node.openingElement.name.name !== component) { | ||
return; | ||
} | ||
var r = void 0; | ||
while (r = reTrans.exec(content)) { | ||
var attributes = {}; | ||
var ar = void 0; | ||
while (ar = reAttribute.exec(r[1])) { | ||
attributes[ar[1]] = ar[2]; | ||
} | ||
var transKey = void 0; | ||
var attr = (0, _ensureArray2.default)(node.openingElement.attributes).reduce(function (acc, attribute) { | ||
if (attribute.type !== 'JSXAttribute' || attribute.name.type !== 'JSXIdentifier') { | ||
return acc; | ||
} | ||
try { | ||
transKey = attributes[i18nKey] ? getStringFromAttribute(attributes[i18nKey]) : ''; | ||
} catch (e) { | ||
this.log('i18next-scanner: i18nKey value must be a static string, saw ' + _chalk2.default.yellow(attributes[i18nKey])); | ||
continue; | ||
} | ||
var name = attribute.name.name; | ||
var defaultsString = void 0; | ||
try { | ||
defaultsString = attributes[defaultsKey] ? getStringFromAttribute(attributes[defaultsKey]) : ''; | ||
} catch (e) { | ||
this.log('i18next-scanner: defaults value must be a static string, saw ' + _chalk2.default.yellow(attributes[defaultsKey])); | ||
continue; | ||
} | ||
var _key2 = _lodash2.default.trim(transKey || ''); | ||
var code = _lodash2.default.trim(r[5]); | ||
var _options = { | ||
defaultValue: defaultsString || (0, _jsxToString2.default)(code), | ||
fallbackKey: opts.fallbackKey || this.options.trans.fallbackKey | ||
}; | ||
if (attributes.count) { | ||
_options.count = 1; | ||
} | ||
if (attributes.context) { | ||
try { | ||
_options.context = getStringFromAttribute(attributes.context); | ||
} catch (e) { | ||
this.log('i18next-scanner: Trans context attribute must be a string, saw ' + _chalk2.default.yellow(attributes.context)); | ||
continue; | ||
} | ||
} | ||
if (attribute.value.type === 'Literal') { | ||
acc[name] = attribute.value.value; | ||
} else if (attribute.value.type === 'JSXExpressionContainer') { | ||
acc[name] = attribute.value.expression; | ||
} | ||
if (customHandler) { | ||
customHandler(_key2, _options); | ||
continue; | ||
} | ||
return acc; | ||
}, {}); | ||
this.set(_key2, _options); | ||
var transKey = _lodash2.default.trim(attr[i18nKey]); | ||
var defaultsString = attr[defaultsKey] || ''; | ||
if (typeof defaultsString !== 'string') { | ||
_this3.log('i18next-scanner: defaults value must be a static string, saw ' + _chalk2.default.yellow(defaultsString)); | ||
} | ||
var options = { | ||
defaultValue: defaultsString || (0, _nodesToString2.default)(node.children), | ||
fallbackKey: opts.fallbackKey || _this3.options.trans.fallbackKey | ||
}; | ||
if (Object.prototype.hasOwnProperty.call(attr, 'count')) { | ||
options.count = Number(attr.count) || 0; | ||
} | ||
if (Object.prototype.hasOwnProperty.call(attr, 'context')) { | ||
options.context = attr.context; | ||
if (typeof options.context !== 'string') { | ||
_this3.log('i18next-scanner: The context attribute must be a string, saw ' + _chalk2.default.yellow(attr.context)); | ||
} | ||
} | ||
if (customHandler) { | ||
customHandler(transKey, options); | ||
return; | ||
} | ||
_this3.set(transKey, options); | ||
} | ||
}); | ||
} catch (err) { | ||
this.log('i18next-scanner: Unable to parse ' + component + ' component with the content'); | ||
this.log(err); | ||
this.log(content); | ||
} | ||
@@ -624,3 +632,3 @@ | ||
value: function get(key) { | ||
var _this3 = this; | ||
var _this4 = this; | ||
@@ -640,5 +648,5 @@ var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
Object.keys(this.resStore).forEach(function (lng) { | ||
Object.keys(_this3.resStore[lng]).forEach(function (ns) { | ||
var resStoreKeys = (0, _flattenObjectKeys2.default)(_lodash2.default.get(_this3.resStore, [lng, ns], {})); | ||
var resScanKeys = (0, _flattenObjectKeys2.default)(_lodash2.default.get(_this3.resScan, [lng, ns], {})); | ||
Object.keys(_this4.resStore[lng]).forEach(function (ns) { | ||
var resStoreKeys = (0, _flattenObjectKeys2.default)(_lodash2.default.get(_this4.resStore, [lng, ns], {})); | ||
var resScanKeys = (0, _flattenObjectKeys2.default)(_lodash2.default.get(_this4.resScan, [lng, ns], {})); | ||
var unusedKeys = _lodash2.default.differenceWith(resStoreKeys, resScanKeys, _lodash2.default.isEqual); | ||
@@ -719,3 +727,3 @@ | ||
value: function set(key) { | ||
var _this4 = this; | ||
var _this5 = this; | ||
@@ -775,21 +783,21 @@ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var _options2 = this.options, | ||
lngs = _options2.lngs, | ||
context = _options2.context, | ||
contextFallback = _options2.contextFallback, | ||
contextSeparator = _options2.contextSeparator, | ||
plural = _options2.plural, | ||
pluralFallback = _options2.pluralFallback, | ||
pluralSeparator = _options2.pluralSeparator, | ||
defaultLng = _options2.defaultLng, | ||
defaultValue = _options2.defaultValue; | ||
var _options = this.options, | ||
lngs = _options.lngs, | ||
context = _options.context, | ||
contextFallback = _options.contextFallback, | ||
contextSeparator = _options.contextSeparator, | ||
plural = _options.plural, | ||
pluralFallback = _options.pluralFallback, | ||
pluralSeparator = _options.pluralSeparator, | ||
defaultLng = _options.defaultLng, | ||
defaultValue = _options.defaultValue; | ||
lngs.forEach(function (lng) { | ||
var resLoad = _this4.resStore[lng] && _this4.resStore[lng][ns]; | ||
var resScan = _this4.resScan[lng] && _this4.resScan[lng][ns]; | ||
var resLoad = _this5.resStore[lng] && _this5.resStore[lng][ns]; | ||
var resScan = _this5.resScan[lng] && _this5.resScan[lng][ns]; | ||
if (!_lodash2.default.isPlainObject(resLoad)) { | ||
// Skip undefined namespace | ||
_this4.log('i18next-scanner: The namespace ' + _chalk2.default.yellow(JSON.stringify(ns)) + ' does not exist:', { key: key, options: options }); | ||
_this5.log('i18next-scanner: The namespace ' + _chalk2.default.yellow(JSON.stringify(ns)) + ' does not exist:', { key: key, options: options }); | ||
return; | ||
@@ -879,3 +887,3 @@ } | ||
} | ||
_this4.log('i18next-scanner: Added a new translation key { ' + _chalk2.default.yellow(JSON.stringify(resKey)) + ': ' + _chalk2.default.yellow(JSON.stringify(resLoad[resKey])) + ' } to ' + _chalk2.default.yellow(JSON.stringify(_this4.formatResourceLoadPath(lng, ns)))); | ||
_this5.log('i18next-scanner: Added a new translation key { ' + _chalk2.default.yellow(JSON.stringify(resKey)) + ': ' + _chalk2.default.yellow(JSON.stringify(resLoad[resKey])) + ' } to ' + _chalk2.default.yellow(JSON.stringify(_this5.formatResourceLoadPath(lng, ns)))); | ||
} else if (options.defaultValue) { | ||
@@ -887,3 +895,3 @@ if (!resLoad[resKey]) { | ||
// A default value has provided but it's different with the expected default | ||
_this4.log('i18next-scanner: The translation key ' + _chalk2.default.yellow(JSON.stringify(resKey)) + ' has a different default value, you may need to check the translation key of default language (' + defaultLng + ')'); | ||
_this5.log('i18next-scanner: The translation key ' + _chalk2.default.yellow(JSON.stringify(resKey)) + ' has a different default value, you may need to check the translation key of default language (' + defaultLng + ')'); | ||
} | ||
@@ -890,0 +898,0 @@ } |
{ | ||
"name": "i18next-scanner", | ||
"version": "2.6.1", | ||
"version": "2.6.2", | ||
"description": "Scan your code, extract translation keys/values, and merge them into i18n resource files.", | ||
@@ -51,2 +51,3 @@ "homepage": "https://github.com/i18next/i18next-scanner", | ||
"acorn-jsx": "^4.1.1", | ||
"acorn-jsx-walk": "^1.0.1", | ||
"chalk": "^2.4.1", | ||
@@ -53,0 +54,0 @@ "clone-deep": "^4.0.0", |
78863
11
1113
16
+ Addedacorn-jsx-walk@^1.0.1
+ Addedacorn@2.7.0(transitive)
+ Addedacorn-jsx@2.0.1(transitive)
+ Addedacorn-jsx-walk@1.0.1(transitive)