reactjs - 如何使用 react i18next 库格式化货币
问题描述
我有一个 create-react-app,我需要为其添加 i18n 支持。我打算使用https://github.com/i18next但在文档中我找不到如何格式化货币。
有人如何使用 react i18next 库格式化货币吗?
非常感谢你的帮助
解决方案
您可以使用Intl.NumberFormat
以格式化货币。
为了将它与i18next
我使用的格式选项集成。
i18next.init({
lng: 'en',
debug: false,
resources: {
en: {
translation: {
"key": "{{value, price, EUR}}"
}
}
},
interpolation: {
format: (value, rawFormat, lng) => {
const [format, ...additionalValues] = rawFormat.split(',').map((v) => v.trim());
switch (format) {
case 'uppercase':
return value.toUpperCase();
case 'price':
return Intl.NumberFormat(lng, {
style: 'currency',
currency: additionalValues[0]
}).format(value);
}
}
}
}).then(function(t) {
// initialized and ready to go!
document.getElementById('output').innerHTML = t('key', {
value: 1000.01
});
});
<script src="https://unpkg.com/i18next/dist/umd/i18next.min.js"></script>
<div id="output"></div>
推荐阅读
- reactjs - 如何调试无法在 react-admin 应用程序中获取
- javascript - 循环中的 JS AddEventListener(适用于所有复选框)
- laravel - 将 Laravel Voyager 中所做的更改保存到项目中
- c - 没有“\n”,服务器端 printf() 不打印(使用套接字的 TCP 客户端-服务器)
- java - JSch 发送命令到多个 ip
- ios - WKWebView 委托没有响应委托方法调用
- sql - 当我在 SSMS 中使用 RIGHT 函数时,虽然没有多余的空间,但它给了我空白的结果
- django - 如果我们想转到上一步,如何禁用字段验证?
- yubico - Yubikey 5 NFC:获得“打包”证明声明
- amazon-s3 - 如何向经过身份验证的网站用户授予对 s3 文件的访问权限