Skip to content

Commit

Permalink
feat(web): instant-translate
Browse files Browse the repository at this point in the history
  • Loading branch information
Harman-singh-waraich committed Nov 30, 2024
1 parent 1c20e01 commit 1d59d83
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
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;
};
};
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
17 changes: 8 additions & 9 deletions web/src/components/TranslateDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ 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" },
{ text: "English", value: "en" },
{ text: "Spanish", value: "es" },
{ text: "Hindi", value: "hi" },
{ text: "Japanese", value: "ja" },
{ text: "Chinese", value: "zh" },
{ text: "Korean", value: "ko" },
{ text: "French", value: "fr" },
];

const TranslateDropdown: React.FC = () => {
Expand All @@ -26,8 +26,7 @@ const TranslateDropdown: React.FC = () => {
}))}
defaultValue={"en"}
callback={(val) => {
//@ts-expect-error is string
setLang(val);
setLang(val as SupportedLangs);
}}
/>
);
Expand Down
13 changes: 8 additions & 5 deletions web/src/context/TranslateProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,17 @@ export const TranslateProvider: React.FC<{ children: React.ReactNode }> = ({ chi

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
includedLanguages: "en,es,hi,ja,zh,fr,ko",
},
"google_translate_element"
);
Expand All @@ -52,7 +49,13 @@ export const TranslateProvider: React.FC<{ children: React.ReactNode }> = ({ chi
(cValue: SupportedLangs) => {
setCurrentLang(cValue);
document.cookie = "googtrans" + "=" + `/en/${cValue}` + ";" + "Session" + ";path=/";
window.location.reload();
if (window.google?.translate?.TranslateElement) {
const select = document.querySelector(".goog-te-combo") as HTMLSelectElement;
if (select) {
select.value = cValue;
select.dispatchEvent(new Event("change"));
}
}
},
[setCurrentLang]
);
Expand Down
2 changes: 2 additions & 0 deletions web/src/layout/Header/navbar/Menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useToggleTheme } from "hooks/useToggleThemeContext";
import { landscapeStyle } from "styles/landscapeStyle";

import LightButton from "components/LightButton";
import TranslateDropdown from "components/TranslateDropdown";

import { IHelp, ISettings } from "..";

Expand Down Expand Up @@ -90,6 +91,7 @@ const Menu: React.FC<ISettings & IHelp> = ({ toggleIsHelpOpen, toggleIsSettingsO
<LightButton {...{ text, onClick, Icon }} />
</ButtonContainer>
))}
<TranslateDropdown />
</Container>
);
};
Expand Down

0 comments on commit 1d59d83

Please sign in to comment.