-
Notifications
You must be signed in to change notification settings - Fork 49
feat(web): google-translate #1763
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
Draft
tractorss
wants to merge
7
commits into
dev
Choose a base branch
from
feat/google-translate
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1c20e01
feat(web): google-translate
tractorss 1d59d83
feat(web): instant-translate
tractorss 246c26a
chore: remove-dropwdown-from-menu
tractorss e023c06
chore(web): remove-chinese-language
tractorss b8933ac
fix(web): hide-google-translate-tooltip
tractorss 73ce498
chore(web): no-translate-addresses
tractorss 8b046ba
Merge branch 'dev' into feat/google-translate
tractorss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,35 @@ | ||
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: "Chinese", value: "zh" }, | ||
{ text: "Korean", value: "ko" }, | ||
{ text: "French", 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) => { | ||
setLang(val as SupportedLangs); | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
export default TranslateDropdown; |
This file contains hidden or 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,71 @@ | ||
import React, { createContext, useCallback, useContext, useEffect, useMemo } from "react"; | ||
|
||
import { useLocalStorage } from "hooks/useLocalStorage"; | ||
|
||
export type SupportedLangs = "en" | "es" | "zh" | "fr" | "hi" | "ko" | "ja"; | ||
tractorss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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,zh,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")); | ||
} | ||
} | ||
}, | ||
[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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.