Skip to content

Commit

Permalink
lookup subdomain with localhost option (#264)
Browse files Browse the repository at this point in the history
* lookup subdomain with localhost option

* subdomain detection  test and fix
  • Loading branch information
handbremse committed Aug 11, 2022
1 parent ff29d32 commit 2cca31d
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 65 deletions.
19 changes: 7 additions & 12 deletions i18nextBrowserLanguageDetector.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,21 +339,16 @@
var subdomain = {
name: 'subdomain',
lookup: function lookup(options) {
var found;
// If given get the subdomain index else 1
var lookupFromSubdomainIndex = typeof options.lookupFromSubdomainIndex === 'number' ? options.lookupFromSubdomainIndex + 1 : 1; // get all matches if window.location. is existing
// first item of match is the match itself and the second is the first group macht which sould be the first subdomain match
// is the hostname no public domain get the or option of localhost

if (typeof window !== 'undefined') {
var language = window.location.href.match(/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/gi);
var language = typeof window !== 'undefined' && window.location && window.location.hostname && window.location.hostname.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i); // if there is no match (null) return undefined

if (language instanceof Array) {
if (typeof options.lookupFromSubdomainIndex === 'number') {
found = language[options.lookupFromSubdomainIndex].replace('http://', '').replace('https://', '').replace('.', '');
} else {
found = language[0].replace('http://', '').replace('https://', '').replace('.', '');
}
}
}
if (!language) return undefined; // return the given group match

return found;
return language[lookupFromSubdomainIndex];
}
};

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.

32 changes: 19 additions & 13 deletions src/browserLookups/subdomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ export default {
name: 'subdomain',

lookup(options) {
let found;
if (typeof window !== 'undefined') {
const language = window.location.href.match(/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/gi);
if (language instanceof Array) {
if (typeof options.lookupFromSubdomainIndex === 'number') {
found = language[options.lookupFromSubdomainIndex].replace('http://', '').replace('https://', '').replace('.', '');
} else {
found = language[0].replace('http://', '').replace('https://', '').replace('.', '');
}
}
}
return found;
}
// If given get the subdomain index else 1
const lookupFromSubdomainIndex =
typeof options.lookupFromSubdomainIndex === 'number'
? options.lookupFromSubdomainIndex + 1
: 1;
// get all matches if window.location. is existing
// first item of match is the match itself and the second is the first group macht which sould be the first subdomain match
// is the hostname no public domain get the or option of localhost
const language =
typeof window !== 'undefined' &&
window.location &&
window.location.hostname &&
window.location.hostname.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i);

// if there is no match (null) return undefined
if (!language) return undefined;
// return the given group match
return language[lookupFromSubdomainIndex];
},
};
105 changes: 66 additions & 39 deletions test/languageDetector.test.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,96 @@
const expect = require('expect.js')
const i18next = require('i18next')
const LanguageDetector = require('../dist/cjs/i18nextBrowserLanguageDetector.js')
const expect = require('expect.js');
const i18next = require('i18next');
const LanguageDetector = require('../dist/cjs/i18nextBrowserLanguageDetector.js');

i18next.init()
i18next.init();

describe('language detector', () => {
const ld = new LanguageDetector(i18next.services, { order: ['querystring', 'path', 'cookie', 'sessionStorage', 'localStorage', 'navigator', 'htmlTag'] })
const ld = new LanguageDetector(i18next.services, {
order: [
'subdomain',
'querystring',
'path',
'cookie',
'sessionStorage',
'localStorage',
'navigator',
'htmlTag',
],
});

describe('cookie', () => {
it('detect', () => {
global.document = {
cookie: 'i18next=de'
}
const lng = ld.detect()
expect(lng).to.contain('de')
})
cookie: 'i18next=de',
};
const lng = ld.detect();
expect(lng).to.contain('de');
});

it('cacheUserLanguage', () => {
global.document = {
cookie: 'my=cookie'
}
ld.cacheUserLanguage('it', ['cookie'])
expect(global.document.cookie).to.match(/i18next=it/)
expect(global.document.cookie).to.match(/Path=\//)
cookie: 'my=cookie',
};
ld.cacheUserLanguage('it', ['cookie']);
expect(global.document.cookie).to.match(/i18next=it/);
expect(global.document.cookie).to.match(/Path=\//);
// expect(global.document.cookie).to.match(/my=cookie/)
})
})
});
});

describe('path', () => {
it('detect', () => {
global.window = {
location: {
pathname: '/fr/some/route',
search: ''
}
}
const lng = ld.detect()
expect(lng).to.contain('fr')
})
})
search: '',
},
};
const lng = ld.detect();
expect(lng).to.contain('fr');
});
});

describe('querystring', () => {
it('detect', () => {
global.window = {
location: {
pathname: '/fr/some/route',
search: '?lng=de'
}
}
const lng = ld.detect()
expect(lng).to.contain('de')
})
})
search: '?lng=de',
},
};
const lng = ld.detect();
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')
})
})
})
search: '',
},
};
const lng = ld.detect();
expect(lng).to.contain('de');
});
});

describe('subdomain', () => {
it('detect', () => {
global.window = {
location: {
hostname: 'es.foot-print-on-the-moon-1968.org',
href: 'http://es.foot-print-on-the-moon-1968.org/fr/some/route',
pathname: '/fr/some/route',
hash: '#/something?lng=de',
search: '?lng=de',
},
};
const lng = ld.detect();
expect(lng).to.contain('es');
});
});
});

0 comments on commit 2cca31d

Please sign in to comment.