Files
freeCodeCamp/client/src/components/createLanguageRedirect.js
2021-03-11 12:19:42 +05:30

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;