Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web): google-translate #1763

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion web/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,25 @@ declare global {
const path: string;
export default path;
}
interface Window {
google: {
translate: {
TranslateElement: new (
config: {
pageLanguage: string;
includedLanguages: string;
},
elementId: string
) => void;
Harman-singh-waraich marked this conversation as resolved.
Show resolved Hide resolved
};
};
googleTranslateElementInit: () => void;
}
}

declare module "styled-components" {
type Theme = typeof lightTheme;
//eslint-disable-next-line @typescript-eslint/no-empty-interface

export interface DefaultTheme extends Theme {}
}

Expand Down
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
34 changes: 34 additions & 0 deletions web/src/components/TranslateDropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from "react";

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

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

const Langs: { value: SupportedLangs; text: string }[] = [
{ text: "English", value: "en" },
{ text: "Spanish", value: "es" },
{ text: "Hindi", value: "hi" },
{ text: "Japanese", value: "ja" },
{ text: "Korean", value: "ko" },
{ text: "French", value: "fr" },
];

const TranslateDropdown: React.FC = () => {
const { currentLang, setLang } = useTranslate();
return (
<DropdownSelect
smallButton
simpleButton
items={Langs.map((range) => ({
value: range.value,
text: range.text,
}))}
defaultValue={currentLang}
callback={(val) => {
setLang(val as SupportedLangs);
}}
/>
);
};

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

import { useLocalStorage } from "hooks/useLocalStorage";

export type SupportedLangs = "en" | "es" | "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 {
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);

window.googleTranslateElementInit = () => {
new window.google.translate.TranslateElement(
{
pageLanguage: "en",
includedLanguages: "en,es,hi,ja,fr,ko",
},
"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=/";
if (window.google?.translate?.TranslateElement) {
const select = document.querySelector(".goog-te-combo") as HTMLSelectElement;
if (select) {
select.value = cValue;

select.dispatchEvent(new Event("change", { bubbles: true }));
select.click();
}
}
},
[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
12 changes: 12 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,15 @@ export const GlobalStyle = createGlobalStyle`
.hiddenCanvasElement{
display: none;
}

//hide-default-google-translate
.skiptranslate{
display: none !important ;
visibility: hidden !important;
};

font{
background-color: transparent !important;
box-shadow: none !important ;
}
`;
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
Loading