mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-25 14:01:44 -04:00
* 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>
27 lines
798 B
TypeScript
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;
|