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

Loop on array of objects #83

Closed
scerelli opened this issue Aug 4, 2020 · 7 comments · Fixed by #105
Closed

Loop on array of objects #83

scerelli opened this issue Aug 4, 2020 · 7 comments · Fixed by #105
Assignees
Labels
enhancement New feature or request

Comments

@scerelli
Copy link

scerelli commented Aug 4, 2020

Describe the bug
Loop on array of objects

To Reproduce

team: {
 teammemberName.career: [
  {
    company: "Company name",
    description: "Whatevah here",
  }
]

then try to do something like this:

{#each $_(`team.${selectedMember}.career`) as c, i}
 <h5>{$_(`team.${selectedMember}.career.${i}.company`)}</h5>
{/each}

of course this won't work #each $_(`team.${selectedMember}.career`) loops more times than the actual length of career.

by reading the tests i discovered that the lib is flattening the array so it means that this each loop is looping on the strings instead of the array. Is this an expected behavior? what's the correct way to do this stuff?

@kaisermann
Copy link
Owner

Hey @scerelli 👋

This is expected behavior, as you can see in #50. The reason behind flattening the dictionaries is to simplify the merging of multiple partial dictionaries.

Also, I don't have experience with a lot of i18n libraries, do you know any that allow this kind of behavior?

@kaisermann kaisermann added the pending clarification Further information is requested label Aug 4, 2020
@archived-m
Copy link

I've written a custom getArrayFromLocale workaround to make this possible.

Also, I don't have experience with a lot of i18n libraries, do you know any that allow this kind of behavior?

i18next does this: https://www.i18next.com/translation-function/objects-and-arrays

I don't have much experience with i18n, but iterating over logical groups makes a lot of sense and it's the first thing I looked for in the library. I implemented svelte-i18n after I was already iterating over arrays of objects, not doing so feels like regression.

@kaisermann kaisermann self-assigned this Aug 6, 2020
@kaisermann kaisermann added enhancement New feature or request and removed pending clarification Further information is requested labels Aug 6, 2020
@Jerome1337
Copy link
Contributor

I tried to do the same but we cannot loop over these JSON arrays, here is the workaround I did:

"description": {
  "0": "...",
  "1": "...",
  "2": "...",
  "3": "..."
}
{#each new Array(4) as _, index}
  <p>{$t(`description.${index}`)}</p>
{/each}

This is working but it would be nice to be able to loop over JSON array

@kaisermann
Copy link
Owner

kaisermann commented Nov 5, 2020

Fixed in 3.2.0 🎉

@blynx
Copy link

blynx commented Nov 8, 2020

This seems to have broken my translations - I like flat data (🤷‍♂️) and have a dictionairy like so:

{
    "something": "Something Works Fine",
    "something.broken": "Something Does Not Work Fine"
}

The former gets translated, the latter not anymore with the "not found" warning [svelte-i18n] The message "my.message" was not found in "de", "en".
I wonder if this is intended or if there could/should be a fallback to retrieve the dot separated keys? This is not a major issue for me since I can just switch to another separator - but I was wondering if you are aware of this?

cheers, and thanks for the library! :)

@kaisermann
Copy link
Owner

Hey @blynx 👋 Sorry, my bad! Fixed in 3.2.5 🎉

@blynx
Copy link

blynx commented Nov 8, 2020

wow, that was quick! Thank you! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
5 participants