-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49932e2
commit 1c20e01
Showing
6 changed files
with
183 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters