/** * Sonamu 내부용 SD (Sonamu Dictionary) 함수 * sonamu 코어 내부에서 사용하는 i18n 함수입니다. */ import { type SonamuConfig } from "../api/config"; import en from "./en"; import ko from "./ko"; import { type LocalizedString } from "./types"; type SonamuDict = typeof ko; type DictKey = keyof SonamuDict; type MergedDictionary = { [K in keyof SonamuDict]: SonamuDict[K] extends (...args: infer P) => string ? (...args: P) => string : string; }; const dictionaries: Record = { ko, en, }; let currentI18nConfig: SonamuConfig["i18n"] | null = null; type SDReturnType = SonamuDict[K] extends (...args: infer P) => string ? (...args: P) => LocalizedString : LocalizedString; function getCurrentI18nConfig(): SonamuConfig["i18n"] { if (currentI18nConfig === null) { throw new Error("Sonamu i18n config has not been initialized"); } return currentI18nConfig; } export function setSDConfig(i18nConfig: SonamuConfig["i18n"]): void { currentI18nConfig = i18nConfig; } function getDictValue(key: K, locale: string): SDReturnType { const { defaultLocale, supportedLocales } = getCurrentI18nConfig(); // 1. 지정된 locale에서 조회 const dict = dictionaries[locale]; if (dict?.[key] !== undefined) { return dict[key] as unknown as SDReturnType; } // 2. default locale에서 조회 if (locale !== defaultLocale && dictionaries[defaultLocale]?.[key] !== undefined) { return dictionaries[defaultLocale][key] as unknown as SDReturnType; } // 3. supported locales 순회 for (const supportedLocale of supportedLocales) { if (supportedLocale !== locale && supportedLocale !== defaultLocale) { if (dictionaries[supportedLocale]?.[key] !== undefined) { return dictionaries[supportedLocale][key] as unknown as SDReturnType; } } } // 4. 모두 실패 시 key 반환 return key as unknown as SDReturnType; } /** * Sonamu 내부용 SD 함수 * sonamu 코어 내부에서만 사용합니다. * * @example * SD("error.notFound") // → "찾을 수 없습니다" 또는 "Not found" (LocalizedString) * SD("error.entityNotFound")("User", 1) // → "존재하지 않는 User ID 1" (LocalizedString) */ export function SD(key: K): SDReturnType { const configLocale = getCurrentI18nConfig().defaultLocale; return getDictValue(key, configLocale); }