From 712fbb53c75579e2ba9d6f540ce1a89e21509111 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 8 Dec 2022 15:54:01 -0800 Subject: [PATCH] Fix rate unit displays --- next-i18next.config.js | 15 ++++++++------- public/locales/en/common.json | 6 ++++-- src/widgets/qbittorrent/component.jsx | 4 ++-- src/widgets/speedtest/component.jsx | 4 ++-- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/next-i18next.config.js b/next-i18next.config.js index ee6eaaa1d..54aeebb4a 100644 --- a/next-i18next.config.js +++ b/next-i18next.config.js @@ -98,20 +98,21 @@ module.exports = { ); i18next.services.formatter.add("rate", (value, lng, options) => { - if (value === 0) return "0 Bps"; - const bits = options.bits ? value : value / 8; - const k = 1024; + const k = options.binary ? 1024 : 1000; + const sizes = options.bits ? (options.binary ? BIBIT_UNITS : BIT_UNITS) : (options.binary ? BIBYTE_UNITS : BYTE_UNITS); + + if (value === 0) return `0 ${sizes[0]}/s`; + const dm = options.decimals ? options.decimals : 0; - const sizes = ["Bps", "KiBps", "MiBps", "GiBps", "TiBps", "PiBps", "EiBps", "ZiBps", "YiBps"]; - const i = Math.floor(Math.log(bits) / Math.log(k)); + const i = options.binary ? 2 : Math.floor(Math.log(value) / Math.log(k)); const formatted = new Intl.NumberFormat(lng, { maximumFractionDigits: dm, minimumFractionDigits: dm }).format( - parseFloat(bits / k ** i) + parseFloat(value / k ** i) ); - return `${formatted} ${sizes[i]}`; + return `${formatted} ${sizes[i]}/s`; }); i18next.services.formatter.add("percent", (value, lng, options) => diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 80ba5357e..96d531e10 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -3,9 +3,11 @@ "bytes": "{{value, bytes}}", "bits": "{{value, bytes(bits: true)}}", "bbytes": "{{value, bytes(binary: true)}}", - "bbits": "{{value, bytes(bits: true, binary: true)}}", - "byterate": "{{value, rate}}", + "bbits": "{{value, bytes(bits: true; binary: true)}}", + "byterate": "{{value, rate(bits: false)}}", + "bibyterate": "{{value, rate(bits: false; binary: true)}}", "bitrate": "{{value, rate(bits: true)}}", + "bibitrate": "{{value, rate(bits: true; binary: true)}}", "percent": "{{value, percent}}", "number": "{{value, number}}", "ms": "{{value, number}}" diff --git a/src/widgets/qbittorrent/component.jsx b/src/widgets/qbittorrent/component.jsx index 3abc933da..d3836a6ba 100644 --- a/src/widgets/qbittorrent/component.jsx +++ b/src/widgets/qbittorrent/component.jsx @@ -44,9 +44,9 @@ export default function Component({ service }) { return ( - + - + ); } diff --git a/src/widgets/speedtest/component.jsx b/src/widgets/speedtest/component.jsx index 3067dbf3d..7d722fafe 100644 --- a/src/widgets/speedtest/component.jsx +++ b/src/widgets/speedtest/component.jsx @@ -29,9 +29,9 @@ export default function Component({ service }) { - +