i18next-parser
Advanced tools
Comparing version 3.2.0 to 3.3.0
# Changelog | ||
# 3.2.0 - latest | ||
# 3.3.0 - latest | ||
- Fix customValueTemplate interpolation of ${key} #242 | ||
- Extract options as third parameter when second parameter is default value string #243 #241 | ||
# 3.2.0 | ||
- Fix defaultValue for plural forms #240 #212 | ||
@@ -6,0 +11,0 @@ |
@@ -15,3 +15,3 @@ 'use strict';Object.defineProperty(exports, "__esModule", { value: true });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;}; /** | ||
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 path = entry.keyWithNamespace; | ||
if (options.suffix || options.suffix === 0) { | ||
@@ -33,5 +33,5 @@ path += '_' + options.suffix; | ||
if (options.useKeysAsDefaultValue) { | ||
newValue = entry.key.substring( | ||
entry.key.indexOf(separator) + separator.length, | ||
entry.key.length); | ||
newValue = entry.keyWithNamespace.substring( | ||
entry.keyWithNamespace.indexOf(separator) + separator.length, | ||
entry.keyWithNamespace.length); | ||
@@ -38,0 +38,0 @@ } |
@@ -142,2 +142,3 @@ 'use strict';Object.defineProperty(exports, "__esModule", { value: true });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 _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); | ||
// Second argument could be a string default value | ||
if ( | ||
@@ -148,3 +149,6 @@ optionsArgument && | ||
entry.defaultValue = optionsArgument.text; | ||
} else if ( | ||
optionsArgument = node.arguments.shift(); | ||
} | ||
if ( | ||
optionsArgument && | ||
@@ -151,0 +155,0 @@ optionsArgument.kind === ts.SyntaxKind.ObjectLiteralExpression) |
@@ -113,3 +113,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'); | ||
key = key.replace(/\\\\/g, '\\'); | ||
entry.key = entry.namespace + this.options.keySeparator + key; | ||
entry.key = key; | ||
entry.keyWithNamespace = entry.namespace + this.options.keySeparator + key; | ||
@@ -255,2 +256,4 @@ this.addEntry(entry); | ||
contextEntry.key += this.options.contextSeparator + entry.context; | ||
contextEntry.keyWithNamespace += | ||
this.options.contextSeparator + entry.context; | ||
this.entries.push(contextEntry); | ||
@@ -257,0 +260,0 @@ } else { |
@@ -5,3 +5,3 @@ { | ||
"name": "i18next-parser", | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "main": "dist/index.js", |
@@ -30,4 +30,6 @@ # i18next Parser [](https://travis-ci.org/i18next/i18next-parser) | ||
`1.x` has been in beta for a good while. You can follow the pre-releases [here](https://github.com/i18next/i18next-parser/releases). It is a deep rewrite of this package that solves many issues, the main one being that it was slowly becoming unmaintainable. The [migration](docs/migration.md) from `0.x` contains all the breaking changes. Everything that follows is related to `1.x`. You can still find the old `0.x` documentation on its dedicated [branch](https://github.com/i18next/i18next-parser/tree/0.x.x). | ||
You can find information about major releases on the [dedicated page](https://github.com/i18next/i18next-parser/releases). The [migration documentation](docs/migration.md) will help you figure out the breaking changes between versions. | ||
For legacy user on `0.x`, the code has since been entirely rewritten and there is a dedicated [branch](https://github.com/i18next/i18next-parser/tree/0.x.x) for it. You are hihgly encouraged to upgrade! | ||
## Usage | ||
@@ -34,0 +36,0 @@ |
@@ -15,3 +15,3 @@ /** | ||
function dotPathToHash(entry, target = {}, options = {}) { | ||
let path = entry.key | ||
let path = entry.keyWithNamespace | ||
if (options.suffix || options.suffix === 0) { | ||
@@ -33,5 +33,5 @@ path += `_${options.suffix}` | ||
if (options.useKeysAsDefaultValue) { | ||
newValue = entry.key.substring( | ||
entry.key.indexOf(separator) + separator.length, | ||
entry.key.length | ||
newValue = entry.keyWithNamespace.substring( | ||
entry.keyWithNamespace.indexOf(separator) + separator.length, | ||
entry.keyWithNamespace.length | ||
) | ||
@@ -38,0 +38,0 @@ } |
@@ -140,4 +140,5 @@ import BaseLexer from './base-lexer' | ||
const optionsArgument = node.arguments.shift() | ||
let optionsArgument = node.arguments.shift() | ||
// Second argument could be a string default value | ||
if ( | ||
@@ -148,3 +149,6 @@ optionsArgument && | ||
entry.defaultValue = optionsArgument.text | ||
} else if ( | ||
optionsArgument = node.arguments.shift() | ||
} | ||
if ( | ||
optionsArgument && | ||
@@ -151,0 +155,0 @@ optionsArgument.kind === ts.SyntaxKind.ObjectLiteralExpression |
@@ -113,3 +113,4 @@ import { dotPathToHash, mergeHashes, transferValues } from './helpers' | ||
key = key.replace(/\\\\/g, '\\') | ||
entry.key = entry.namespace + this.options.keySeparator + key | ||
entry.key = key | ||
entry.keyWithNamespace = entry.namespace + this.options.keySeparator + key | ||
@@ -255,2 +256,4 @@ this.addEntry(entry) | ||
contextEntry.key += this.options.contextSeparator + entry.context | ||
contextEntry.keyWithNamespace += | ||
this.options.contextSeparator + entry.context | ||
this.entries.push(contextEntry) | ||
@@ -257,0 +260,0 @@ } else { |
@@ -6,3 +6,3 @@ import { assert } from 'chai' | ||
it('creates an object from a string path', (done) => { | ||
const { target, duplicate } = dotPathToHash({ key: 'one' }) | ||
const { target, duplicate } = dotPathToHash({ keyWithNamespace: 'one' }) | ||
assert.deepEqual(target, { one: '' }) | ||
@@ -14,3 +14,7 @@ assert.equal(duplicate, false) | ||
it('ignores trailing separator', (done) => { | ||
const { target } = dotPathToHash({ key: 'one.' }, {}, { separator: '.' }) | ||
const { target } = dotPathToHash( | ||
{ keyWithNamespace: 'one.' }, | ||
{}, | ||
{ separator: '.' } | ||
) | ||
assert.deepEqual(target, { one: '' }) | ||
@@ -21,3 +25,3 @@ done() | ||
it('ignores duplicated separator', (done) => { | ||
const { target } = dotPathToHash({ key: 'one..two' }) | ||
const { target } = dotPathToHash({ keyWithNamespace: 'one..two' }) | ||
assert.deepEqual(target, { one: { two: '' } }) | ||
@@ -28,3 +32,7 @@ done() | ||
it('supports custom separator', (done) => { | ||
const { target } = dotPathToHash({ key: 'one-two' }, {}, { separator: '-' }) | ||
const { target } = dotPathToHash( | ||
{ keyWithNamespace: 'one-two' }, | ||
{}, | ||
{ separator: '-' } | ||
) | ||
assert.deepEqual(target, { one: { two: '' } }) | ||
@@ -36,3 +44,3 @@ done() | ||
const { target } = dotPathToHash( | ||
{ key: 'namespace-two-three' }, | ||
{ keyWithNamespace: 'namespace-two-three' }, | ||
{}, | ||
@@ -47,3 +55,3 @@ { separator: '-', useKeysAsDefaultValue: true } | ||
const { target, duplicate } = dotPathToHash( | ||
{ key: 'one.two.three' }, | ||
{ keyWithNamespace: 'one.two.three' }, | ||
{ one: { twenty: '' } } | ||
@@ -58,3 +66,3 @@ ) | ||
const { target } = dotPathToHash( | ||
{ key: 'one' }, | ||
{ keyWithNamespace: 'one' }, | ||
{}, | ||
@@ -69,3 +77,3 @@ { value: 'myDefaultValue' } | ||
const { target } = dotPathToHash( | ||
{ key: 'one_two_three.' }, | ||
{ keyWithNamespace: 'one_two_three.' }, | ||
{}, | ||
@@ -80,3 +88,3 @@ { separator: '_' } | ||
const { target, duplicate, conflict } = dotPathToHash( | ||
{ key: 'one.two.three' }, | ||
{ keyWithNamespace: 'one.two.three' }, | ||
{ one: { two: { three: '' } } } | ||
@@ -92,3 +100,3 @@ ) | ||
const { target, duplicate, conflict } = dotPathToHash( | ||
{ key: 'one.two.three', defaultValue: 'new' }, | ||
{ keyWithNamespace: 'one.two.three', defaultValue: 'new' }, | ||
{ one: { two: { three: 'old' } } } | ||
@@ -104,3 +112,3 @@ ) | ||
const { target, duplicate, conflict } = dotPathToHash( | ||
{ key: 'one', defaultValue: 'bla' }, | ||
{ keyWithNamespace: 'one', defaultValue: 'bla' }, | ||
{ one: { two: { three: 'bla' } } } | ||
@@ -116,3 +124,3 @@ ) | ||
const { target, duplicate, conflict } = dotPathToHash( | ||
{ key: 'one.two.three', defaultValue: 'bla' }, | ||
{ keyWithNamespace: 'one.two.three', defaultValue: 'bla' }, | ||
{ one: 'bla' } | ||
@@ -119,0 +127,0 @@ ) |
@@ -1120,2 +1120,26 @@ import { assert } from 'chai' | ||
it('generates plurals for defaultValue as second parameter', (done) => { | ||
let result | ||
const i18nextParser = new i18nTransform() | ||
const fakeFile = new Vinyl({ | ||
contents: Buffer.from("t('key', 'test {{count}}', { count })"), | ||
path: 'file.js', | ||
}) | ||
i18nextParser.on('data', (file) => { | ||
if (file.relative.endsWith(enLibraryPath)) { | ||
result = JSON.parse(file.contents) | ||
} | ||
}) | ||
i18nextParser.once('end', () => { | ||
assert.deepEqual(result, { | ||
key: 'test {{count}}', | ||
key_plural: 'test {{count}}', | ||
}) | ||
done() | ||
}) | ||
i18nextParser.end(fakeFile) | ||
}) | ||
it('generates plurals for different defaultValue in singular and plural form', (done) => { | ||
@@ -1147,3 +1171,3 @@ let result | ||
it('generates plurals for different defaultValue in singular plural forms with fallback', (done) => { | ||
it('generates plurals for different defaultValue in plural forms with fallback', (done) => { | ||
let result | ||
@@ -1249,3 +1273,3 @@ const i18nextParser = new i18nTransform({ | ||
it('supports customValueTemplate option', (done) => { | ||
it.only('supports customValueTemplate option', (done) => { | ||
let result | ||
@@ -1256,2 +1280,4 @@ const i18nextParser = new i18nTransform({ | ||
description: '${max}', | ||
namespace: '${namespace}', | ||
key: '${key}', | ||
}, | ||
@@ -1276,2 +1302,4 @@ }) | ||
description: '', | ||
namespace: 'translation', | ||
key: 'test', | ||
}, | ||
@@ -1281,2 +1309,4 @@ salt: { | ||
description: '150', | ||
namespace: 'translation', | ||
key: 'salt', | ||
}, | ||
@@ -1283,0 +1313,0 @@ }) |
5180
367
407511
83