35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
// ============================================
|
|
// Site Settings Utilities
|
|
// Helper functions for copy configuration
|
|
// ============================================
|
|
|
|
import { getSiteType } from "../site-types";
|
|
|
|
type SiteCopyTranslator = (key: string) => string;
|
|
|
|
export function getSiteCopyFallbackKey(type: string, key: string): string {
|
|
return `models.site.types.${type}.copy.${key}.fallback`;
|
|
}
|
|
|
|
function resolveCopyFallback(type: string, key: string, fallback: string, t?: SiteCopyTranslator): string {
|
|
if (!t) return fallback;
|
|
|
|
const fallbackKey = getSiteCopyFallbackKey(type, key);
|
|
const translated = t(fallbackKey);
|
|
return translated === fallbackKey ? fallback : translated;
|
|
}
|
|
|
|
/**
|
|
* Build copy object with fallback values for missing keys
|
|
*/
|
|
export function buildCopyWithFallbacks(type: string, savedCopy: Record<string, string> | null, t?: SiteCopyTranslator): Record<string, string> {
|
|
const siteType = getSiteType(type);
|
|
if (!siteType) return {};
|
|
|
|
const result: Record<string, string> = {};
|
|
for (const { key, fallback } of siteType.copyKeys) {
|
|
result[key] = savedCopy?.[key] || resolveCopyFallback(type, key, fallback, t);
|
|
}
|
|
return result;
|
|
}
|