fix: load translations from API instead of static CDN files
Some checks failed
CI / API Lint (push) Has been cancelled
CI / Admin UI Tests (push) Has been cancelled
CI / Admin UI Build (push) Has been cancelled
CI / Detect changes (push) Has been cancelled
CI / API Tests (push) Has been cancelled
CI / Scanner Lint (push) Has been cancelled
CI / Scanner Tests (push) Has been cancelled
CI / Banner Lint & Typecheck (push) Has been cancelled
CI / Banner Tests (push) Has been cancelled
CI / Banner Build (push) Has been cancelled
CI / Admin UI Typecheck (push) Has been cancelled

The banner was fetching /translations-{locale}.json from the CDN as
static files, but translations are stored in the DB and served via
the public /api/v1/translations/{siteId}/{locale} endpoint.

Fixes:
- fetchTranslations() now calls the public API endpoint
- loadTranslations() takes (apiBase, siteId, locale)
- banner.ts passes apiBase and siteId to loadTranslations()
- i18n.test.ts updated to match new signature
This commit is contained in:
Kunthawat Greethong
2026-06-15 18:30:02 +07:00
parent e9bae32ee2
commit 683aa2379d
5 changed files with 25 additions and 14 deletions

View File

@@ -84,34 +84,38 @@ export function normaliseLocale(locale: string): string {
}
/**
* Fetch translations for a locale from the CDN.
* Fetch translations for a locale from the public API endpoint.
* Returns null if not found or on error.
*/
export async function fetchTranslations(
cdnBase: string,
apiBase: string,
siteId: string,
locale: string,
): Promise<Partial<TranslationStrings> | null> {
try {
const resp = await fetch(`${cdnBase}/translations-${locale}.json`);
const resp = await fetch(`${apiBase}/api/v1/translations/${siteId}/${locale}`);
if (!resp.ok) return null;
return (await resp.json()) as Partial<TranslationStrings>;
// API returns { strings: { ... } }
const data = (await resp.json()) as { strings?: Partial<TranslationStrings> };
return data.strings ?? null;
} catch {
return null;
}
}
/**
* Load translations: try fetching from CDN, fall back to defaults.
* Load translations: try fetching from API, fall back to defaults.
*/
export async function loadTranslations(
cdnBase: string,
apiBase: string,
siteId: string,
locale: string,
): Promise<TranslationStrings> {
if (locale === 'en') {
return { ...DEFAULT_TRANSLATIONS };
}
const remote = await fetchTranslations(cdnBase, locale);
const remote = await fetchTranslations(apiBase, siteId, locale);
if (!remote) {
return { ...DEFAULT_TRANSLATIONS };
}