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

Support arrays for dynamic rendering and component reuse #64

Closed
fabifrank opened this issue Nov 13, 2021 · 4 comments
Closed

Support arrays for dynamic rendering and component reuse #64

fabifrank opened this issue Nov 13, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@fabifrank
Copy link

fabifrank commented Nov 13, 2021

Is your feature request related to a problem? Please describe.
Given a component Section that is rendering 2 paragraphs:

const Section = () => (
  <>
    <h2>{t("title")}<h2>
    {t("p1") !== "" && <p>{t("p1")}</p>}
    {t("p2") !== "" && <p>{t("p2")}</p>}
  </>
)

that could be used on multiple pages and layouts, one must check for endless paragraphs if they exist in the locale and render them. So if "contact" page has 2 paragraphs, this is fine. But what if "imprint" had 3 or more?

Describe the solution you'd like

"title": "imprint",
"paragraphs": [
  "p1...",
  "p2...",
  "p3..."
]

and in the component:

const Section = () => (
  <>
    <h2>{t("title")}<h2>
    {t("paragraphs").map(text => (
      <p>{text}</p>
    ))}
  </>
)

Describe alternatives you've considered
The only alternative I see now is the one in the problem description.

Additional context
The code snippets above show all relevant parts.

@fabifrank fabifrank added the enhancement New feature or request label Nov 13, 2021
@amannn
Copy link
Owner

amannn commented Nov 15, 2021

Hi @fabifrank and thank you for your question.

In your case I'd suggest to use rich text formatting to get a flexible amount of tags that can be defined in the messages. By doing that you can also potentially use other tags in your content.

Does that help?

@amannn
Copy link
Owner

amannn commented Nov 24, 2021

Closing due to inactivity … Feel free to reopen this with additional details if there are still open questions!

@amannn amannn closed this as completed Nov 24, 2021
@fabifrank
Copy link
Author

fabifrank commented Nov 26, 2021

Thanks @amannn for your answer. I was out of that project for some days, sorry for the delay.
I tried your method with rich text, but it states t.rich is not a function. Currently using const t = useTranslations(namespace).

Is there something I am missing?

@amannn
Copy link
Owner

amannn commented Nov 26, 2021

Are you maybe on an old version? t.rich was added in 2.0.0.

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
Development

No branches or pull requests

2 participants