Skip to content

Commit dcd72d1

Browse files
authored
Merge pull request github#19430 from github/fix-for-translations
Fallback if translated file cannot be found
2 parents 0e8d5e3 + 9bb120b commit dcd72d1

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

middleware/contextualizers/generic-toc.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module.exports = async function genericToc (req, res, next) {
88
const currentSiteTree = req.context.siteTree[req.context.currentLanguage][req.context.currentVersion]
99

1010
// Find the array of child pages that start with the requested path.
11-
const currentPageInSiteTree = findPageInSiteTree(currentSiteTree.childPages, req.context.currentPath)
11+
const currentPageInSiteTree = findPageInSiteTree(currentSiteTree.childPages, req.context.currentPath, req.context.currentLanguage)
12+
if (!currentPageInSiteTree) return next()
1213

1314
req.context.tocItems = sortBy(
1415
await getUnsortedTocItems(currentPageInSiteTree.childPages, req.context),
@@ -21,13 +22,22 @@ module.exports = async function genericToc (req, res, next) {
2122

2223
// Recursively loop through the siteTree until we reach the point where the
2324
// current siteTree page is the same as the requested page. Then stop.
24-
function findPageInSiteTree (pageArray, currentPath) {
25+
function findPageInSiteTree (pageArray, currentPath, currentLanguage) {
2526
const childPage = pageArray.find(page => {
2627
// Find a page that matches at least an initial part of the current path
2728
const regex = new RegExp(`^${page.href}($|/)`, 'm')
2829
return regex.test(currentPath)
2930
})
3031

32+
// Fallback for outdated translations
33+
if (!childPage && currentLanguage !== 'en') {
34+
return findPageInSiteTree(pageArray, currentPath.replace(`/${currentLanguage}`, '/en'), 'en')
35+
}
36+
37+
if (!childPage && currentLanguage === 'en') {
38+
return
39+
}
40+
3141
if (childPage.href === currentPath) {
3242
return childPage
3343
}

0 commit comments

Comments
 (0)