i18next-parser
Advanced tools
Comparing version 1.0.0-beta4 to 1.0.0-beta6
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.populateHash = exports.mergeHashes = exports.dotPathToHash = undefined;var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {return typeof obj;} : function (obj) {return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;};var _lodash = require('lodash');var _lodash2 = _interopRequireDefault(_lodash);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} | ||
// Takes a `path` of the form 'foo.bar' and | ||
// turn it into a hash {foo: {bar: ""}}. | ||
// The generated hash can be attached to an | ||
// optional `hash`. | ||
function dotPathToHash(path) {var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '.';var value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';var target = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
// Turn an entry for the Parser and turn in into a hash, | ||
// turning the key path 'foo.bar' into an hash {foo: {bar: ""}} | ||
// The generated hash can be attached to an optional `target`. | ||
function dotPathToHash(entry) {var target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var path = entry.key; | ||
var separator = options.separator || '.'; | ||
var value = entry.defaultValue || options.value || ''; | ||
if (path.endsWith(separator)) { | ||
@@ -16,2 +19,5 @@ path = path.slice(0, -separator.length); | ||
segments.reduce(function (hash, segment, index) { | ||
if (!segment) { | ||
return hash; | ||
} else | ||
if (index === segments.length - 1) { | ||
@@ -18,0 +24,0 @@ hash[segment] = value; |
@@ -9,3 +9,4 @@ 'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _extends = Object.assign || function (target) {for (var i = 1; i < arguments.length; i++) {var source = arguments[i];for (var key in source) {if (Object.prototype.hasOwnProperty.call(source, key)) {target[key] = source[key];}}}return target;};var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}();var _helpers = require('./helpers'); | ||
var _vinyl = require('vinyl');var _vinyl2 = _interopRequireDefault(_vinyl); | ||
var _yamljs = require('yamljs');var _yamljs2 = _interopRequireDefault(_yamljs);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self, call) {if (!self) {throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call && (typeof call === "object" || typeof call === "function") ? call : self;}function _inherits(subClass, superClass) {if (typeof superClass !== "function" && superClass !== null) {throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);}subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } });if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;}var | ||
var _yamljs = require('yamljs');var _yamljs2 = _interopRequireDefault(_yamljs); | ||
var _baseLexer = require('./lexers/base-lexer');var _baseLexer2 = _interopRequireDefault(_baseLexer);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self, call) {if (!self) {throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call && (typeof call === "object" || typeof call === "function") ? call : self;}function _inherits(subClass, superClass) {if (typeof superClass !== "function" && superClass !== null) {throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);}subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } });if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;}var | ||
@@ -19,3 +20,3 @@ i18nTransform = function (_Transform) {_inherits(i18nTransform, _Transform); | ||
contextSeparator: '_', | ||
createOldLibraries: true, | ||
createOldCatalogs: true, | ||
defaultNamespace: 'translation', | ||
@@ -33,2 +34,3 @@ defaultValue: '', | ||
output: 'locales', | ||
reactNamespace: false, | ||
sort: false }; | ||
@@ -40,3 +42,3 @@ | ||
_this.parser = new _parser2.default(_this.options.lexers); | ||
_this.parser = new _parser2.default(_this.options); | ||
_this.parser.on('error', function (error) {return _this.emit('error', error);}); | ||
@@ -52,3 +54,3 @@ _this.parser.on('warning', function (warning) {return _this.emit('warning', warning);}); | ||
if (file.isBuffer()) { | ||
content = file.contents; | ||
content = file.contents.toString('utf8'); | ||
} else | ||
@@ -61,4 +63,4 @@ { | ||
var extenstion = _path2.default.extname(file.path).substring(1); | ||
var entries = this.parser.parse(content, extenstion); | ||
var extension = _path2.default.extname(file.path).substring(1); | ||
var entries = this.parser.parse(content, extension); | ||
@@ -72,5 +74,6 @@ entries.forEach(function (entry) { | ||
} else | ||
{ | ||
entry.namespace = _this2.options.defaultNamespace; | ||
if (extension === 'jsx' || _this2.options.reactNamespace) { | ||
entry.namespace = _this2.grabReactNamespace(content); | ||
} | ||
entry.namespace = entry.namespace || _this2.options.defaultNamespace; | ||
@@ -100,7 +103,9 @@ key = parts.join(_this2.options.namespaceSeparator); | ||
catalog = (0, _helpers.dotPathToHash)( | ||
entry.key, | ||
_this3.options.keySeparator, | ||
entry.defaultValue || _this3.options.defaultValue, | ||
catalog); | ||
entry, | ||
catalog, | ||
{ | ||
separator: _this3.options.keySeparator, | ||
value: _this3.options.defaultValue }); | ||
}); | ||
@@ -146,3 +151,3 @@ | ||
_this3.pushFile(namespacePath, newCatalog); | ||
if (_this3.options.createOldLibraries) { | ||
if (_this3.options.createOldCatalogs) { | ||
_this3.pushFile(namespaceOldPath, oldCatalog); | ||
@@ -215,2 +220,12 @@ } | ||
this.push(file); | ||
} }, { key: 'grabReactNamespace', value: function grabReactNamespace( | ||
content) { | ||
var reactTranslateRegex = new RegExp( | ||
'translate\\((?:\\s*\\[?\\s*)(' + _baseLexer2.default.stringPattern + ')'); | ||
var translateMatches = content.match(reactTranslateRegex); | ||
if (translateMatches) { | ||
return translateMatches[1].slice(1, -1); | ||
} | ||
} }]);return i18nTransform;}(_stream.Transform);exports.default = i18nTransform; |
@@ -18,3 +18,3 @@ 'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _extends = Object.assign || function (target) {for (var i = 1; i < arguments.length; i++) {var source = arguments[i];for (var key in source) {if (Object.prototype.hasOwnProperty.call(source, key)) {target[key] = source[key];}}}return target;};var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}();var _baseLexer = require('./base-lexer');var _baseLexer2 = _interopRequireDefault(_baseLexer);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self, call) {if (!self) {throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call && (typeof call === "object" || typeof call === "function") ? call : self;}function _inherits(subClass, superClass) {if (typeof superClass !== "function" && superClass !== null) {throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);}subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } });if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;}var | ||
var regex = new RegExp( | ||
'<([A-Z][A-Z0-9]*)([^>]*\\s' + this.attr + '[^>]*)>(?:(.*?)<\\/\\1>)?', | ||
'<([A-Z][A-Z0-9]*)([^>]*\\s' + this.attr + '[^>]*)>(?:((?:\\s|.)*?)<\\/\\1>)?', | ||
'gi');var _loop = function _loop() { | ||
@@ -21,0 +21,0 @@ |
@@ -5,2 +5,3 @@ 'use strict';Object.defineProperty(exports, "__esModule", { value: true });var _extends = Object.assign || function (target) {for (var i = 1; i < arguments.length; i++) {var source = arguments[i];for (var key in source) {if (Object.prototype.hasOwnProperty.call(source, key)) {target[key] = source[key];}}}return target;};var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}();var _events = require('events');var _events2 = _interopRequireDefault(_events); | ||
var _javascriptLexer = require('./lexers/javascript-lexer');var _javascriptLexer2 = _interopRequireDefault(_javascriptLexer); | ||
var _jsxLexer = require('./lexers/jsx-lexer');var _jsxLexer2 = _interopRequireDefault(_jsxLexer); | ||
var _path = require('path');var _path2 = _interopRequireDefault(_path);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self, call) {if (!self) {throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call && (typeof call === "object" || typeof call === "function") ? call : self;}function _inherits(subClass, superClass) {if (typeof superClass !== "function" && superClass !== null) {throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);}subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } });if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;} | ||
@@ -16,2 +17,3 @@ | ||
js: ['JavascriptLexer'], | ||
jsx: ['JavascriptLexer', 'JsxLexer'], | ||
mjs: ['JavascriptLexer'], | ||
@@ -25,3 +27,4 @@ | ||
HTMLLexer: _htmlLexer2.default, | ||
JavascriptLexer: _javascriptLexer2.default };var | ||
JavascriptLexer: _javascriptLexer2.default, | ||
JsxLexer: _jsxLexer2.default };var | ||
@@ -32,3 +35,9 @@ | ||
options)); | ||
_this.lexers = _extends({}, lexers, options);return _this; | ||
_this.options = options; | ||
if (options.reactNamespace) { | ||
lexers.js = lexers.jsx; | ||
} | ||
_this.lexers = _extends({}, lexers, options.lexers);return _this; | ||
}_createClass(Parser, [{ key: 'parse', value: function parse( | ||
@@ -35,0 +44,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"name": "i18next-parser", | ||
"version": "1.0.0-beta4", | ||
"version": "1.0.0-beta6", | ||
"license": "MIT", | ||
@@ -14,3 +14,3 @@ "main": "src/index.js", | ||
"test": "mocha --require babel-register --require babel-polyfill 'test/**/*.test.js'", | ||
"watch": "babel src -d dist -w" | ||
"watch": "./node_modules/babel-cli/bin/babel.js src -d dist -w" | ||
}, | ||
@@ -17,0 +17,0 @@ "repository": { |
@@ -185,3 +185,5 @@ import { assert } from 'chai' | ||
second: '', | ||
third: 'Hello <strong title={t(\'fourth\')}>{{name}}</strong>, you have {{count}} unread message. <Link to="/msgs">Go to messages</Link>.', | ||
third: { | ||
first: 'Hello <strong title={t(\'fourth\')}>{{name}}</strong>, you have {{count}} unread message. <Link to="/msgs">Go to messages</Link>.' | ||
}, | ||
fourth: '' | ||
@@ -546,3 +548,5 @@ } | ||
second: '', | ||
third: 'Hello <strong title={t(\'fourth\')}>{{name}}</strong>, you have {{count}} unread message. <Link to="/msgs">Go to messages</Link>.', | ||
third: { | ||
first: 'Hello <strong title={t(\'fourth\')}>{{name}}</strong>, you have {{count}} unread message. <Link to="/msgs">Go to messages</Link>.' | ||
}, | ||
fourth: '' | ||
@@ -549,0 +553,0 @@ } |
@@ -11,3 +11,3 @@ import React from 'react' | ||
<Interpolate i18nKey="second" value="some thing" component={interpolateComponent} /> | ||
<Trans i18nKey="third" count={count}> | ||
<Trans i18nKey="third.first" count={count}> | ||
Hello <strong title={t('fourth')}>{{name}}</strong>, you have {{count}} unread message. <Link to="/msgs">Go to messages</Link>. | ||
@@ -14,0 +14,0 @@ </Trans> |
207581
51
3003