handling country label according to current language

This commit is contained in:
Antoine M 2024-02-21 11:04:29 +01:00
parent 31ae2116ac
commit 896d90121b

View File

@ -1,10 +1,11 @@
import { createContext, useEffect, useState } from "react";
import CountriesJSON from "../data/countries/countries.json";
export const UserContext = createContext();
export function UserContextProvider({ children }) {
const [language, setLanguage] = useState("FR");
const [country, setCountry] = useState({ name: "belgique", iso: "BE" });
const [country, setCountry] = useState({ name: "belgique", iso: "BE", label: "Belgique" });
const [profile, setProfile] = useState("lhoist_employee");
const [screensTranslations, setScreensTranslations] = useState({});
@ -21,6 +22,12 @@ export function UserContextProvider({ children }) {
// Appeler la fonction loadTranslations avec la langue actuelle
loadTranslations(language);
// ADAPTING COUNTRY LABEL TO LANGUAGE
const currentCountry = CountriesJSON.find(
(countryJson) => countryJson.alpha2 === country.iso.toLowerCase()
);
setCountry({ ...country, label: currentCountry[language.toLowerCase()] });
}, [language]);
function changeLanguage() {
@ -45,6 +52,7 @@ export function UserContextProvider({ children }) {
screensTranslations,
changeLanguage,
country,
setLanguage,
changeCountry,
profile,
changeProfile,