Skip to content

Interpolation fails with arrays (TypeError) and doesn't work with objects in defaultValue #1829

@martinkustl

Description

@martinkustl

🐛 Bug Report

When using t() function with an array as defaultValue that contains interpolation placeholders, the following error occurs:

TypeError: val.replace is not a function
    at regexSafe (i18next.js?v=6f6c3bbd:1068:30)
    at Object.safeValue (i18next.js?v=6f6c3bbd:1164:76)
    at i18next.js?v=6f6c3bbd:1187:32
    at Array.forEach (<anonymous>)

Additionally, when using an object as defaultValue with interpolation, the interpolation does not work as expected.

To Reproduce

Using array with interpolation in defaultValue:

t("missingKeyHere", {
      defaultValue: ["Tickets {{value}}"],
      value: 10,
      returnObjects: true,
    })

Using object with interpolation in defaultValue:

t("missingKeyHere", {
      defaultValue: { ticket: "Tickets {{value}}" },
      value: 10,
      returnObjects: true,
    })

Expected behavior

Array interpolation in defaultValue should work the same way as when translations are provided via translation JSON files.
Both array and object interpolation should properly replace {{value}} with the provided value (10 in this case)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions