fix(i18n): address Monitor and CLI review feedback

This commit is contained in:
VAIO73
2026-10-22 04:49:16 +00:00
parent 61168b9abd
commit dbcc4de6e1
11 changed files with 121 additions and 411 deletions
+12 -10
View File
@@ -1,17 +1,19 @@
import enMessages from "../messages/en/common.json"
import skMessages from "../messages/sk/common.json"
import { detectBrowserLanguage, LANGUAGE_STORAGE_KEY } from "./i18n/languages"
import { MESSAGE_CATALOG } from "./i18n/catalogs"
import { detectBrowserLanguage, isSupportedLanguage, LANGUAGE_STORAGE_KEY } from "./i18n/languages"
type Failure = keyof typeof enMessages.apiErrors
type Failure = keyof (typeof import("../messages/en/common.json"))["apiErrors"]
// fetchApi also runs outside React hooks. Read the provider's current document
// language, with its stored/browser preference as a fallback.
export function apiFailure(key: Failure, params: Record<string, string | number> = {}): string {
let language = typeof document === "undefined" ? "" : document.documentElement?.lang
if (!language && typeof window !== "undefined") {
try { language = window.localStorage.getItem(LANGUAGE_STORAGE_KEY) || "" } catch { /* private browsing */ }
let preference = typeof document === "undefined" ? "" : document.documentElement?.lang
if (!isSupportedLanguage(preference) && typeof window !== "undefined") {
try { preference = window.localStorage.getItem(LANGUAGE_STORAGE_KEY) || "" } catch { /* private browsing */ }
}
language ||= detectBrowserLanguage()
const messages = language === "sk" ? skMessages.apiErrors : enMessages.apiErrors
return messages[key].replace(/\{(\w+)\}/g, (match, name) => params[name] === undefined ? match : String(params[name]))
const language = isSupportedLanguage(preference) ? preference : detectBrowserLanguage()
const messages = MESSAGE_CATALOG[language].apiErrors
const localized = messages && typeof messages === "object" ? (messages as Record<string, unknown>)[key] : undefined
const fallback = (MESSAGE_CATALOG.en.apiErrors as Record<Failure, string>)[key]
const template = typeof localized === "string" ? localized : fallback
return template.replace(/\{(\w+)\}/g, (match, name) => params[name] === undefined ? match : String(params[name]))
}