使用 i18n 打印此文本的最佳方式是什么?

发布于 2025-01-13 17:57:54 字数 1910 浏览 3 评论 0原文

基本上我想要显示的是 I am Python, Javascript and Flutter Developer. 字符串。但它看起来是这样的:

在此处输入图像描述

使用以下组件进行反应:

<Typography gutterBottom variant="h6">
  I am <HL>Python</HL>, <HL>Javascript</HL> and <HL>Flutter</HL> developer.
</Typography>

使用 HL 组件,我基本上将一些样式应用于文本,我将如何将其翻译为下一个翻译?我最好的尝试是:

<Typography gutterBottom variant="h6">
  {t("introduction2", {
    langs: [
      <HL>Python</HL>,
      <HL>JavaScript</HL>,
      <HL>Flutter</HL>,
    ]
  })}
</Typography>

使用:

{
  ...
  "introduction2": "I am {{langs[0]}}, {{langs[1]}} and {{langs[2]}} developer.",
  ...
}

也尝试过:

{
  ...
  "introduction2": "I am {{langs.0}}, {{langs.1}} and {{langs.2}} developer.",
  ...
}

并且还在 HL 组件上尝试使用 renderToString 但它的行为与我使用 Next.JS 渲染时的预期不同页面,它搞乱了 mui 提供的主题设置。

编辑

通过使用@adrai提到的Trans组件解决了这个问题,但解决方案有点不同,所以这里是最后一种形式

import Trans from "next-translate/Trans";

...

<Typography gutterBottom variant="h6">
  <Trans i18nKey="common:introduction2" components={[<HL />]}>
    I am <HL>Python</HL>, <HL>Javascript</HL> and <HL>Flutter</HL> developer.
  </Trans>
</Typography>

{
  ...
  "introduction2": "I am <0>Python</0>, <0>Javascript</0> and <0>Flutter</0> developer.",
  ...
}

Basically what I am trying to display is I am Python, Javascript and Flutter developer. string. But it looks this way:

enter image description here

With the following component in react:

<Typography gutterBottom variant="h6">
  I am <HL>Python</HL>, <HL>Javascript</HL> and <HL>Flutter</HL> developer.
</Typography>

With HL component I basically am applying a few stylings to texts, how would I go translating that with next-translate? My best try is:

<Typography gutterBottom variant="h6">
  {t("introduction2", {
    langs: [
      <HL>Python</HL>,
      <HL>JavaScript</HL>,
      <HL>Flutter</HL>,
    ]
  })}
</Typography>

With:

{
  ...
  "introduction2": "I am {{langs[0]}}, {{langs[1]}} and {{langs[2]}} developer.",
  ...
}

Also tried with:

{
  ...
  "introduction2": "I am {{langs.0}}, {{langs.1}} and {{langs.2}} developer.",
  ...
}

And also tried with renderToString on HL components but it does not behave how you may expect as I use Next.JS to render the page, it messes up theme settings provided by mui.

EDIT

Solved it by using Trans component mentioned by @adrai, but the solution differed a bit so here is the last form:

import Trans from "next-translate/Trans";

...

<Typography gutterBottom variant="h6">
  <Trans i18nKey="common:introduction2" components={[<HL />]}>
    I am <HL>Python</HL>, <HL>Javascript</HL> and <HL>Flutter</HL> developer.
  </Trans>
</Typography>

With:

{
  ...
  "introduction2": "I am <0>Python</0>, <0>Javascript</0> and <0>Flutter</0> developer.",
  ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太傻旳人生 2025-01-20 17:57:54

使用 Trans 组件:https://react.i18next.com/latest/trans-component

<Typography gutterBottom variant="h6">
  <Trans i18nKey="myKey">I am <HL>Python</HL>, <HL>Javascript</HL> and <HL>Flutter</HL> developer.</Trans>
</Typography>

并在翻译字符串中使用 <1>Python标签…

"myKey": "I am <1>Python</1>, <3>Javascript</3> and <5>Flutter</5> developer."

Use the Trans Component: https://react.i18next.com/latest/trans-component

<Typography gutterBottom variant="h6">
  <Trans i18nKey="myKey">I am <HL>Python</HL>, <HL>Javascript</HL> and <HL>Flutter</HL> developer.</Trans>
</Typography>

And in the translation string use the <1>Python</1> tags…

"myKey": "I am <1>Python</1>, <3>Javascript</3> and <5>Flutter</5> developer."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文