【问题标题】:i18n translation make a variable in boldi18n 翻译以粗体显示变量
【发布时间】:2022-01-02 19:34:15
【问题描述】:

我正在尝试创建一个位于翻译字符串中间的变量,但我找不到解决方案。 我需要两种不同语言的所有翻译:“en”和“de”

我的代码如下所示:

import { useTranslation } from 'react-i18next'

const { t } = useTranslation('payment');

const companyName = 'Probando'

<Typography>{ t('setup with company name', <b>{companyName: companyName}</b>)</Typography>

我的翻译文件:

支付 en.json { "setup with company name": "Please contact {{ companyName }} to re-edit or re-send this invitation.", }

付款de.json: { "setup with company name": "Bitte wenden Sie sich an {{ companyName }} um diese Einladung nochmal zu bearbeiten oder neu zu erhalten." }

【问题讨论】:

    标签: javascript reactjs translation react-i18next


    【解决方案1】:

    如果要在翻译中使用标签,则需要使用Trans 组件。

    https://react.i18next.com/latest/trans-component

    import { Trans } from 'react-i18next'
    
    const { t } = useTranslation('payment');
    
    <Trans t={t} i18nKey="setup with company name">
      Please contact <b>{{ companyName }}</b> to re-edit or re-send this invitation.
    </Trans>
    

    然后是这样的翻译:

    // EN
    "setup with company name": "Please contact <1>{{companyName}}</1> to re-edit or re-send this invitation.",
    
    // DE
    "setup with company name": "Bitte wenden Sie sich an <1>{{ companyName }}</1> um diese Einladung nochmal zu bearbeiten oder neu zu erhalten."
    

    【讨论】:

    • 非常感谢!我现在的问题是如何处理我需要的其他翻译?因为那只会翻译成英文,但我也需要德文。谢谢
    • 就像我发布的示例一样,您使用 而不是 进行翻译。
    • PS: Am besten du verwendest ein Tool um die translations zu extrahieren。 Infos dazu findest du hier: i18next.com/how-to/extracting-translations. dann musst du nicht auswendig wissen welche Nummer () du hier verwenden musst.
    • 但只呈现英文版本,因为我们正在设置英文文本: 请联系 {{ companyName }} 重新编辑或重新发送此邀请。 对于德国人,我需要这样的东西吗?: Bitte wenden Sie sich an {{ companyName }} um diese Einladung nochmal zu Bearbeiten oder neu zu erhalten。 `
    • 不,它将用翻译文件中的文本替换Trans 组件内的内容。如果您将翻译设置为德语,它将使用德语翻译中定义的文本。组件内的文本只是默认翻译。
    猜你喜欢
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 2017-02-27
    • 1970-01-01
    • 2013-02-10
    相关资源
    最近更新 更多