javascript - React i18n,如何插入字符串(即不使用零件)?
问题描述
我的 React 组件中有这个功能:
const fireError = (n) => {
alert(`The value should go over ${n}% to be accepted.`);
}
在我的翻译 JSON 中,我有这个:
{ "percentage": "The value should go over {{percentage}} to be accepted" }
我怎样才能使这项工作?当然我不能使用 Trans 组件。
解决方案
您可以i18n
直接使用该服务:
// import it
import i18n from "i18next";
// in your React component
const fireError = (n) => {
alert(i18n.t('percentage', { percentage: n }));
}
请记住,在调用此函数之前必须初始化库 -i18n.init()
必须已经被调用。
推荐阅读
- python - python如何让timeit识别定义的输入
- python - python中读取网页的方法及区别
- docker - 将 windows 目录共享到 Linux docker 容器
- python - 计算平均分数并返回一个字符串
- node.js - 节点js中的异步等待,for循环内有readline
- node.js - Cheerio 中的 get() 函数有什么作用?
- dart - 将浮动操作按钮锚定到卡片
- sql-server - 遍历表的每一行,对该行进行计算,插入临时表并将临时表用于下一行
- node.js - 尝试在本地开发环境中使用 express 运行 next.js 时引发错误
- .htaccess - 仅在主页上将 http 重定向到 https