amannn / next-intl

🌐 Internationalization (i18n) for Next.js
https://next-intl-docs.vercel.app
MIT License
2.46k stars 223 forks source link

Rich Text with Placeholders in t.rich Not Rendering Correctly - Displays Key Instead of Content #1409

Closed RedEagle-dh closed 1 week ago

RedEagle-dh commented 1 week ago

Description

When using t.rich with translations that contain placeholders like <strong1>, the expected behavior is that the content between these placeholders is rendered according to the mapping provided in the t.rich call. However, due to the bug, the output is only the translation key (e.g., IndexPage.subtitle) rather than the processed translation content.

Verifications

Mandatory reproduction URL

https://github.com/RedEagle-dh/next-intl-bug-placeholders-not-rendered

Reproduction description

  1. Clone the repository:

    git clone https://github.com/redeagle-dh/next-intl-bug-placeholders-not-rendered.git
    cd next-intl-bug-placeholders-not-rendered
  2. Install dependencies: Make sure you have Node.js installed, then run:

    yarn
  3. Start the development server:

    yarn dev
  4. Open the application in your browser: Navigate to http://localhost:3000.

  5. Trigger the bug:

    • You should already see the output on the main page.
    • The translation key IndexPage.subtitle will be displayed instead of the expected text content with the rich text formatting applied.

Expected behaviour

The t.rich function should correctly parse the placeholders in the translation (e.g., <strong1>...</strong1>) and render the content according to the provided mappings. For example, if the translation in de.json is:

{
  "IndexPage": {
    "title": "Hello!",
    "subtitle": "<strong1>Welcome</strong1> to Next.js!"
  }
}

The expected output should display Welcome and to Next.js! wrapped in the appropriate React components as defined in the t.rich call.

RedEagle-dh commented 1 week ago

I found the root cause of the issue. It was due to an error on my end where I used the wrong name in the function mapping for t.rich. After correcting it, the rich text rendering works as expected.

Apologies for the confusion, and thank you for your time and support!

I’m closing this issue now.