mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-11 13:00:56 -04:00
21 lines
717 B
JavaScript
21 lines
717 B
JavaScript
const createLanguageRedirect = ({ clientLocale, lang }) => {
|
|
// return early if requesting the same page
|
|
if (clientLocale === lang) return `${window?.location}`;
|
|
|
|
let path = window?.location?.pathname?.split('/');
|
|
path = path
|
|
.filter(item => (item !== clientLocale && item !== lang ? item : ''))
|
|
.join('/');
|
|
|
|
const hostTail = window?.location?.host.split('.').slice(1).join('.');
|
|
const nextClient = lang !== 'chinese' ? 'www' : 'chinese';
|
|
const nextLocation = `${window?.location?.protocol}//${nextClient}.${hostTail}`;
|
|
|
|
if (lang === 'english' || lang === 'chinese')
|
|
return `${nextLocation}/${path}`;
|
|
|
|
return `${nextLocation}/${lang}/${path}`;
|
|
};
|
|
|
|
export default createLanguageRedirect;
|