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

Commit

Permalink
Merge pull request #1034 from electron/language-query-fix
Browse files Browse the repository at this point in the history
add lang query param and document it in contributing
  • Loading branch information
vanessayuenn committed Jan 25, 2018
2 parents 1930361 + ff007ae commit 9dfab01
Show file tree
Hide file tree
Showing 5 changed files with 2,107 additions and 2,823 deletions.
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ See
on how to get started, or jump right into translating at
[crowdin.com/project/electron](https://crowdin.com/project/electron).

#### Sharing Localized URLs

If you wish to share a URL linking to a translated page of the Electron website,
add a `lang` param to the URL. This will override the visitor's existing
language preferences and display the given page in the specified language:

Example:
[/docs/api/browser-window?lang=fr-FR](https://electronjs.org/docs/api/browser-window?lang=fr-FR)

## Routes

Website routes are defined in [server.js](server.js).
Expand Down
12 changes: 7 additions & 5 deletions middleware/context-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ module.exports = function contextBuilder (req, res, next) {
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?language=fr-FR and know that
// 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)
const targetLanguage = req.query.language || req.language
if (req.query.lang) {
req.language = req.query.lang
}

const localized = i18n.website[targetLanguage]
const localized = i18n.website[req.language]

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

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

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

return next()
Expand Down

0 comments on commit 9dfab01

Please sign in to comment.