Skip to content

Commit

Permalink
Language feature Implementation (#137)
Browse files Browse the repository at this point in the history
* feat: Added language menu item on side nav

* file: added package-lock json file

* packages: Added packages for language translation

* package: added new yarn package

* feat: remove commented links

* file: Added translation file

* feat: Added translation for loading screen.

* feat: added radio button for switching languages

* feat: added translation for links data item

* file: Added json file for english translation

* file: Added json file for french translation

* feat: added translation feature

* feat: Make used of translation file

* feat: make used of translation file

* feat: added language selector

* feat: Added translation for pages
  • Loading branch information
godsakani authored Sep 25, 2022
1 parent b1dc6e5 commit fd8a661
Show file tree
Hide file tree
Showing 19 changed files with 1,607 additions and 47 deletions.
4 changes: 4 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
"dependencies": {
"axios": "^0.22.0",
"eslint": "^7.11.0",
"i18next": "^21.9.0",
"i18next-browser-languagedetector": "^6.1.5",
"i18next-http-backend": "^1.4.1",
"leaflet": "^1.7.1",
"rc-drawer": "^4.4.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-hooks-use-modal": "^2.1.0",
"react-i18next": "^11.18.3",
"react-leaflet": "^3.2.2",
"react-modern-drawer": "^0.1.1",
"react-router-dom": "^5.3.0",
Expand Down
3 changes: 1 addition & 2 deletions frontend/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<body style="background: #262626">
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!-- <link href='https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.3/leaflet.css' rel='stylesheet'> -->
<link rel="stylesheet" href="leaflet.css" />
<link rel="stylesheet" href="leaflet.css" />
</body>
</html>
9 changes: 4 additions & 5 deletions frontend/src/assets/data/Items.data.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { LANGUAGE } from "../../constants/language";
import home from "../img/home.png";
import conseils from "../img/conseils.png";
import listes from "../img/liste.png";
import language from "../img/language.png";
import report from "../img/alert.png";
import home from "../img/home.png"
import conseils from "../img/conseils.png"
import listes from "../img/liste.png"
import language from "../img/language.png"

export const ITEMS = [
{
Expand Down
14 changes: 8 additions & 6 deletions frontend/src/assets/data/Links.data.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import i18n from "../../i18n";

export const LINKS = [
{
title: "Que faire en cas de panne de courant ",
title: i18n.t("adviceTitle2"),
content: [
"S'agit-il d'une panne de courant qui n'affecte que les appareils individuels ? Vos voisins ont également eu une coupure de courant ? Il est possible qu'un seul fusible ait grillé et que vous puissiez résoudre le problème vous-même.",
i18n.t("adviceContent"),
]
},
{
title: "Comportement après une panne de courant ",
title:i18n.t( "adviceTitle3"),
content: [
"Dès que le courant est rétabli, dans la plupart des cas, il y a d'abord un soulagement. Mais il y a certaines choses que vous devez garder à l'esprit même après une panne de courant.",
i18n.t("adviceContent1"),
]
},
{
title: "Raisons d'une panne de courant ",
title: i18n.t("adviceTitle4"),
content: [
"En cas de panne de courant, le plus important est de rétablir l'alimentation. Pour la plupart des personnes touchées par une panne de courant, cela peut être réalisé au moyen de mesures de commutation dans le réseau électrique. L'opérateur du réseau ne connaît souvent la raison exacte de la panne de courant qu'un certain temps plus tard.",
i18n.t("adviceContent2"),
]
}
]
34 changes: 34 additions & 0 deletions frontend/src/assets/translation/en/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"select_language": "Select Language",
"hello_welcome_to_react": "Hello, Welcome to React",
"this_is_an_example": "This is an example",
"please_enter_name": "Please enter your name",
"loading": "loading...",
"panel":"Electrical failure",
"reportOutage" :"Report a power outage",
"accept" : "accept",
"notification" : "This site uses cookies to make your visit to our site as pleasant as possible. We use your location to show you reports for your area.",
"info" : "Official Information on Network Operator Interference",
"title" : "Where is the current power outage? Network operators map",
"search" : "Enter the region",
"home" : "Home page",
"info1" : "Advice and information",
"view" : "List view",
"city" : "Enter your neighborhood",
"empty" : "No alerts for this region",
"location" : "show on map",
"notAvailable" : "This content is not available at the moment",
"warning" : "This problem usually stems from the owner only sharing it with a small group of people, changing who can see it, or deleting it.",
"homePage" : "Go to the home page",
"title1" : "Power outage, now what?",
"contents" : "A power outage is a sudden and rare event. However, in the event of a power outage, it is advisable to remain calm at first. In addition, you must pay attention to a few elements of your household and notify us of the power outage.",
"link" : "Quick links:",
"adviceContent" : "Is it a power outage that only affects individual devices? Have your neighbors also had a power outage? It is possible that only one fuse has blown and you can fix the problem yourself.",
"adviceContent1" : "As soon as the power is restored, in most cases there is relief first. But there are some things you need to keep in mind even after a power outage.",
"adviceContent2" : "In the event of a power outage, the most important thing is to restore power. For most people affected by a power outage, this can be achieved through switching measures in the power grid. The network operator often does not know the exact reason for the power failure until some time later.",
"adviceTitle2" : "What to do in the event of a power outage",
"adviceTitle3" : "Behavior after a power failure",
"adviceTitle4" : "Reasons for a power outage"

}

33 changes: 33 additions & 0 deletions frontend/src/assets/translation/fr/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"select_language": "Choisir la langue",
"hello_welcome_to_react": "Bonjour, Bienvenue chez React",
"this_is_an_example": "Ceci est un exemple",
"please_enter_name": "Veuillez saisir le nom",
"loading" : "Chargement.....",
"panel":"Panne électrique",
"reportOutage" :"Signaler une panne de courant",
"accept" : "J'accepte",
"notification" : "Ce site utilise des cookies pour rendre votre visite sur notre site aussi agréable que possible. Nous utilisons votre emplacement pour vous montrer les rapports de votre région.",
"info" : "Informations officielles sur Interférences des opérateurs de réseau",
"title" : "Où est la panne de courant actuelle? Carte des opérateurs de réseau",
"search" : "Entrez la region",
"home" : "Page d'accueil",
"info1" : "Conseils et informations",
"view" : "Vue liste",
"city" : "Entrer votre quartier",
"empty" : "Aucune alerte pour cette region",
"location" : "afficher sur la carte",
"notAvailable" : "Ce contenu n’est pas disponible pour le moment",
"warning" : "Ce problème vient généralement du fait que le propriétaire ne l’a partagé qu’avec un petit groupe de personnes, a modifié qui pouvait le voir ou l’a supprimé.",
"homePage" : "Acceder a la page d'accueil",
"title1" : "Panne de courant, et maintenant ? ",
"contents" : "Une panne de courant est un événement soudain et rare. Cependant, en cas de panne de courant, il est conseillé de rester calme dans un premier temps. De plus, vous devez faire attention à quelques éléments de votre foyer et nous signaler la panne de courant. ",
"link" : "Liens rapides:",
"adviceContent" : "S'agit-il d'une panne de courant qui n'affecte que les appareils individuels ? Vos voisins ont également eu une coupure de courant ? Il est possible qu'un seul fusible ait grillé et que vous puissiez résoudre le problème vous-même.",
"adviceContent1" : "Dès que le courant est rétabli, dans la plupart des cas, il y a d'abord un soulagement. Mais il y a certaines choses que vous devez garder à l'esprit même après une panne de courant.",
"adviceContent2" : "En cas de panne de courant, le plus important est de rétablir l'alimentation. Pour la plupart des personnes touchées par une panne de courant, cela peut être réalisé au moyen de mesures de commutation dans le réseau électrique. L'opérateur du réseau ne connaît souvent la raison exacte de la panne de courant qu'un certain temps plus tard.",
"advicetitle2" : "Que faire en cas de panne de courant ",
"advicetitle3" : "Comportement après une panne de courant ",
"advicetitle4" : "Raisons d'une panne de courant "
}

7 changes: 5 additions & 2 deletions frontend/src/components/cityDetail/CityDetail.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import city from "../../assets/img/city.png";
import home from "../../assets/img/home.png"
import { LANGUAGE } from "../../constants/language";
import { Link } from "react-router-dom";
import StreetMap from "../../pages/streetMap/StreetMap.page";
import { useTranslation } from "react-i18next";

export const CityDetail = (props: any) => {
const { t } = useTranslation();
return (
<div className="site__detail border mt-4 p-4 w-full">
<div className="site__detail-item flex items-center">
Expand Down Expand Up @@ -63,8 +66,8 @@ export const CityDetail = (props: any) => {
}&long=${props.long}`}>
<button className="px-4 text-white">
<p className="flex px-2 py-1" >
<img src={home} alt={LANGUAGE.button.location} className="w-5 h-auto flex items-center justify-center"/>
{LANGUAGE.button.location}
<img src={home} alt={t("location")} className="w-5 h-auto flex items-center justify-center"/>
{t("location")}
</p>
</button>
</Link>
Expand Down
11 changes: 6 additions & 5 deletions frontend/src/components/notFound/NotFound.component.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import React from "react";
import { Link } from "react-router-dom";
import error from "../../assets/img/error.png";
import { LANGUAGE } from "../../constants/language";
import { Footer } from "../footer/Footer.component";
import { MyText } from "../myText/MyText.component";
import { useTranslation } from "react-i18next";
import { Footer } from "../footer/Footer.component";

export const NotFound = () => {
const { t } = useTranslation();
return (
<div className="flex flex-col items-center justify-center h-screen site__notFound">
<img src={error} alt="error" className="w-auto h-1/4" />
<div className="w-full py-6 md:w-1/3">
<MyText
variant="title"
myText={LANGUAGE.alert.notavailable}
myText={t("notAvailable")}
myTextColor="text-ind"
myTextAlign="text-center"
/>
<MyText
variant="normal"
myText={LANGUAGE.alert.warning}
myText={t("warning")}
myTextColor="text-ind"
myTextAlign="text-center"
/>
Expand All @@ -29,7 +30,7 @@ export const NotFound = () => {
className="text-gray-300"
style={{ fontFamily: " 'Varela Round', sans-serif" }}
>
{LANGUAGE.notfound.homePage}
{t("homePage")}
</p>
</Link>
</div>
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/search/Search.component.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from "react";
import search from "../../assets/img/search.png";
import { LANGUAGE } from "../../constants/language";

export const Search = (props: any) => {
return (
Expand Down
38 changes: 38 additions & 0 deletions frontend/src/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import i18n from "i18next";
import { initReactI18next } from "react-i18next";

// Importing translation files

import translationEN from "./assets/translation/en/en.json";
import translationFR from "./assets/translation/fr/fr.json";

//Creating object with the variables of imported translation files
const resources = {
en: {
translation: translationEN,
},
fr: {
translation: translationFR,
},
};

//i18N Initialization

i18n.use(initReactI18next).init({
resources,
lng: "en", //default language
keySeparator: false,
interpolation: {
escapeValue: false,
},
});
i18n.init({
// ... other config

react: {
// Turn off the use of React Suspense
useSuspense: false
}
});

export default i18n;
9 changes: 5 additions & 4 deletions frontend/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ import "./index.css";
import reportWebVitals from "./reportWebVitals";
import { AppNavigation } from "./constants/navigations/App.navigation";
import { RecoilRoot } from "recoil";
import { LANGUAGE } from "./constants/language";
import './i18n';
import i18n from "./i18n";

ReactDOM.render(
<React.StrictMode>
<Suspense
<React.Suspense
fallback={
<p className="text-gray-300 flex justify-center items-center pt-40 md:pt-52">
{LANGUAGE.loading}
{i18n.t('loading')}
</p>
}
>
<RecoilRoot>
<AppNavigation />
</RecoilRoot>
</Suspense>
</React.Suspense>
</React.StrictMode>,
document.getElementById("root")
);
Expand Down
18 changes: 18 additions & 0 deletions frontend/src/languageSelect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react";
import { useTranslation } from "react-i18next";

const LanguageSelector = () => {

const { i18n } = useTranslation();

const changeLanguage = (event) => {
i18n.changeLanguage(event.target.value);
};
return (
<div onChange={changeLanguage}>
<input type="radio" value="en" name="language" defaultChecked /> English
<input type="radio" value="fr" name="language" /> French
</div>
);
};
export default LanguageSelector;
9 changes: 5 additions & 4 deletions frontend/src/pages/advices/Advices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { Accordion } from "../../components/accordion/Accordion.component";
import { MyDrawer } from "../../components/drawer/Drawer.component";
import {Footer} from "../../components/footer/Footer.component";
import { MyText } from "../../components/myText/MyText.component";
import { LANGUAGE } from "../../constants/language";
import { useTranslation } from "react-i18next";

const Advices = () => {
const {t} = useTranslation();
return (
<div className="site__conseils bg-cover w-auto h-screen">
<div className="px-4 md:px-20 pt-5 md:pt-0">
Expand All @@ -15,19 +16,19 @@ const Advices = () => {
<main className="site__main pt-8 px-4 md:px-40">
<MyText
variant="title"
myText={LANGUAGE.info.title}
myText={t('title1')}
myTextColor="text-white"
/>
<div className="w-full md:w-3/4 py-4">
<MyText
variant="normal"
myText={LANGUAGE.info.content}
myText={t('contents')}
myTextColor="text-white"
/>
</div>
<MyText
variant="normal"
myText={LANGUAGE.info.link}
myText={t('link')}
myTextColor="text-ind"
/>
{LINKS.map((i: any) => (
Expand Down
9 changes: 5 additions & 4 deletions frontend/src/pages/detail/Detail.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { MyText } from "../../components/myText/MyText.component";
import home from "../../assets/img/hotel.png";
import lists from "../../assets/img/list.png";
import { Search } from "../../components/search/Search.component";
import { LANGUAGE } from "../../constants/language";
import { Link } from "react-router-dom";
import boxImg from "../../assets/img/box.png";
import { getDetails } from "../../atoms/details";
import { useRecoilValue } from "recoil";
import { NotFound } from "../../components/notFound/NotFound.component";
import { useTranslation } from "react-i18next";

const Detail = () => {
const { t } = useTranslation();
const { search } = window.location;
const details: any = useRecoilValue(getDetails);
const query = new URLSearchParams(search).get("s");
Expand Down Expand Up @@ -49,7 +50,7 @@ const Detail = () => {
<main className="site__main pt-8 md:pt-20 px-4 pb-6 md:px-40 z-0">
<div className=" sticky z-50 top-0 left-0">
<Search
placeholder={LANGUAGE.list.city}
placeholder={t("city")}
searchQuery={searchQuery}
setSearchQuery={setSearchQuery}
/>
Expand All @@ -63,7 +64,7 @@ const Detail = () => {
}}
className="text-white pt-4"
>
{LANGUAGE.empty}
{t("empty")}
</p>
</div>
</main>
Expand Down Expand Up @@ -91,7 +92,7 @@ const Detail = () => {
<main className="site__main pt-8 md:pt-20 px-4 pb-6 md:px-40 z-0">
<div className=" sticky z-50 top-0 left-0">
<Search
placeholder={LANGUAGE.list.city}
placeholder={t("city")}
searchQuery={searchQuery}
setSearchQuery={setSearchQuery}
/>
Expand Down
Loading

0 comments on commit fd8a661

Please sign in to comment.