Files
freeCodeCamp/client/src/components/create-language-redirect.ts
Parth Parth 11d71adc6e feat(client): ts-migrate /client/src/components/ redirects (#42642)
* rename

* migrate

* migrate create language redirect

* fix type of redirects

* migrate test

Co-authored-by: Parth Parth <thecodingaviator@users.noreply.github.com>
Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
2021-06-30 20:52:17 +05:30

27 lines
798 B
TypeScript

const createLanguageRedirect = ({
clientLocale,
lang
}: {
clientLocale: string;
lang: string;
}): string => {
// return early if requesting the same page
if (clientLocale === lang) return window?.location.toString();
const pathArray = window?.location?.pathname?.split('/');
const path = pathArray
.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;