-
Notifications
You must be signed in to change notification settings - Fork 0
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 #37 from dsaltares/feat/32
feat: 32 - correct date localisation depending on system language
- Loading branch information
Showing
2 changed files
with
44 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { LocalizationProvider as LocalizationProviderBase } from '@mui/x-date-pickers/LocalizationProvider'; | ||
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV3'; | ||
import type { PropsWithChildren } from 'react'; | ||
import { enUS } from 'date-fns/locale/en-US'; | ||
import { enGB } from 'date-fns/locale/en-GB'; | ||
import { fr } from 'date-fns/locale/fr'; | ||
import { de } from 'date-fns/locale/de'; | ||
import { es } from 'date-fns/locale/es'; | ||
import { ro } from 'date-fns/locale/ro'; | ||
import type { Locale } from 'date-fns'; | ||
|
||
const LocaleMap: Record<string, Locale | undefined> = { | ||
'en-US': enUS, | ||
'en-GB': enGB, | ||
fr: fr, | ||
de: de, | ||
es: es, | ||
ro: ro, | ||
}; | ||
|
||
export default function LocalizationProvider({ children }: PropsWithChildren) { | ||
return ( | ||
<LocalizationProviderBase | ||
dateAdapter={AdapterDateFns} | ||
adapterLocale={getLocale()} | ||
> | ||
{children} | ||
</LocalizationProviderBase> | ||
); | ||
} | ||
|
||
function getLocale() { | ||
const locale = navigator.language; | ||
if (locale in LocaleMap) { | ||
return LocaleMap[locale]; | ||
} | ||
const language = locale.split('-')[0]; | ||
if (language in LocaleMap) { | ||
return LocaleMap[language]; | ||
} | ||
return enGB; | ||
} |