Skip to content

Commit

Permalink
Merge pull request WildMeOrg#604 from WildMeOrg/590_react_lang_select…
Browse files Browse the repository at this point in the history
…ion_persist

590 react lang selection persist
  • Loading branch information
TanyaStere42 authored Jun 12, 2024
2 parents 28a9f9d + 1ddbc05 commit 42c155d
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 66 deletions.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
5 changes: 4 additions & 1 deletion frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import FrontDesk from "./FrontDesk";
import { BrowserRouter, useLocation, useRoutes } from "react-router-dom";
import LocaleContext from "./IntlProvider";
import FooterVisibilityContext from "./FooterVisibilityContext";
import Cookies from "js-cookie";

function App() {
const messageMap = {
Expand All @@ -19,7 +20,8 @@ function App() {
fr: messagesFr,
it: messagesIt,
};
const [locale, setLocale] = useState("en");
const initialLocale = Cookies.get("wildbookLangCode") || "en";
const [locale, setLocale] = useState(initialLocale);
const [visible, setVisible] = useState(true);
const containerStyle = {
maxWidth: "1440px",
Expand All @@ -29,6 +31,7 @@ function App() {

const handleLocaleChange = (newLocale) => {
setLocale(newLocale);
Cookies.set("wildbookLangCode", newLocale);
};

return (
Expand Down
105 changes: 50 additions & 55 deletions frontend/src/components/navBar/MultiLanguageDropdown.jsx
Original file line number Diff line number Diff line change
@@ -1,59 +1,54 @@
import React, { useContext, useState }from 'react';
import Dropdown from 'react-bootstrap/Dropdown';
import DownIcon from '../svg/DownIcon';
import LocaleContext from '../../IntlProvider';
import { locales, localeMap, languageMap } from '../../constants/locales';
import React, { useContext, useState } from "react";
import Dropdown from "react-bootstrap/Dropdown";
import DownIcon from "../svg/DownIcon";
import LocaleContext from "../../IntlProvider";
import { locales, localeMap, languageMap } from "../../constants/locales";
import Cookies from "js-cookie";

export default function MultiLanguageDropdown() {
const { onLocaleChange } = useContext(LocaleContext);
const initialLocale = Cookies.get("wildbookLangCode") || "en";
const [flag, setFlag] = useState(initialLocale);
return (
<div
className="d-flex align-items-center justify-content-center border-0 rounded-pill m-2"
style={{
backgroundColor: "rgba(255, 255, 255, 0.25)",
minWidth: "65px",
height: "35px",
}}
>
<Dropdown>
<Dropdown.Toggle variant="basic" id="dropdown-basic">
<img
src={`/react/flags/${flag}.png`}
alt="flag"
style={{ width: "20px", height: "12px" }}
/>
<span style={{ paddingLeft: 7 }}>
<DownIcon />
</span>
</Dropdown.Toggle>

const { onLocaleChange } = useContext(LocaleContext);
const [ flag, setFlag ] = useState('UK');
return (
<div style={{
backgroundColor: 'rgba(255, 255, 255, 0.25)',
border: 'none',
borderRadius: '30px',
minWidth: '65px',
height: '35px',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
margin: '10px',
}}>

<Dropdown style={{
border: 'none',
color: 'white',
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',

}}>
<Dropdown.Toggle variant="basic" id="dropdown-basic" >
<img src={`/react/flags/${flag}.png`} alt="flag" style={{width: '20px', height: '12px'}} />
<span style={{paddingLeft: 7}}><DownIcon /></span>
</Dropdown.Toggle>

<Dropdown.Menu>
{
locales.map((locale, index) => (
<Dropdown.Item
key={index}
onClick={() => {
onLocaleChange(locale);
setFlag(localeMap[locale]);
}}>
<img
src={`/react/flags/${localeMap[locale]}.png`} alt={locale}
style={{width: '20px', height: '12px', marginRight: '10px'}} />
{languageMap[locale]}
</Dropdown.Item>
))
}
</Dropdown.Menu>
</Dropdown>
</div>
)
<Dropdown.Menu>
{locales.map((locale, index) => (
<Dropdown.Item
key={index}
onClick={() => {
onLocaleChange(locale);
setFlag(localeMap[locale]);
}}
>
<img
src={`/react/flags/${localeMap[locale]}.png`}
alt={locale}
style={{ width: "20px", height: "12px", marginRight: "10px" }}
/>
{languageMap[locale]}
</Dropdown.Item>
))}
</Dropdown.Menu>
</Dropdown>
</div>
);
}

19 changes: 9 additions & 10 deletions frontend/src/constants/locales.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@

const locales = ['en', 'es', 'fr', 'it'];
const locales = ["en", "es", "fr", "it"];
const localeMap = {
en: 'UK',
es: 'ES',
fr: 'FR',
it: 'IT',
en: "en",
es: "es",
fr: "fr",
it: "it",
};

const languageMap = {
en: 'English',
es: 'Español',
fr: 'Francés',
it: 'Italiano',
en: "English",
es: "Español",
fr: "Francés",
it: "Italiano",
};

export { locales, localeMap, languageMap };

0 comments on commit 42c155d

Please sign in to comment.