Skip to content

Commit

Permalink
feat(web): google-translate
Browse files Browse the repository at this point in the history
  • Loading branch information
Harman-singh-waraich committed Nov 30, 2024
1 parent 49932e2 commit 1c20e01
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 62 deletions.
125 changes: 64 additions & 61 deletions web/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Web3Provider from "context/Web3Provider";
import Loader from "components/Loader";
import Layout from "layout/index";

import TranslateProvider from "./context/TranslateProvider";
import { SentryRoutes } from "./utils/sentry";

const App: React.FC = () => {
Expand All @@ -33,67 +34,69 @@ const App: React.FC = () => {
<AtlasProvider>
<IsListProvider>
<NewDisputeProvider>
<SentryRoutes>
<Route path="/" element={<Layout />}>
<Route
index
element={
<Suspense fallback={<Loader width={"48px"} height={"48px"} />}>
<Home />
</Suspense>
}
/>
<Route
path="cases/*"
element={
<Suspense fallback={<Loader width={"48px"} height={"48px"} />}>
<Cases />
</Suspense>
}
/>
<Route
path="courts/*"
element={
<Suspense fallback={<Loader width={"48px"} height={"48px"} />}>
<Courts />
</Suspense>
}
/>
<Route
path="dashboard/:page/:order/:filter"
element={
<Suspense fallback={<Loader width={"48px"} height={"48px"} />}>
<Dashboard />
</Suspense>
}
/>
<Route
path="resolver/*"
element={
<Suspense fallback={<Loader width={"48px"} height={"48px"} />}>
<DisputeResolver />
</Suspense>
}
/>
<Route
path="get-pnk/*"
element={
<Suspense fallback={<Loader width={"48px"} height={"48px"} />}>
<GetPnk />
</Suspense>
}
/>
<Route
path="settings/*"
element={
<Suspense fallback={<Loader width={"48px"} height={"48px"} />}>
<Settings />
</Suspense>
}
/>
<Route path="*" element={<h1>Justice not found here ¯\_( ͡° ͜ʖ ͡°)_/¯</h1>} />
</Route>
</SentryRoutes>
<TranslateProvider>
<SentryRoutes>
<Route path="/" element={<Layout />}>
<Route
index
element={
<Suspense fallback={<Loader width={"48px"} height={"48px"} />}>
<Home />
</Suspense>
}
/>
<Route
path="cases/*"
element={
<Suspense fallback={<Loader width={"48px"} height={"48px"} />}>
<Cases />
</Suspense>
}
/>
<Route
path="courts/*"
element={
<Suspense fallback={<Loader width={"48px"} height={"48px"} />}>
<Courts />
</Suspense>
}
/>
<Route
path="dashboard/:page/:order/:filter"
element={
<Suspense fallback={<Loader width={"48px"} height={"48px"} />}>
<Dashboard />
</Suspense>
}
/>
<Route
path="resolver/*"
element={
<Suspense fallback={<Loader width={"48px"} height={"48px"} />}>
<DisputeResolver />
</Suspense>
}
/>
<Route
path="get-pnk/*"
element={
<Suspense fallback={<Loader width={"48px"} height={"48px"} />}>
<GetPnk />
</Suspense>
}
/>
<Route
path="settings/*"
element={
<Suspense fallback={<Loader width={"48px"} height={"48px"} />}>
<Settings />
</Suspense>
}
/>
<Route path="*" element={<h1>Justice not found here ¯\_( ͡° ͜ʖ ͡°)_/¯</h1>} />
</Route>
</SentryRoutes>
</TranslateProvider>
</NewDisputeProvider>
</IsListProvider>
</AtlasProvider>
Expand Down
36 changes: 36 additions & 0 deletions web/src/components/TranslateDropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from "react";

import { DropdownSelect } from "@kleros/ui-components-library";

import { SupportedLangs, useTranslate } from "context/TranslateProvider";

const Langs: { value: SupportedLangs; text: string }[] = [
{ text: "en", value: "en" },
{ text: "es", value: "es" },
{ text: "hi", value: "hi" },
{ text: "js", value: "ja" },
{ text: "zh", value: "zh" },
{ text: "ko", value: "ko" },
{ text: "fr", value: "fr" },
];

const TranslateDropdown: React.FC = () => {
const { setLang } = useTranslate();
return (
<DropdownSelect
smallButton
simpleButton
items={Langs.map((range) => ({
value: range.value,
text: range.text,
}))}
defaultValue={"en"}
callback={(val) => {
//@ts-expect-error is string
setLang(val);
}}
/>
);
};

export default TranslateDropdown;
68 changes: 68 additions & 0 deletions web/src/context/TranslateProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React, { createContext, useCallback, useContext, useEffect, useMemo } from "react";

import { useLocalStorage } from "hooks/useLocalStorage";

export type SupportedLangs = "en" | "es" | "zh" | "fr" | "hi" | "ko" | "ja";

interface ITranslate {
currentLang: SupportedLangs;
setLang: (lang: SupportedLangs) => void;
}
const TranslateContext = createContext<ITranslate | undefined>(undefined);

export const useTranslate = () => {
const context = useContext(TranslateContext);
if (!context) {
throw new Error("Context Provider not found.");
}
return context;
};

export const TranslateProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [currentLang, setCurrentLang] = useLocalStorage<SupportedLangs>("lang", "en");

useEffect(() => {
try {
// Check if the Google Translate script is already in the document
const existingScript = document.querySelector('script[src*="translate.google.com/translate_a/element.js"]');
if (!existingScript) {
const addScript = document.createElement("script");
addScript.setAttribute("src", "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit");
document.body.appendChild(addScript);

//@ts-expect-error will exist
window.googleTranslateElementInit = () => {
//@ts-expect-error will exist
new window.google.translate.TranslateElement(
{
pageLanguage: "en",
includedLanguages: "en,es,hi,ja,zh,fr,ko", // Include all languages you need here
},
"google_translate_element"
);
};
}
} catch (err) {
// eslint-disable-next-line no-console
console.log("Error injecting google translate script", err);
}
}, []);

const setLang = useCallback(
(cValue: SupportedLangs) => {
setCurrentLang(cValue);
document.cookie = "googtrans" + "=" + `/en/${cValue}` + ";" + "Session" + ";path=/";
window.location.reload();
},
[setCurrentLang]
);

return (
<TranslateContext.Provider value={useMemo(() => ({ currentLang, setLang }), [currentLang, setLang])}>
<div id="google_translate_element"></div>
{children}
</TranslateContext.Provider>
);
};

export default TranslateProvider;
6 changes: 6 additions & 0 deletions web/src/layout/Header/navbar/Menu/Settings/General.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import { Button } from "@kleros/ui-components-library";

import { AddressOrName, ChainDisplay, IdenticonOrAvatar } from "components/ConnectWallet/AccountDisplay";
import { EnsureChain } from "components/EnsureChain";
import TranslateDropdown from "components/TranslateDropdown";

const Container = styled.div`
display: flex;
flex-direction: column;
justify-content: center;
margin-top: 12px;
align-items: center;
`;

const StyledChainContainer = styled.div`
Expand Down Expand Up @@ -57,6 +59,9 @@ const StyledButton = styled.div`

const EnsureChainContainer = styled.div`
display: flex;
flex-direction: column;
align-items: center;
gap: 16px;
justify-content: center;
padding: 16px;
`;
Expand Down Expand Up @@ -93,6 +98,7 @@ const General: React.FC = () => {

return (
<EnsureChainContainer>
<TranslateDropdown />
<EnsureChain>
<Container>
{address && (
Expand Down
7 changes: 7 additions & 0 deletions web/src/styles/global-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const GlobalStyle = createGlobalStyle`
body {
font-family: "Open Sans", sans-serif;
margin: 0px;
top:0px !important
}
html {
Expand Down Expand Up @@ -120,4 +121,10 @@ export const GlobalStyle = createGlobalStyle`
.hiddenCanvasElement{
display: none;
}
//hide-default-google-translate
.skiptranslate{
display: none;
};
`;
3 changes: 2 additions & 1 deletion web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
"noImplicitAny": false,
"resolveJsonModule": true,
"lib": [
"ESNext.Array"
"ESNext.Array",
"DOM"
],
"types": [
"vite/client",
Expand Down

0 comments on commit 1c20e01

Please sign in to comment.