question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Correct way to use Trans component and how to pass variables; incomplete docs

See original GitHub issue

i18next 11.3.2 react-i18next 7.6.1

By reading the docs, it is unclear to me (from the simple example with no context located at https://react.i18next.com/components/trans-component; the linked source has nothing to do with the provided code snippet - where does Arthur value comes from?) as to how to pass variable values when using Trans component. For example:

Foo.tsx (snippet for clarity):

<Trans i18nKey="objects.errors.unknownType.content">
    Object with ID <strong>{this.props.objectId}</strong> is of unknown type.
    Go back to <a
        href="/objects"
        onClick={this.handleNavigationHrefClick}
    >
        Objects
    </a> route.
</Trans>

en.json (snippet for clarity):

"objects": {
    "errors": {
        "unknownType": {
            "title": "Unknown Object Type",
            "content": "Object with ID <1>{{id}}</1> is of unkown type. Go back to <3>Objects</3> route."
        }
    }
}

I get:

react-i18next: the passed in value is invalid - seems you passed in a variable like {number} - please pass in variables for interpolation as full objects like {{number}}. __ACTUAL_VALUE_HERE__

I also tried (by guessing) tOptions={{id: this.props.objectId}} with no luck.

What is the correct way to use Trans component and pass variable values to it?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

14reactions
vladeckcommented, May 29, 2018

Got it working. I’ll leave working code here (so you can compare), for future references:

Foo.tsx (snippet for clarity):

const id = this.props.objectId;

<Trans i18nKey="objects.errors.unknownType.content">
    Object with ID <strong>{{id}}</strong> is of unknown type.
    Go back to <a
        href="/objects"
        onClick={this.handleNavigationHrefClick}
    >
        Objects
    </a> route.
</Trans>

en.json (snippet for clarity):

"objects": {
    "errors": {
        "unknownType": {
            "title": "Unknown Object Type",
            "content": "Object with ID <1><0>{{id}}</0></1> is of unknown type. Go back to <3>Objects</3> route."
        }
    }
}
12reactions
geraman21commented, May 18, 2020

@jamuhl but then doesn’t it feel like I am just writing redundant text there ? My i18nKey already points at a translation string and I don’t see the reason for rewriting the English version of it. This point can be maybe added to the Documentation, I am sure many people will find it helpfull.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trans Component - react-i18next documentation
You can pass in variables to get interpolated into the translation string by using objects containing those key:values.
Read more >
Google Translate Community
Welcome to the Google Translate Help Community. Featured posts. View all featured posts · Please share your feedback on Google Translate Material You...
Read more >
Use react-i18next Trans component with parameters and ...
Solved as in github.com/i18next/react-i18next/issues/… even though I don't know if that is the correct way to do that.
Read more >
Rails Internationalization (I18n) API - Ruby on Rails Guides
To create proper abstraction, the I18n gem ships with a feature called variable interpolation that allows you to use variables in translation definitions...
Read more >
Insights - Using Oracle Digital Assistant
The report's metrics break this total down by the number of complete, incomplete, and in-progress conversations. In addition, this report tells ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found