Skip to content

Falling back to english when page hasn't been translated locally #490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const chokidar = require('chokidar')
const mount = st({
path: path.join(__dirname, 'build'),
cache: false,
index: 'index.html'
index: 'index.html',
passthrough: true
})

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

// Redirect mechanism meant as a fix for languages where some pages
// has not translated yet, therefore redirect to the english equivalent,
// which ofc isn't the correct language, but better than a 404-page
function redirectToEnglishUrl (req, res) {
return () => {
const isAlreadyEnglish = req.url.startsWith('/en')
const urlContainsLanguage = req.url.split('/').length > 2

if (isAlreadyEnglish || !urlContainsLanguage) {
res.writeHead(404, 'Not found')
return res.end()
}

let englishUrl = req.url.replace(/^\/\w+\//, '/en/')

res.writeHead(302, {
location: englishUrl
})
res.end()
}
}

// Gets the locale name by path.
function getLocale (filePath) {
const pre = path.join(__dirname, 'locale')
Expand Down Expand Up @@ -61,7 +84,9 @@ statics.on('add', (filePath) => {
})

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

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