import type { Locale } from "@/translations/config"; import ar from "@/translations/locales/ar.json"; import az from "@/translations/locales/az.json"; import bn from "@/translations/locales/bn.json"; import da from "@/translations/locales/da.json"; import de from "@/translations/locales/de.json"; import en from "@/translations/locales/en.json"; import es from "@/translations/locales/es.json"; import fa from "@/translations/locales/fa.json"; import fr from "@/translations/locales/fr.json"; import gu from "@/translations/locales/gu.json"; import ha from "@/translations/locales/ha.json"; import he from "@/translations/locales/he.json"; import hi from "@/translations/locales/hi.json"; import id from "@/translations/locales/id.json"; import ks from "@/translations/locales/ks.json"; import pt from "@/translations/locales/pt.json"; import ru from "@/translations/locales/ru.json"; import sw from "@/translations/locales/sw.json"; import tg from "@/translations/locales/tg.json"; import tr from "@/translations/locales/tr.json"; import ul from "@/translations/locales/ul.json"; import ur from "@/translations/locales/ur.json"; import uz from "@/translations/locales/uz.json"; import zh from "@/translations/locales/zh.json"; export const dictionaries = { ar, az, bn, da, de, en, es, fa, fr, gu, ha, he, hi, id, ks, pt, ru, sw, tg, tr, ul, ur, uz, zh, } as const; export type Dictionary = typeof dictionaries.en; function mergeWithEnglishFallback( fallback: Record, localized: Record, ): Record { const result = { ...fallback }; for (const [key, localizedValue] of Object.entries(localized)) { const fallbackValue = fallback[key]; if ( localizedValue && typeof localizedValue === "object" && !Array.isArray(localizedValue) && fallbackValue && typeof fallbackValue === "object" && !Array.isArray(fallbackValue) ) { result[key] = mergeWithEnglishFallback( fallbackValue as Record, localizedValue as Record, ); } else { result[key] = localizedValue; } } return result; } export function getDictionary(locale: Locale): Dictionary { const localized = (dictionaries as unknown as Record)[ locale ]; if (!localized || locale === "en") return dictionaries.en; return mergeWithEnglishFallback(dictionaries.en, localized) as Dictionary; }