最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React i18n, how to interpolate to a string (i.e. without using <Trans> component)? - Stack Overflow

programmeradmin3浏览0评论

I have this function in my React ponent:

const fireError = (n) => {
   alert(`The value should go over ${n}% to be accepted.`);
}

in my translation JSON I have this:

{ "percentage": "The value should go over {{percentage}} to be accepted" }

How can I make this work? Of course I cannot use the Trans ponent.

I have this function in my React ponent:

const fireError = (n) => {
   alert(`The value should go over ${n}% to be accepted.`);
}

in my translation JSON I have this:

{ "percentage": "The value should go over {{percentage}} to be accepted" }

How can I make this work? Of course I cannot use the Trans ponent.

Share Improve this question asked Sep 20, 2021 at 16:52 nicknick 3,2876 gold badges43 silver badges85 bronze badges 1
  • use t('percentage'), as for how to setup react i18n, check documentation – windmaomao Commented Sep 20, 2021 at 16:57
Add a ment  | 

1 Answer 1

Reset to default 4

You can use the i18n service directly:

// import it
import i18n from "i18next";

// in your React ponent
const fireError = (n) => {
   alert(i18n.t('percentage', { percentage: n }));
}

Keep in mind that the library has to be initialized before calling this function - i18n.init() must have already been invoked.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论