Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recognize nested object arrays and handle appropriately #101

Merged
merged 1 commit into from
Apr 23, 2021
Merged

recognize nested object arrays and handle appropriately #101

merged 1 commit into from
Apr 23, 2021

Conversation

ripvannwinkler
Copy link
Contributor

The key

{ "test": { array: [{ inner1: "testing" }]}}

should become:

msgid "test##array##inner1"
msgstr "testing"

Before this patch, it resulted in:

msgid "test##array"
msgstr "[object Object]"

Checklist

  • only relevant code is changed (make a diff before you submit the PR)
  • run tests npm run test
  • tests are included
  • documentation is changed or added

The key

  { "test": { array: [{ inner1: "testing" }]}}

should become:

  msgid "test##array##inner"
  msgstr "testing"

Before this patch, it resulted in:

  msgid "test##array"
  msgstr "[object Object]"
@ripvannwinkler
Copy link
Contributor Author

Found this bug attempting to convert a JSON file with deeply nested keys. For whatever reason, the original code didn't anticipate key/value nodes inside arrays. This patch checks to see if the value is an array and contains at least one object, and recurses if so.

@jamuhl
Copy link
Member

jamuhl commented Apr 14, 2021

This looks very limited to one case - should it not be more like JSON flattens array in most implementations like:

{ "test": { array: [{ inner1: "testing", inner2: "testing" }, { inner1: "testing", inner2: "testing" }]}}

// becomes
msgid "test##array##0##inner1"
...
msgid "test##array##0##inner2"
...
msgid "test##array##1##inner1"
...
msgid "test##array##1##inner2"
...

@ripvannwinkler
Copy link
Contributor Author

ripvannwinkler commented Apr 14, 2021

@jamuhl You are correct. This is actually what it does. I mistyped the result in my PR comment. If you look at the test files, this should confirm your expected result.

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

becomes

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"
"POT-Creation-Date: 2021-04-13T17:16:37.311Z\n"
"PO-Revision-Date: 2021-04-13T17:16:37.311Z\n"
"Language: en\n"

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

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

To be clear, I did not modify the logic that determines keys when flattening - I just added the extra recursion step for nested objects inside arrays.

@jamuhl
Copy link
Member

jamuhl commented Apr 14, 2021

@perrin4869 looks good to me...do you have time to publish a new version? or should I?!?

@jamuhl jamuhl merged commit 0ffdd15 into i18next:master Apr 23, 2021
@jamuhl
Copy link
Member

jamuhl commented Apr 23, 2021

@ripvannwinkler sorry, almost forgot about this one.

finally published in [email protected]

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.

2 participants