Last active
June 8, 2024 05:29
-
-
Save maou-shonen/506afc1e7f796ebe0cfc497ada15f50d to your computer and use it in GitHub Desktop.
Google translate website refer https://stackoverflow.com/a/77475162/17424201
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
export default App() { | |
useGoogleTranslate() | |
return ( | |
<> | |
<div key="GoogleTranslate" id="google_translate_element" /> | |
</> | |
) | |
} |
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
/* If you want to hide the toolbar */ | |
body div.skiptranslate iframe { | |
display: none; | |
} |
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
declare global { | |
interface Window { | |
google: { | |
translate: { | |
// TranslateElement: ObjectConstructor | |
TranslateElement: any | |
} | |
} | |
} | |
} | |
function useGoogleTranslate(elementId = 'google_translate_element') { | |
const googleTranslateElementInit = () => { | |
new window.google.translate.TranslateElement( | |
{ | |
pageLanguage: 'en', | |
includedLanguages: 'zh-TW,zh-CN,en', | |
layout: window.google.translate.TranslateElement.InlineLayout.SIMPLE, | |
}, | |
elementId, | |
) | |
} | |
Object.assign(window, { | |
[googleTranslateElementInit.name]: googleTranslateElementInit, | |
}) | |
useEffect(() => { | |
const addScript = document.createElement('script') | |
addScript.setAttribute( | |
'src', | |
`//translate.google.com/translate_a/element.js?cb=${googleTranslateElementInit.name}`, | |
) | |
document.body.append(addScript) | |
}, []) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment