first commit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import 'server-only';
|
||||
import { dict, type Dict } from '@/lib/i18n/dictionaries';
|
||||
import { getAllSections } from '@/lib/db/store';
|
||||
|
||||
/**
|
||||
* The shape handed to the client: a fully-resolved bilingual content tree.
|
||||
* It is structurally identical to `dict` so `LocaleProvider` can drop it in
|
||||
* without any component being aware the data now comes from a database.
|
||||
*/
|
||||
export type SiteContent = { fa: Dict; en: Dict };
|
||||
|
||||
/**
|
||||
* Build the live content tree: start from the in-code `dict` defaults, then
|
||||
* overlay any per-section overrides saved through the admin panel. Each stored
|
||||
* override is `{ fa, en }` for one top-level section key and replaces that
|
||||
* subtree wholesale (the admin always edits and saves a complete section).
|
||||
*/
|
||||
export function loadContent(): SiteContent {
|
||||
// Shallow clone the locale roots so we can swap section subtrees safely.
|
||||
// `as const` gives fa/en distinct literal types, so cast through unknown.
|
||||
const fa = { ...dict.fa } as unknown as Dict;
|
||||
const en = { ...dict.en } as unknown as Dict;
|
||||
|
||||
for (const row of getAllSections()) {
|
||||
let payload: { fa?: unknown; en?: unknown };
|
||||
try {
|
||||
payload = JSON.parse(row.data);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
const key = row.key as keyof Dict;
|
||||
if (payload.fa !== undefined) (fa as Record<string, unknown>)[key] = payload.fa;
|
||||
if (payload.en !== undefined) (en as Record<string, unknown>)[key] = payload.en;
|
||||
}
|
||||
|
||||
return { fa, en };
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a single section for the admin editor: the saved override if one
|
||||
* exists, otherwise the in-code default for both locales.
|
||||
*/
|
||||
export function loadSection(key: keyof Dict): { fa: unknown; en: unknown } {
|
||||
for (const row of getAllSections()) {
|
||||
if (row.key !== key) continue;
|
||||
try {
|
||||
const payload = JSON.parse(row.data);
|
||||
return {
|
||||
fa: payload.fa ?? dict.fa[key],
|
||||
en: payload.en ?? dict.en[key],
|
||||
};
|
||||
} catch {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return { fa: dict.fa[key], en: dict.en[key] };
|
||||
}
|
||||
Reference in New Issue
Block a user