import { fa } from "./fa"; export type LocaleKey = "fa"; export interface TranslationsType { [key: string]: string | TranslationsType; } const locales: Record = { fa }; let currentLang: LocaleKey = "fa"; export const setLang = (lang: LocaleKey) => { currentLang = lang; }; export const getLang = (): LocaleKey => currentLang; const cache = new Map(); export const t = (key: string, fallback?: string): string => { const cacheKey = `${currentLang}:${key}`; if (cache.has(cacheKey)) return cache.get(cacheKey)!; const keys = key.split("."); let value: string | TranslationsType | undefined = locales[currentLang]; for (const k of keys) { if (typeof value === "object" && value !== null) { value = value[k]; } else { value = undefined; break; } } const result = typeof value === "string" ? value : fallback || key; cache.set(cacheKey, result); return result; };