From 4a15b4bf76100c01249a771a73c9f882db7c384c Mon Sep 17 00:00:00 2001 From: codevictory Date: Wed, 12 Jan 2022 18:57:32 +0200 Subject: [PATCH] Set language from URL. --- src/pages/Confirmation.tsx | 11 ++++++++++- src/pages/Main.tsx | 10 +++++++++- src/pages/Registration.tsx | 11 ++++++++++- src/pages/utils.ts | 9 +++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/pages/utils.ts diff --git a/src/pages/Confirmation.tsx b/src/pages/Confirmation.tsx index 5b45dc9..3dd3173 100644 --- a/src/pages/Confirmation.tsx +++ b/src/pages/Confirmation.tsx @@ -1,8 +1,17 @@ import { FormattedMessage } from 'react-intl'; -import { Link } from 'react-router-dom'; +import { Link, useLocation } from 'react-router-dom'; +import { useSetRecoilState } from 'recoil'; +import { currentLanguage } from '../atoms/language'; import './Confirmation.scss'; +import { getLangFromSearch } from './utils'; export const Confirmation = () => { + const { search } = useLocation(); + const setlang = useSetRecoilState(currentLanguage); + + const lang = getLangFromSearch(search); + if (lang != '') setlang(lang); + return (

diff --git a/src/pages/Main.tsx b/src/pages/Main.tsx index 50f54f2..793e216 100644 --- a/src/pages/Main.tsx +++ b/src/pages/Main.tsx @@ -1,6 +1,6 @@ import './Main.scss'; -import { Link, useParams } from 'react-router-dom'; +import { Link, useParams, useLocation } from 'react-router-dom'; import { Menu } from '../components/Menu'; import { NavBar } from '../components/NavBar'; import { Program } from '../components/Program'; @@ -9,6 +9,9 @@ import { LanguagePicker } from '../components/LanguagePicker'; import { FormattedMessage } from 'react-intl'; import { DateAndPlace } from '../components/DateAndPlace'; import { Info } from '../components/Info'; +import { getLangFromSearch } from './utils'; +import { useSetRecoilState } from 'recoil'; +import { currentLanguage } from '../atoms/language'; interface MainParams { page: string; @@ -16,6 +19,11 @@ interface MainParams { export const Main = () => { const { page } = useParams(); + const { search } = useLocation(); + const setlang = useSetRecoilState(currentLanguage); + + const lang = getLangFromSearch(search); + if (lang != '') setlang(lang); return (
diff --git a/src/pages/Registration.tsx b/src/pages/Registration.tsx index bd912a6..f179ee3 100644 --- a/src/pages/Registration.tsx +++ b/src/pages/Registration.tsx @@ -4,9 +4,18 @@ import { FormattedMessage } from 'react-intl'; import RegForm from '../components/RegForm'; import { RegistrationHeader } from '../components/RegistrationHeader'; import { LanguagePicker } from '../components/LanguagePicker'; -import { Link } from 'react-router-dom'; +import { Link, useLocation } from 'react-router-dom'; +import { getLangFromSearch } from './utils'; +import { useSetRecoilState } from 'recoil'; +import { currentLanguage } from '../atoms/language'; export const Registration = () => { + const { search } = useLocation(); + const setlang = useSetRecoilState(currentLanguage); + + const lang = getLangFromSearch(search); + if (lang != '') setlang(lang); + return ( <> diff --git a/src/pages/utils.ts b/src/pages/utils.ts new file mode 100644 index 0000000..576c100 --- /dev/null +++ b/src/pages/utils.ts @@ -0,0 +1,9 @@ +export const getLangFromSearch = (search: string) => { + const param = search.substring(0,6); + const lang = search.substring(6,8); + + if(param === '?lang=' && (lang === 'en' || lang === 'fi' || lang === 'sk')) { + return lang; + } + return ""; +} \ No newline at end of file