public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Noel Ullreich <n.ullreich@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH proxmox-widget-toolkit v2 1/1] fix 4551: ui: translate byte unit in `format_size`
Date: Thu,  6 Apr 2023 13:38:06 +0200	[thread overview]
Message-ID: <20230406113806.52027-3-n.ullreich@proxmox.com> (raw)
In-Reply-To: <20230406113806.52027-1-n.ullreich@proxmox.com>

Some languages translate byte units like 'GiB' or write them in their
own script.

By `gettext`ing the units in the `format_size` function, we can
translate the units for (almost) all of the web interface.

Signed-off-by: Noel Ullreich <n.ullreich@proxmox.com>
---
 src/Utils.js | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/Utils.js b/src/Utils.js
index 5e1a6f3..529e552 100644
--- a/src/Utils.js
+++ b/src/Utils.js
@@ -688,21 +688,23 @@ utilities: {
     },
 
     format_size: function(size, useSI) {
-	let units = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
+	let unitsSI = [gettext('B'), gettext('KB'), gettext('MB'), gettext('GB'),
+	    gettext('TB'), gettext('PB'), gettext('EB'), gettext('ZB'), gettext('YB')];
+	let unitsIEC = [gettext('B'), gettext('KiB'), gettext('MiB'), gettext('GiB'),
+	    gettext('TiB'), gettext('PiB'), gettext('EiB'), gettext('ZiB'), gettext('YiB')];
 	let order = 0;
+	let commaDigits = 2;
 	const baseValue = useSI ? 1000 : 1024;
-	while (size >= baseValue && order < units.length) {
+	while (size >= baseValue && order < unitsSI.length) {
 	    size = size / baseValue;
 	    order++;
 	}
 
-	let unit = units[order], commaDigits = 2;
+	let unit = useSI ? unitsSI[order] : unitsIEC[order];
 	if (order === 0) {
 	    commaDigits = 0;
-	} else if (!useSI) {
-	    unit += 'i';
 	}
-	return `${size.toFixed(commaDigits)} ${unit}B`;
+	return `${size.toFixed(commaDigits)} ${unit}`;
     },
 
     SizeUnits: {
-- 
2.30.2





  parent reply	other threads:[~2023-04-06 11:38 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-06 11:38 [pve-devel] [PATCH pve-manager, proxmox-widget-toolkit v2 0/2] fix 4551: ui: translate byte units in web interface Noel Ullreich
2023-04-06 11:38 ` [pve-devel] [PATCH proxmox-widget-toolkit v2 1/1] fix #4551: ui: use gettext on hardcoded byte units Noel Ullreich
2023-06-01 14:14   ` Thomas Lamprecht
2023-04-06 11:38 ` Noel Ullreich [this message]
2023-06-01 14:16   ` [pve-devel] applied: [PATCH proxmox-widget-toolkit v2 1/1] fix 4551: ui: translate byte unit in `format_size` Thomas Lamprecht
2023-05-22  9:08 ` [pve-devel] [PATCH pve-manager, proxmox-widget-toolkit v2 0/2] fix 4551: ui: translate byte units in web interface Noel Ullreich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230406113806.52027-3-n.ullreich@proxmox.com \
    --to=n.ullreich@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal