2026-10-22 04:49:16 +00:00
|
|
|
import { MESSAGE_CATALOG } from "./i18n/catalogs"
|
|
|
|
|
import { detectBrowserLanguage, isSupportedLanguage, LANGUAGE_STORAGE_KEY } from "./i18n/languages"
|
2026-10-21 09:43:40 +00:00
|
|
|
|
2026-10-22 04:49:16 +00:00
|
|
|
type Failure = keyof (typeof import("../messages/en/common.json"))["apiErrors"]
|
2026-10-21 09:43:40 +00:00
|
|
|
|
|
|
|
|
// 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 {
|
2026-10-22 04:49:16 +00:00
|
|
|
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 */ }
|
2026-10-21 09:43:40 +00:00
|
|
|
}
|
2026-10-22 04:49:16 +00:00
|
|
|
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]))
|
2026-10-21 09:43:40 +00:00
|
|
|
}
|