Browse Source

Localize formatting helpers

Matt Rubens 10 months ago
parent
commit
76b37b11ca

+ 7 - 1
webview-ui/src/i18n/locales/ca/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "mil",
+		"million_suffix": "M",
+		"billion_suffix": "MM"
+	}
+}

+ 7 - 1
webview-ui/src/i18n/locales/de/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "Tsd.",
+		"million_suffix": "Mio.",
+		"billion_suffix": "Mrd."
+	}
+}

+ 7 - 1
webview-ui/src/i18n/locales/en/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "k",
+		"million_suffix": "m",
+		"billion_suffix": "b"
+	}
+}

+ 7 - 1
webview-ui/src/i18n/locales/es/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "k",
+		"million_suffix": "M",
+		"billion_suffix": "MM"
+	}
+}

+ 7 - 1
webview-ui/src/i18n/locales/fr/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "k",
+		"million_suffix": "M",
+		"billion_suffix": "Md"
+	}
+}

+ 7 - 1
webview-ui/src/i18n/locales/hi/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "ह",
+		"million_suffix": "लाख",
+		"billion_suffix": "अरब"
+	}
+}

+ 7 - 1
webview-ui/src/i18n/locales/it/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "k",
+		"million_suffix": "Mln",
+		"billion_suffix": "Mrd"
+	}
+}

+ 7 - 1
webview-ui/src/i18n/locales/ja/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "k",
+		"million_suffix": "M",
+		"billion_suffix": "B"
+	}
+}

+ 7 - 1
webview-ui/src/i18n/locales/ko/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "천",
+		"million_suffix": "백만",
+		"billion_suffix": "십억"
+	}
+}

+ 7 - 1
webview-ui/src/i18n/locales/pl/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "tys.",
+		"million_suffix": "mln",
+		"billion_suffix": "mld"
+	}
+}

+ 7 - 1
webview-ui/src/i18n/locales/pt-BR/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "mil",
+		"million_suffix": "mi",
+		"billion_suffix": "bi"
+	}
+}

+ 7 - 1
webview-ui/src/i18n/locales/tr/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "B",
+		"million_suffix": "Mn",
+		"billion_suffix": "Mr"
+	}
+}

+ 7 - 1
webview-ui/src/i18n/locales/vi/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "k",
+		"million_suffix": "tr",
+		"billion_suffix": "tỷ"
+	}
+}

+ 7 - 1
webview-ui/src/i18n/locales/zh-CN/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "千",
+		"million_suffix": "百万",
+		"billion_suffix": "十亿"
+	}
+}

+ 7 - 1
webview-ui/src/i18n/locales/zh-TW/common.json

@@ -1 +1,7 @@
-{}
+{
+	"number_format": {
+		"thousand_suffix": "千",
+		"million_suffix": "百萬",
+		"billion_suffix": "十億"
+	}
+}

+ 22 - 14
webview-ui/src/utils/format.ts

@@ -1,27 +1,35 @@
+import i18next from "i18next"
+
 export function formatLargeNumber(num: number): string {
 	if (num >= 1e9) {
-		return (num / 1e9).toFixed(1) + "b"
+		return (num / 1e9).toFixed(1) + i18next.t("common:number_format.billion_suffix")
 	}
 	if (num >= 1e6) {
-		return (num / 1e6).toFixed(1) + "m"
+		return (num / 1e6).toFixed(1) + i18next.t("common:number_format.million_suffix")
 	}
 	if (num >= 1e3) {
-		return (num / 1e3).toFixed(1) + "k"
+		return (num / 1e3).toFixed(1) + i18next.t("common:number_format.thousand_suffix")
 	}
 	return num.toString()
 }
 
 export const formatDate = (timestamp: number) => {
 	const date = new Date(timestamp)
-	return date
-		.toLocaleString("en-US", {
-			month: "long",
-			day: "numeric",
-			hour: "numeric",
-			minute: "2-digit",
-			hour12: true,
-		})
-		.replace(", ", " ")
-		.replace(" at", ",")
-		.toUpperCase()
+	const locale = i18next.language || "en"
+
+	// Get date format style from translations or use default transformations
+	const dateStr = date.toLocaleString(locale, {
+		month: "long",
+		day: "numeric",
+		hour: "numeric",
+		minute: "2-digit",
+		hour12: true,
+	})
+
+	// Apply transformations based on locale or use default
+	if (locale === "en") {
+		return dateStr.replace(", ", " ").replace(" at", ",").toUpperCase()
+	}
+
+	return dateStr.toUpperCase()
 }