yassinedoghri / astro-i18next

An astro integration of i18next + some utility components to help you translate your astro websites!
https://astro-i18next.yassinedoghri.com
MIT License
473 stars 33 forks source link

Array of objects #153

Open djmtype opened 1 year ago

djmtype commented 1 year ago

This probably isn't a bug, but rather my lack of understanding. I have a nested array of objects. However, {t("data.products[0].slug")} doesn't even render. It returns as a missingKey. What am I doing wrong?

{
"pageTitle": "Products",
"data": {
        "products": [
            {
                "id": "1",
                "status": "published",
                "sort": null,
                "slug": "my-url"
            },
            {…}
    ]
  }
}
djmtype commented 1 year ago

OH… {t('data.products.0.slug')}

djmtype commented 1 year ago

While this seems to work when mapping, VS Code doesn't seem to know its type, meaning I'm getting a warning that map does not exist on type object. Unsure if I'm implementing this correctly.

{t('data.products', { returnObjects: true }).map((product) => 
        (
            <p>{product.slug}</p>
        )
)}
kristianhnielsen commented 1 year ago

I used the Array constructor to call .map on non-array objects

Example:

{
    Array.prototype.map.call(
      t("data.products", { returnObjects: true }),
      (product) => <p>{product.slug}</p>
    )
  }