Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Support a new language query param #1031

Merged
merged 3 commits into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
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
13 changes: 10 additions & 3 deletions middleware/context-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ module.exports = function contextBuilder (req, res, next) {
// Attach i18n object to request so any route handler can use it if needed
req.i18n = i18n

const localized = i18n.website[req.language]
// This allows the language to be set per-request using a query param, so
// folks can share a link like /docs/api/app?language=fr-FR and know that
// the recipient will see the doc in that language, regardless of their
// language settings. If no query param is set, fall back to the default
// language (or the one set in the cookie)
const targetLanguage = req.query.language || req.language

const localized = i18n.website[targetLanguage]

// Page titles, descriptions, etc
let page = Object.assign({
title: 'Electron',
path: req.path
}, i18n.website[req.language].pages[req.path])
}, i18n.website[targetLanguage].pages[req.path])

if (req.path !== '/') {
page.title = `${page.title} | Electron`
Expand All @@ -34,7 +41,7 @@ module.exports = function contextBuilder (req, res, next) {
}

if (req.path.startsWith('/docs')) {
req.context.docs = i18n.docs[req.language]
req.context.docs = i18n.docs[targetLanguage]
}

return next()
Expand Down
18 changes: 14 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,20 @@ describe('electronjs.org', () => {
})
})

test('/languages', async () => {
const $ = await get('/languages')
$('h1').text().should.eq('Languages')
$('body').text().should.include('global developer community')
describe('languages', () => {
test('/languages', async () => {
const $ = await get('/languages')
$('h1').text().should.eq('Languages')
$('body').text().should.include('global developer community')
})

test('language query param for one-off viewing in other languages', async () => {
let $ = await get('/docs/api/browser-window?language=fr-FR')
$('body').text().should.include('fenêtres')

$ = await get('/docs/api/browser-window')
$('body').text().should.not.include('fenêtres')
})
})

test('redirects for date-style blog URLs', async () => {
Expand Down