src/eric7/Globals/__init__.py

branch
eric7-maintenance
changeset 10148
31ba20629af4
parent 10079
0222a480e93d
parent 10109
193b15c75155
child 10460
3b34efa2857c
--- a/src/eric7/Globals/__init__.py	Sun Jul 02 17:40:17 2023 +0200
+++ b/src/eric7/Globals/__init__.py	Tue Aug 01 09:59:45 2023 +0200
@@ -184,29 +184,57 @@
     return QByteArray(txt)
 
 
-def dataString(size):
+def dataString(size, loc=None):
     """
     Module function to generate a formatted size string.
 
     @param size size to be formatted
     @type int
+    @param loc locale to be used for localized size strings (defaults to None)
+    @type QLocale (optional)
     @return formatted data string
     @rtype str
     """
-    if size < 1024:
-        return QCoreApplication.translate("Globals", "{0:4.2f} Bytes").format(size)
-    elif size < 1024 * 1024:
-        size /= 1024
-        return QCoreApplication.translate("Globals", "{0:4.2f} KiB").format(size)
-    elif size < 1024 * 1024 * 1024:
-        size /= 1024 * 1024
-        return QCoreApplication.translate("Globals", "{0:4.2f} MiB").format(size)
-    elif size < 1024 * 1024 * 1024 * 1024:
-        size /= 1024 * 1024 * 1024
-        return QCoreApplication.translate("Globals", "{0:4.2f} GiB").format(size)
+    if loc is None:
+        if size < 1024:
+            return QCoreApplication.translate("Globals", "{0:4.2f} Bytes").format(size)
+        elif size < 1024 * 1024:
+            size /= 1024
+            return QCoreApplication.translate("Globals", "{0:4.2f} KiB").format(size)
+        elif size < 1024 * 1024 * 1024:
+            size /= 1024 * 1024
+            return QCoreApplication.translate("Globals", "{0:4.2f} MiB").format(size)
+        elif size < 1024 * 1024 * 1024 * 1024:
+            size /= 1024 * 1024 * 1024
+            return QCoreApplication.translate("Globals", "{0:4.2f} GiB").format(size)
+        else:
+            size /= 1024 * 1024 * 1024 * 1024
+            return QCoreApplication.translate("Globals", "{0:4.2f} TiB").format(size)
     else:
-        size /= 1024 * 1024 * 1024 * 1024
-        return QCoreApplication.translate("Globals", "{0:4.2f} TiB").format(size)
+        if size < 1024:
+            return QCoreApplication.translate("Globals", "{0} Bytes").format(
+                loc.toString(size, "f", 2)
+            )
+        elif size < 1024 * 1024:
+            size /= 1024
+            return QCoreApplication.translate("Globals", "{0} KiB").format(
+                loc.toString(size, "f", 2)
+            )
+        elif size < 1024 * 1024 * 1024:
+            size /= 1024 * 1024
+            return QCoreApplication.translate("Globals", "{0} MiB").format(
+                loc.toString(size, "f", 2)
+            )
+        elif size < 1024 * 1024 * 1024 * 1024:
+            size /= 1024 * 1024 * 1024
+            return QCoreApplication.translate("Globals", "{0} GiB").format(
+                loc.toString(size, "f", 2)
+            )
+        else:
+            size /= 1024 * 1024 * 1024 * 1024
+            return QCoreApplication.translate("Globals", "{0} TiB").format(
+                loc.toString(size, "f", 2)
+            )
 
 
 ###############################################################################

eric ide

mercurial