generated from theodorusclarence/ts-nextjs-tailwind-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from martapanc/feat/i18n
Feat/i18n
- Loading branch information
Showing
23 changed files
with
342 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
'use client'; | ||
|
||
import { ThemeProvider } from 'next-themes'; | ||
import { ThemeProvider as NextThemeProvider } from 'next-themes'; | ||
import type { PropsWithChildren } from 'react'; | ||
|
||
import MuiThemeProvider from '@/components/helpers/MuiThemeProvider'; | ||
import Footer from '@/components/layout/Footer'; | ||
import Header from '@/components/layout/Header'; | ||
|
||
export default function LayoutClient({ children }: PropsWithChildren) { | ||
return ( | ||
<ThemeProvider attribute='class' defaultTheme='light' enableSystem={false}> | ||
<Header /> | ||
<NextThemeProvider | ||
attribute='class' | ||
defaultTheme='light' | ||
enableSystem={false} | ||
> | ||
<MuiThemeProvider> | ||
<Header /> | ||
|
||
<main id='content'>{children}</main> | ||
<main id='content'>{children}</main> | ||
|
||
<Footer /> | ||
</ThemeProvider> | ||
<Footer /> | ||
</MuiThemeProvider> | ||
</NextThemeProvider> | ||
); | ||
} |
89 changes: 89 additions & 0 deletions
89
src/components/atoms/LanguageSwitcher/LanguageSwitcher.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { InputLabel, MenuItem, Select, SelectChangeEvent } from '@mui/material'; | ||
import FormControl from '@mui/material/FormControl'; | ||
import * as React from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
import { HiLanguage } from 'react-icons/hi2'; | ||
|
||
import i18n from '@/app/(public)/i18n'; | ||
|
||
export interface LanguageSwitcherProps { | ||
languages: LanguageDef[]; | ||
} | ||
|
||
export interface LanguageDef { | ||
label: string; | ||
value: string; | ||
disabled?: boolean; | ||
} | ||
|
||
const LanguageSwitcher = ({ languages }: LanguageSwitcherProps) => { | ||
const [_, setMounted] = useState(false); | ||
|
||
// When mounted on client, now we can show the UI | ||
useEffect(() => setMounted(true), []); | ||
|
||
const [chosenLanguage, setChosenLanguage] = useState(''); | ||
|
||
const handleChange = (event: SelectChangeEvent) => { | ||
const newLang = event.target.value; | ||
setChosenLanguage(newLang); | ||
|
||
i18n.changeLanguage(newLang); | ||
}; | ||
|
||
const label = <HiLanguage />; | ||
|
||
return ( | ||
<div> | ||
<FormControl | ||
size='small' | ||
className='ring-offset-2' | ||
sx={{ | ||
m: 1, | ||
minWidth: 95, | ||
'& .MuiFormLabel-root': { | ||
fontSize: '1.5rem', | ||
}, | ||
'& .MuiOutlinedInput-notchedOutline': { | ||
borderWidth: '2px', | ||
borderColor: '#E5E7EB', | ||
borderRadius: '0.375rem', | ||
}, | ||
'&:hover .MuiOutlinedInput-notchedOutline': { | ||
borderColor: '#2562E8 !important', | ||
}, | ||
}} | ||
> | ||
<InputLabel id='demo-select-small-label'>{label}</InputLabel> | ||
<Select | ||
id='demo-select-small' | ||
labelId='demo-select-small-label' | ||
value={chosenLanguage} | ||
label={label} | ||
onChange={handleChange} | ||
sx={{ | ||
'& .MuiFormLabel-root': { | ||
fontSize: '1.5rem', | ||
}, | ||
}} | ||
> | ||
{languages.map( | ||
(lang: LanguageDef) => | ||
!lang.disabled && ( | ||
<MenuItem | ||
className='font-light' | ||
style={{ fontSize: '14px', fontWeight: 300 }} | ||
key={lang.value} | ||
value={lang.value} | ||
> | ||
{lang.label} | ||
</MenuItem> | ||
), | ||
)} | ||
</Select> | ||
</FormControl> | ||
</div> | ||
); | ||
}; | ||
|
||
export default LanguageSwitcher; |
53 changes: 53 additions & 0 deletions
53
src/components/atoms/LanguageSwitcher/LanguageSwitcherMobile.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Image from 'next/image'; | ||
import * as React from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
import i18n from '@/app/(public)/i18n'; | ||
|
||
export interface LanguageSwitcherProps { | ||
languages: LanguageDef[]; | ||
} | ||
|
||
export interface LanguageDef { | ||
label: string; | ||
value: string; | ||
flag: string; | ||
disabled?: boolean; | ||
} | ||
|
||
const LanguageSwitcherMobile = ({ languages }: LanguageSwitcherProps) => { | ||
const [_, setMounted] = useState(false); | ||
|
||
// When mounted on client, now we can show the UI | ||
useEffect(() => setMounted(true), []); | ||
|
||
const [__, setChosenLanguage] = useState(''); | ||
|
||
const handleChange = (newLang: string) => { | ||
setChosenLanguage(newLang); | ||
i18n.changeLanguage(newLang); | ||
}; | ||
|
||
const flagSize = 20; | ||
|
||
return ( | ||
<div className='flex flex-row'> | ||
{languages.map( | ||
(lang) => | ||
!lang.disabled && ( | ||
<Image | ||
key={lang.value} | ||
className='mx-2 hover:drop-shadow-md' | ||
src={lang.flag} | ||
alt={lang.value} | ||
height={flagSize} | ||
width={flagSize * 1.5} | ||
onClick={() => handleChange(lang.value)} | ||
/> | ||
), | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default LanguageSwitcherMobile; |
45 changes: 45 additions & 0 deletions
45
src/components/atoms/LanguageSwitcher/__stories__/LanguageSwitcher.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Meta } from '@storybook/react'; | ||
|
||
import LanguageSwitcher, { | ||
LanguageDef, | ||
LanguageSwitcherProps, | ||
} from '@/components/atoms/LanguageSwitcher/LanguageSwitcher'; | ||
|
||
const meta: Meta<typeof LanguageSwitcher> = { | ||
title: 'LanguageSwitcher', | ||
component: LanguageSwitcher, | ||
tags: ['autodocs'], | ||
}; | ||
|
||
export default meta; | ||
|
||
const languages: LanguageDef[] = [ | ||
{ | ||
label: '🇬🇧 EN', | ||
value: 'en', | ||
}, | ||
{ | ||
label: '🇮🇹 IT', | ||
value: 'it', | ||
}, | ||
{ | ||
label: '🇩🇪 DE', | ||
value: 'de', | ||
}, | ||
{ | ||
label: '🇫🇷 FR', | ||
value: 'fr', | ||
}, | ||
{ | ||
label: '🇪🇸 ES', | ||
value: 'es', | ||
disabled: true, | ||
}, | ||
]; | ||
|
||
export const SampleStory = (args: LanguageSwitcherProps) => { | ||
return <LanguageSwitcher {...args} />; | ||
}; | ||
SampleStory.args = { | ||
languages, | ||
}; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { GlobalStyles } from '@mui/material'; | ||
import { CssBaseline, ThemeProvider } from '@mui/material'; | ||
import { useTheme } from 'next-themes'; | ||
import { FC, useEffect, useState } from 'react'; | ||
|
||
import { darkTheme, globalStyles, lightTheme } from '@/theme'; | ||
|
||
const MUIThemeProvider: FC<{ children: React.ReactNode }> = ({ children }) => { | ||
const { resolvedTheme } = useTheme(); | ||
const [currentTheme, setCurrentTheme] = useState(lightTheme); | ||
|
||
useEffect(() => { | ||
resolvedTheme === 'light' | ||
? setCurrentTheme(lightTheme) | ||
: setCurrentTheme(darkTheme); | ||
}, [resolvedTheme]); | ||
|
||
return ( | ||
<ThemeProvider theme={currentTheme}> | ||
<CssBaseline /> | ||
<GlobalStyles styles={globalStyles} /> | ||
{children} | ||
</ThemeProvider> | ||
); | ||
}; | ||
|
||
export default MUIThemeProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
4a01606
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
martacodes-it – ./
martacodes-it-martapanc.vercel.app
martacodes-it.vercel.app
martacodes-it-git-master-martapanc.vercel.app
www.martacodes.it
martacodes.it