forked from electron/electronjs.org-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context-builder.js
65 lines (56 loc) · 2.05 KB
/
context-builder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const i18n = require('lib/i18n')
const electronReleases = require('electron-releases')
const electronLatestStableVersion = i18n.electronLatestStableTag.slice(1)
const { deps } = electronReleases.find(
(release) => release.version === electronLatestStableVersion
)
const { getLanguageNativeName } = require('locale-code')
const rtlDetect = require('rtl-detect')
const Releases = require('../lib/releases')
// Supply all route handlers with a baseline `req.context` object
module.exports = function contextBuilder(req, res, next) {
// Attach i18n object to request so any route handler can use it if needed
req.i18n = i18n
// This allows the language to be set per-request using a query param, so
// folks can share a link like /docs/api/app?lang=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)
if (req.query.lang) {
req.language = req.query.lang
}
// If request language does not exist, fall back to English
// e.g. /?lang=Foo
if (!i18n.website[req.language]) req.language = 'en-US'
const localized = i18n.website[req.language]
// Page titles, descriptions, etc
let page = Object.assign(
{
title: 'Electron',
path: req.path,
},
i18n.website[req.language].pages[req.path]
)
if (req.path !== '/') {
page.title = `${page.title} | Electron`
}
req.context = {
electronLatestStableVersion,
electronLatestStableTag: i18n.electronLatestStableTag,
deps: deps,
releases: new Releases(electronReleases),
currentLocale: req.language,
currentLocaleNativeName: getLanguageNativeName(req.language),
languageDirection: rtlDetect.getLangDir(req.language),
locales: i18n.locales,
page: page,
showAnnouncementBanner: true,
localized: localized,
cookies: req.cookies,
hostname: `${req.protocol}://${req.get('host')}`,
}
if (req.path.startsWith('/docs')) {
req.context.docs = i18n.docs[req.language]
}
return next()
}