Skip to content

Commit

Permalink
Add LanguageDetector component to handle language selection based on …
Browse files Browse the repository at this point in the history
…browser settings
  • Loading branch information
jamalsoueidan committed Jun 23, 2024
1 parent 9c7b086 commit 0629776
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
77 changes: 77 additions & 0 deletions app/components/LanguageDetector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {ActionIcon, Card, Flex, Modal, rem, Title} from '@mantine/core';
import {AE, DK, US} from 'country-flag-icons/react/1x1';
import {useEffect, useState} from 'react';
import {useTranslation} from 'react-i18next';

export const LanguageDetector = () => {
const {i18n} = useTranslation();
const [showModal, setShowModal] = useState(false);
const [currentPath, setCurrentPath] = useState('');

useEffect(() => {
if (typeof window !== 'undefined') {
setCurrentPath(
window.location.pathname +
window.location.search +
window.location.hash,
);

const browserLanguage = navigator.language.split('-')[0];
const currentLanguage = i18n.language.split('-')[0];
const userDecision = localStorage.getItem('languageDecision');

if (!userDecision && browserLanguage !== currentLanguage) {
setShowModal(true);
}
}
}, [i18n.language]);

const getNewUrl = (newDomain: string) => `${newDomain}${currentPath}`;

const changeLanguage = (url: string) => {
localStorage.setItem('languageDecision', 'true');
window.location.href = url;
};

const closeModal = () => {
localStorage.setItem('languageDecision', 'true');
setShowModal(false);
};

return (
<Modal opened={showModal} onClose={closeModal}>
<Flex gap="md" align="center" justify="center">
<Card withBorder onClick={closeModal}>
<ActionIcon size={rem(100)} radius={rem(100)} bg="transparent">
<DK style={{width: '100px', height: '100px'}} />
</ActionIcon>
<Title fw="500" ta="center">
Dansk
</Title>
</Card>
<Card
withBorder
onClick={() => changeLanguage(getNewUrl('https://en.bysisters.dk'))}
>
<ActionIcon size={rem(100)} radius={rem(100)} bg="transparent">
<US style={{width: '100px', height: '100px'}} />
</ActionIcon>
<Title fw="500" ta="center">
English
</Title>
</Card>
<Card
withBorder
onClick={() => changeLanguage(getNewUrl('https://ar.bysisters.dk'))}
>
<ActionIcon size={rem(100)} radius={rem(100)} bg="transparent">
<AE style={{width: '100px', height: '100px'}} />
</ActionIcon>
<Title fw="400" ta="center">
عربي
</Title>
</Card>
</Flex>
</Modal>
);
};
2 changes: 2 additions & 0 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
import {useEffect, type ReactNode} from 'react';
import favicon from './assets/favicon.svg';
import {CustomAnalytics} from './components/CustomAnalytics';
import {LanguageDetector} from './components/LanguageDetector';
import {LayoutWrapper} from './components/LayoutWrapper';
import appStyles from './styles/app.css?url';

Expand Down Expand Up @@ -177,6 +178,7 @@ export function Layout({children}: {children: ReactNode}) {
) : (
children
)}
<LanguageDetector />
</ModalsProvider>
</MantineProvider>
</DirectionProvider>
Expand Down
1 change: 0 additions & 1 deletion app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export async function loader({context, request}: LoaderFunctionArgs) {
export default function Homepage() {
const {page} = useLoaderData<typeof loader>();

console.log(page);
return (
<>
<Header />
Expand Down

0 comments on commit 0629776

Please sign in to comment.