fix: add fallback for separate language documents in API

When requesting a translation via ?lang=de or ?lang=fr, the API now:
1. First checks for embedded translations (document.translations.de/fr)
2. Falls back to checking for separate documents with -de/-fr suffix

This allows the glossary translations (glossary-de, glossary-fr) to work
with the standard /api/documents/glossary?lang=de endpoint.

Fixes the 404 error when switching languages on /docs.html page.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
TheFlow 2025-11-01 10:03:58 +13:00
parent f0db6052ad
commit cc8600263d

View file

@ -110,7 +110,7 @@ async function getDocument(req, res) {
});
}
// Check if translation exists
// Check if translation exists (embedded in document)
if (document.translations && document.translations[lang]) {
const translation = document.translations[lang];
@ -133,7 +133,24 @@ async function getDocument(req, res) {
success: true,
document: translatedDoc
});
} else {
}
// FALLBACK: Check for separate language document (e.g., glossary-de, glossary-fr)
// Try appending language code to slug
const translatedSlug = `${identifier}-${lang}`;
const translatedDoc = await Document.findBySlug(translatedSlug);
if (translatedDoc) {
return res.json({
success: true,
document: {
...translatedDoc,
language: lang,
sections: undefined // Force traditional view
}
});
}
// Translation not available
return res.status(404).json({
error: 'Not Found',
@ -141,7 +158,6 @@ async function getDocument(req, res) {
available_languages: Object.keys(document.translations || {})
});
}
}
// Default: Return English version
res.json({