Skip to content

Commit d6cdd94

Browse files
phillipjfhemberger
authored andcommitted
Falling back to english when page hasn't been translated locally. (#490)
Previously translation working groups would have to translate *all* pages to their respective language, if not most pages would end in displaying a (english) 404 page. That's a massive requirement on translation groups, and keeping up with future changes are probably near impossible. Lowering the barrier and letting translation groups start on the frontpage and work their way down a couple of page depths, should be alot simpler and more rewarding. NB! This only affects local development as nodejs.org runs nginx serving strictly static content. Getting the same behaviour in production requires changes to the nginx config.
1 parent d23e2a2 commit d6cdd94

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

server.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const chokidar = require('chokidar')
1010
const mount = st({
1111
path: path.join(__dirname, 'build'),
1212
cache: false,
13-
index: 'index.html'
13+
index: 'index.html',
14+
passthrough: true
1415
})
1516

1617
const build = require('./build')
@@ -34,6 +35,28 @@ const locales = chokidar.watch(path.join(__dirname, 'locale'), opts)
3435
const layouts = chokidar.watch(path.join(__dirname, 'layouts'), opts)
3536
const statics = chokidar.watch(path.join(__dirname, 'static'), opts)
3637

38+
// Redirect mechanism meant as a fix for languages where some pages
39+
// has not translated yet, therefore redirect to the english equivalent,
40+
// which ofc isn't the correct language, but better than a 404-page
41+
function redirectToEnglishUrl (req, res) {
42+
return () => {
43+
const isAlreadyEnglish = req.url.startsWith('/en')
44+
const urlContainsLanguage = req.url.split('/').length > 2
45+
46+
if (isAlreadyEnglish || !urlContainsLanguage) {
47+
res.writeHead(404, 'Not found')
48+
return res.end()
49+
}
50+
51+
let englishUrl = req.url.replace(/^\/\w+\//, '/en/')
52+
53+
res.writeHead(302, {
54+
location: englishUrl
55+
})
56+
res.end()
57+
}
58+
}
59+
3760
// Gets the locale name by path.
3861
function getLocale (filePath) {
3962
const pre = path.join(__dirname, 'locale')
@@ -65,7 +88,9 @@ statics.on('add', (filePath) => {
6588
})
6689

6790
// Initializes the server and mounts it in the generated build directory.
68-
http.createServer(mount).listen(port, () => console.log(`http://localhost:${port}/en/`))
91+
http.createServer((req, res) => {
92+
mount(req, res, redirectToEnglishUrl(req, res))
93+
}).listen(port, () => console.log(`http://localhost:${port}/en/`))
6994

7095
// Start the initial build of static HTML pages
7196
build.fullBuild()

0 commit comments

Comments
 (0)