Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 50 additions & 19 deletions gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,23 @@ function _default(input) {
};
appendTo[appendKey] = kv;
} else if (Array.isArray(value)) {
kv = {
key: key,
value: value.join('\n'),
isArray: true,
isPlural: isPlural,
pluralNumber: isPlural ? number : 0,
context: context
};
appendTo[appendKey] = kv;
if (value.every(function (v) {
return typeof v === 'string';
})) {
kv = {
key: key,
value: value.join('\n'),
isArray: true,
isPlural: isPlural,
pluralNumber: isPlural ? number : 0,
context: context
};
appendTo[appendKey] = kv;
} else {
value.forEach(function (v, i) {
recurse(appendTo, v, "".concat(key).concat(keyseparator).concat(i));
});
}
} else {
recurse(appendTo, value, key);
}
Expand Down Expand Up @@ -536,15 +544,23 @@ function _default(input) {
};
appendTo[appendKey] = kv;
} else if (Array.isArray(value)) {
kv = {
key: key,
value: value.join('\n'),
isArray: true,
isPlural: isPlural,
pluralNumber: isPlural ? number : 0,
context: context
};
appendTo[appendKey] = kv;
if (value.every(function (v) {
return typeof v === 'string';
})) {
kv = {
key: key,
value: value.join('\n'),
isArray: true,
isPlural: isPlural,
pluralNumber: isPlural ? number : 0,
context: context
};
appendTo[appendKey] = kv;
} else {
value.forEach(function (v, i) {
recurse(appendTo, v, "".concat(key).concat(keyseparator).concat(i));
});
}
} else {
recurse(appendTo, value, key);
}
Expand Down Expand Up @@ -4686,7 +4702,19 @@ module.exports = {
'big5hkscs': {
type: '_dbcs',
table: function() { return require('./tables/cp950.json').concat(require('./tables/big5-added.json')) },
encodeSkipVals: [0xa2cc],
encodeSkipVals: [
// Although Encoding Standard says we should avoid encoding to HKSCS area (See Step 1 of
// https://encoding.spec.whatwg.org/#index-big5-pointer), we still do it to increase compatibility with ICU.
// But if a single unicode point can be encoded both as HKSCS and regular Big5, we prefer the latter.
0x8e69, 0x8e6f, 0x8e7e, 0x8eab, 0x8eb4, 0x8ecd, 0x8ed0, 0x8f57, 0x8f69, 0x8f6e, 0x8fcb, 0x8ffe,
Comment thread
perrin4869 marked this conversation as resolved.
0x906d, 0x907a, 0x90c4, 0x90dc, 0x90f1, 0x91bf, 0x92af, 0x92b0, 0x92b1, 0x92b2, 0x92d1, 0x9447, 0x94ca,
0x95d9, 0x96fc, 0x9975, 0x9b76, 0x9b78, 0x9b7b, 0x9bc6, 0x9bde, 0x9bec, 0x9bf6, 0x9c42, 0x9c53, 0x9c62,
0x9c68, 0x9c6b, 0x9c77, 0x9cbc, 0x9cbd, 0x9cd0, 0x9d57, 0x9d5a, 0x9dc4, 0x9def, 0x9dfb, 0x9ea9, 0x9eef,
0x9efd, 0x9f60, 0x9fcb, 0xa077, 0xa0dc, 0xa0df, 0x8fcc, 0x92c8, 0x9644, 0x96ed,

// Step 2 of https://encoding.spec.whatwg.org/#index-big5-pointer: Use last pointer for U+2550, U+255E, U+2561, U+256A, U+5341, or U+5345
0xa2a4, 0xa2a5, 0xa2a7, 0xa2a6, 0xa2cc, 0xa2ce,
],
},

'cnbig5': 'big5hkscs',
Expand Down Expand Up @@ -8274,6 +8302,7 @@ process.chdir = function (dir) {
process.umask = function() { return 0; };

},{}],44:[function(require,module,exports){
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
/* eslint-disable node/no-deprecated-api */
var buffer = require('buffer')
var Buffer = buffer.Buffer
Expand All @@ -8296,6 +8325,8 @@ function SafeBuffer (arg, encodingOrOffset, length) {
return Buffer(arg, encodingOrOffset, length)
}

SafeBuffer.prototype = Object.create(Buffer.prototype)

// Copy static methods from Buffer
copyProps(Buffer, SafeBuffer)

Expand Down
2 changes: 1 addition & 1 deletion gettext.min.js

Large diffs are not rendered by default.

46 changes: 29 additions & 17 deletions lib/flatten.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,22 @@ export default function (input, {
}
appendTo[appendKey] = kv
} else if (Array.isArray(value)) {
kv = {
if (value.every(v => typeof v === 'string')) {
kv = {
// id: key.replace(new RegExp(' ', 'g'), ''),
key,
value: value.join('\n'),
isArray: true,
isPlural,
pluralNumber: isPlural ? number : 0,
context
key,
value: value.join('\n'),
Comment on lines 82 to +87

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied the same "array of strings" guard in handleNewFormat so arrays of non-string elements now recurse with indexed keys instead of joining as [object Object]. Also added fixture and test cases for both default and compatibilityJSON: 'v4' modes. Commit: 9f4f02f

isArray: true,
isPlural,
pluralNumber: isPlural ? number : 0,
context
}
appendTo[appendKey] = kv
} else {
value.forEach((v, i) => {
recurse(appendTo, v, `${key}${keyseparator}${i}`)
})
Comment on lines +95 to +97

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests were already added in commit 9f4f02f: test/fixtures/example-nested-array.json and test/fixtures/example-nested-array.po as fixtures, with assertions for both default and compatibilityJSON: 'v4' modes in test/test.js under the nested array of objects describe block.

}
appendTo[appendKey] = kv
} else {
recurse(appendTo, value, key)
}
Expand Down Expand Up @@ -174,16 +180,22 @@ export default function (input, {
}
appendTo[appendKey] = kv
} else if (Array.isArray(value)) {
kv = {
// id: key.replace(new RegExp(' ', 'g'), ''),
key,
value: value.join('\n'),
isArray: true,
isPlural,
pluralNumber: isPlural ? number : 0,
context
if (value.every(v => typeof v === 'string')) {
kv = {
// id: key.replace(new RegExp(' ', 'g'), ''),
key,
value: value.join('\n'),
isArray: true,
isPlural,
pluralNumber: isPlural ? number : 0,
context
}
appendTo[appendKey] = kv
} else {
value.forEach((v, i) => {
recurse(appendTo, v, `${key}${keyseparator}${i}`)
})
}
appendTo[appendKey] = kv
} else {
recurse(appendTo, value, key)
}
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/example-nested-array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"parent": {
"questions": [
{
"title": "test question",
"answer": "test answer"
}
]
}
}
16 changes: 16 additions & 0 deletions test/fixtures/example-nested-array.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
msgid ""
msgstr ""
"Project-Id-Version: gettext-converter\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2020-04-17T10:46:16.313Z\n"
"PO-Revision-Date: 2020-04-17T10:46:16.313Z\n"
"Language: en-US\n"

msgid "parent##questions##0##title"
msgstr "test question"

msgid "parent##questions##0##answer"
msgstr "test answer"
4 changes: 4 additions & 0 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ module.exports = {
example_fallbackMsgId: {
jsi18next: require('./example-i18next_fallbackMsgId.json'),
poi18next: fs.readFileSync(path.join(__dirname, 'example-i18next_fallbackMsgId.po')).toString().replace(/\n$/, '')
},
example_nestedArray: {
jsi18next: require('./example-nested-array.json'),
poi18next: fs.readFileSync(path.join(__dirname, 'example-nested-array.po')).toString().replace(/\n$/, '')
}
}
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,24 @@ describe('Plural handling with empty msgid_plural', () => {
expect(res).to.eql(fixtures.example_emptyMsgidPlural.jsi18next)
})
})

describe('nested array of objects', () => {
test('i18next2po', (fn) => () => {
const res = fn('en-US', fixtures.example_nestedArray.jsi18next, {
potCreationDate: new Date('2020-04-17T10:46:16.313Z'),
poRevisionDate: new Date('2020-04-17T10:46:16.313Z')
})
expect(res).to.eql(fixtures.example_nestedArray.poi18next)
})

describe('i18next v4', () => {
test('i18next2po', (fn) => () => {
const res = fn('en-US', fixtures.example_nestedArray.jsi18next, {
compatibilityJSON: 'v4',
potCreationDate: new Date('2020-04-17T10:46:16.313Z'),
poRevisionDate: new Date('2020-04-17T10:46:16.313Z')
})
expect(res).to.eql(fixtures.example_nestedArray.poi18next)
})
})
})