Skip to content

Commit

Permalink
fix querystring lookup if happening after # (fragment)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Mar 23, 2022
1 parent 18997a6 commit 1b74005
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 6.1.4

- fix querystring lookup if happening after # [256](https://github.com/i18next/i18next-browser-languageDetector/issues/256)

### 6.1.3

- export DecetorOptions and CustomDetector types
Expand Down
8 changes: 7 additions & 1 deletion i18nextBrowserLanguageDetector.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@
var found;

if (typeof window !== 'undefined') {
var query = window.location.search.substring(1);
var search = window.location.search;

if (!window.location.search && window.location.hash && window.location.hash.indexOf('?') > -1) {
search = window.location.hash.substring(window.location.hash.indexOf('?'));
}

var query = search.substring(1);
var params = query.split('&');

for (var i = 0; i < params.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion i18nextBrowserLanguageDetector.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/browserLookups/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ export default {
let found;

if (typeof window !== 'undefined') {
let query = window.location.search.substring(1);
let params = query.split('&');
let search = window.location.search;
if (!window.location.search && window.location.hash && window.location.hash.indexOf('?') > -1) {
search = window.location.hash.substring(window.location.hash.indexOf('?'))
}
const query = search.substring(1);
const params = query.split('&');
for (let i=0; i<params.length; i++) {
let pos = params[i].indexOf('=');
if (pos > 0) {
Expand Down
14 changes: 14 additions & 0 deletions test/languageDetector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,18 @@ describe('language detector', () => {
expect(lng).to.contain('de')
})
})

describe('querystring (fragment)', () => {
it('detect', () => {
global.window = {
location: {
pathname: '/fr/some/route',
hash: '#/something?lng=de',
search: ''
}
}
const lng = ld.detect()
expect(lng).to.contain('de')
})
})
})

0 comments on commit 1b74005

Please sign in to comment.