Skip to content

Commit

Permalink
sitelinks: allow to set lang=auto
Browse files Browse the repository at this point in the history
addressing #21 (comment)
  • Loading branch information
maxlath committed Jan 13, 2022
1 parent 912c240 commit 232dcde
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
10 changes: 5 additions & 5 deletions docs/readme/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ By default, the destination is Wikipedia in the user language, which is guessed
#### Wikimedia Projects
###### lang

Pass a `lang` parameter (or just `l`) to override the `accept-language` header. Pass several values to set the fallback chain.
Pass a `lang` parameter (or just `l`) to override the `accept-language` header. Pass several values to set the fallback chain. The value `auto` can be used to represent the value of the `accept-language` header.

| request | redirection |
|:--------------------------------------------|:-----------------------------------------------------|
| `/Q184226?lang=fr` | https://fr.wikipedia.org/wiki/Gilles_Deleuze |
| `/Q184226?lang=als,oc,fr,en&site=wikiquote` | https://oc.wikipedia.org/wiki/Gilles_Deleuze |
| request | redirection |
|:-------------------------------------------------|:-----------------------------------------------------|
| `/Q184226?lang=fr` | https://fr.wikipedia.org/wiki/Gilles_Deleuze |
| `/Q184226?lang=als,oc,auto,fr,en&site=wikiquote` | https://oc.wikipedia.org/wiki/Gilles_Deleuze |

###### site

Expand Down
14 changes: 13 additions & 1 deletion lib/get_request_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ module.exports = (query, headers, context) => {
}

const site = query.site || 'wiki'
const lang = query.lang || parseLangHeader(headers) || 'en'

const langHeader = parseLangHeader(headers)

let lang
if (query.lang) {
lang = query.lang
.split(',')
.map(l => l === 'auto' ? langHeader : l)
.join(',')
} else {
lang = langHeader || 'en'
}

logger.info('site', site)
logger.info('lang', lang)

Expand Down
6 changes: 6 additions & 0 deletions test/sitelinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ describe('sitelinks', function () {
res.headers.get('location').should.equal('https://de.wikipedia.org/wiki/Gilles_Deleuze')
})

it('should use the header in auto mode', async () => {
const res = await get('/Q184226?lang=auto,de,fr', 'es')
res.statusCode.should.equal(302)
res.headers.get('location').should.equal('https://es.wikipedia.org/wiki/Gilles_Deleuze')
})

it('should take the site from the query', async () => {
const res = await get('/Q184226?site=wikiquote')
res.statusCode.should.equal(302)
Expand Down

0 comments on commit 232dcde

Please sign in to comment.