From cc8600263d7924fa61d2fd7f13f88c17513c61e1 Mon Sep 17 00:00:00 2001 From: TheFlow Date: Sat, 1 Nov 2025 10:03:58 +1300 Subject: [PATCH] fix: add fallback for separate language documents in API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/controllers/documents.controller.js | 30 +++++++++++++++++++------ 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/controllers/documents.controller.js b/src/controllers/documents.controller.js index 3eb1496d..8663b872 100644 --- a/src/controllers/documents.controller.js +++ b/src/controllers/documents.controller.js @@ -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,14 +133,30 @@ async function getDocument(req, res) { success: true, document: translatedDoc }); - } else { - // Translation not available - return res.status(404).json({ - error: 'Not Found', - message: `Translation not available for language: ${lang}`, - available_languages: Object.keys(document.translations || {}) + } + + // 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', + message: `Translation not available for language: ${lang}`, + available_languages: Object.keys(document.translations || {}) + }); } // Default: Return English version