Skip to content

fix: nested array of objects#14

Merged
perrin4869 merged 2 commits into
masterfrom
fix/nested-array-of-objects
May 13, 2026
Merged

fix: nested array of objects#14
perrin4869 merged 2 commits into
masterfrom
fix/nested-array-of-objects

Conversation

@perrin4869

@perrin4869 perrin4869 commented May 13, 2026

Copy link
Copy Markdown
Collaborator

Ref: i18next/i18next-gettext-converter#428

There were a few false-positive tests in i18next-gettext-converter, and fixing those tests I found this use-case was broken. Translation files such as:

{
  "parent": {
    "questions": [
      {
        "title": "test question",
        "answer": "test answer"
      }
    ]
  }
}

would output:

msgid ""
msgstr ""
"Project-Id-Version: i18next-conv\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"

msgid "parent##questions"
msgstr "[object Object]"

Instead of

msgid ""
msgstr ""
"Project-Id-Version: i18next-conv\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"

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

msgid "parent##questions##0##answer"
msgstr "test answer"

The fix is to apply the current logic only if the array is confirmed to be an array of strings

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to fix flattening behavior so nested arrays containing objects (e.g., questions: [{ title, answer }]) are expanded into indexed keys instead of being coerced to "[object Object]" in generated gettext output.

Changes:

  • Update flattening logic to only treat arrays as “array-of-strings” when all elements are strings; otherwise recurse into array elements with an index suffix.
  • Regenerate/update the Browserify bundle (gettext.js) to reflect the flattening change (but it also includes additional unrelated bundle diffs).

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 3 comments.

File Description
lib/flatten.js Adds a string-array guard and recursion for non-string arrays (currently only in the old-format path).
gettext.js Updates the bundled output to include the new array handling, but also includes unrelated dependency/table changes.
Comments suppressed due to low confidence (1)

gettext.js:8301

  • This PR also changes the generated gettext.js bundle to include a modified safe-buffer implementation (new license header / prototype assignment). Since this is not directly related to the flattening logic change, it would be better to avoid unrelated bundle churn (pin deps + rebuild reproducibly) or explicitly justify the dependency update that caused it, as it could have subtle runtime impact in browser builds.
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
/* eslint-disable node/no-deprecated-api */
var buffer = require('buffer')
var Buffer = buffer.Buffer


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/flatten.js
Comment on lines 82 to +87
} 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'),

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

Comment thread lib/flatten.js
Comment on lines +95 to +97
value.forEach((v, i) => {
recurse(appendTo, v, `${key}${keyseparator}${i}`)
})

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.

Comment thread gettext.js
@perrin4869 perrin4869 merged commit cc3b8e2 into master May 13, 2026
3 checks passed
@perrin4869

Copy link
Copy Markdown
Collaborator Author

@adrai when you have time can you publish to npm? Thanks!

@adrai

adrai commented May 13, 2026

Copy link
Copy Markdown
Contributor

thx...
published in v1.3.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants