admin管理员组文章数量:1417070
My React app uses next-i18next
package. I'd like to put some React ponent inside my interpolation:
import React from 'react';
import { useTranslation } from 'next-i18next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
export default function Review({ text, author }) {
const { t } = useTranslation('reviews');
return (
<article>
<p>{text}</p>
<footer>
{t('footer', { author: <a href={author.url}>{author.name}</a> })}
</footer>
</article>
);
}
export const getStaticProps = async ({ locale }) => ({
props: {
...await serverSideTranslations(locale, ['reviews']),
}
});
And reviews.json
:
{
"footer": "By {{author}}, all rights reserved"
}
Trying to use JSX element to fill the interpolation doesn't work as expected. The result I'm getting is:
By [object Object], all rights reserved
I also tried with unescaped interpolation with By {{- author}}, all rights reserved
, but it ends up being the same result.
Can I use JSX element to fill my interpolation?
My React app uses next-i18next
package. I'd like to put some React ponent inside my interpolation:
import React from 'react';
import { useTranslation } from 'next-i18next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
export default function Review({ text, author }) {
const { t } = useTranslation('reviews');
return (
<article>
<p>{text}</p>
<footer>
{t('footer', { author: <a href={author.url}>{author.name}</a> })}
</footer>
</article>
);
}
export const getStaticProps = async ({ locale }) => ({
props: {
...await serverSideTranslations(locale, ['reviews']),
}
});
And reviews.json
:
{
"footer": "By {{author}}, all rights reserved"
}
Trying to use JSX element to fill the interpolation doesn't work as expected. The result I'm getting is:
By [object Object], all rights reserved
I also tried with unescaped interpolation with By {{- author}}, all rights reserved
, but it ends up being the same result.
Can I use JSX element to fill my interpolation?
Share Improve this question edited Mar 27, 2021 at 11:14 Robo Robok asked Mar 22, 2021 at 22:57 Robo RobokRobo Robok 22.9k20 gold badges83 silver badges144 bronze badges1 Answer
Reset to default 6You can't use t
function in order to inject jsx
.
There is a special Trans
ponent for this, you should use it.
import React from 'react';
import { useTranslation, Trans } from 'next-i18next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
export default function Review({ text, author }) {
const { t } = useTranslation('reviews');
return (
<article>
<p>{text}</p>
<footer>
<Trans
i18nKey="footer"
t={t}
values={{ author }}
ponents={{ authorLink: <a href={author.url}>placeholder</a> }}
/>
</footer>
</article>
);
}
export const getStaticProps = async ({ locale }) => ({
props: {
...(await serverSideTranslations(locale, ['reviews'])),
},
});
When in your translation file you will have:
{
"footer": "My footer text <authorLink>{{author.name}}</authorLink>"
}
本文标签: javascriptReact components in i18next interpolation display as object ObjectStack Overflow
版权声明:本文标题:javascript - React components in i18next interpolation display as [object Object] - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745258331a2650236.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论