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]) { if (document.translations && document.translations[lang]) {
const translation = document.translations[lang]; const translation = document.translations[lang];
@ -133,14 +133,30 @@ async function getDocument(req, res) {
success: true, success: true,
document: translatedDoc document: translatedDoc
}); });
} else { }
// Translation not available
return res.status(404).json({ // FALLBACK: Check for separate language document (e.g., glossary-de, glossary-fr)
error: 'Not Found', // Try appending language code to slug
message: `Translation not available for language: ${lang}`, const translatedSlug = `${identifier}-${lang}`;
available_languages: Object.keys(document.translations || {}) 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',
message: `Translation not available for language: ${lang}`,
available_languages: Object.keys(document.translations || {})
});
} }
// Default: Return English version // Default: Return English version