-
-
Notifications
You must be signed in to change notification settings - Fork 33
[WIP] Translate chat messages with Google #38
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
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
42a86eb
loading the lib in the extension doesn't work
KentoNishi b55c7e5
Starting over, probably with iframe memery
KentoNishi fcd4950
WIP
KentoNishi 4b68869
yay, it shows for each message!
KentoNishi da220bf
Use the online host
KentoNishi ff5acd8
use the iframe-translator package
KentoNishi 86048a5
bump translation package
KentoNishi cd769a2
fix scrolling issues after TL injection
KentoNishi 1089ee1
hover-to-show
KentoNishi b18b3c8
fixed reactive feedback loop when seeking video
KentoNishi 3ab6f96
shift the translation icon a little down
KentoNishi cc5394f
prepping for upgrade
KentoNishi 281eee6
gigantic merge of settings pr
KentoNishi c8cd8dc
CAN CHANGE THE LANGUAGE!
KentoNishi 5cca3f7
make lang select take effect immediately
KentoNishi 0e752cd
Chinese (Traditional) and Chinese (Simplified)
KentoNishi 75f14be
change dropdown label
KentoNishi a30b667
some formatting things
KentoNishi 596a679
WIP forceDark
KentoNishi 769e50c
Merge branch 'master' into google-translate
KentoNishi 26cf7af
vite compat changes
KentoNishi 640e2ed
WIP hover animation
KentoNishi 5280a97
click to restore original text
KentoNishi 5bc7402
scroll to bottom when expanding dropdown
KentoNishi 2eecd9a
scroll the page when the checkbox is clicked
KentoNishi bc3c350
Merge branch 'master' into google-translate
KentoNishi cfd0f63
Merge branch 'master' into google-translate
KentoNishi 7efa9ff
Merge branch 'master' into google-translate
KentoNishi 54628c5
import type { IframeTranslatorClient }
KentoNishi c26682c
show {run.text} for `deleted`
KentoNishi 5417595
<span>{run.text}</span>
KentoNishi 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,3 +28,4 @@ pnpm-debug.log* | |
| *.pub | ||
| *.zip | ||
| artifacts | ||
| .eslintcache | ||
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
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,72 @@ | ||
| <script lang="ts"> | ||
| import { refreshScroll, translatorClient, translateTargetLanguage } from '../ts/storage'; | ||
| import Icon from './common/Icon.svelte'; | ||
| import { fade } from 'svelte/transition'; | ||
|
|
||
| export let forceDark = false; | ||
|
|
||
| export let text: string; | ||
| let translatedMessage = ''; | ||
| let translatedLanguage = ''; | ||
| let showOriginal = false; | ||
|
|
||
| $: if ($translateTargetLanguage && $translatorClient) { | ||
| $translatorClient.translate(text, $translateTargetLanguage).then(result => { | ||
| if (result !== text) { | ||
| translatedLanguage = $translateTargetLanguage; | ||
| translatedMessage = result; | ||
| $refreshScroll = true; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| $: showTL = Boolean(translatedMessage && !showOriginal); | ||
|
|
||
| $: if ($translateTargetLanguage !== translatedLanguage) { | ||
| translatedMessage = ''; | ||
| translatedLanguage = ''; | ||
| } | ||
|
|
||
| const duration = 100; | ||
|
|
||
| $: translatedColor = forceDark ? 'text-translated-dark' : 'dark:text-translated-dark text-translated-light'; | ||
| $: stockTextColor = forceDark ? 'text-white' : 'dark:text-white text-black'; | ||
| </script> | ||
|
|
||
| <span | ||
| class={ | ||
| showTL ? translatedColor : stockTextColor | ||
| } | ||
| class:cursor-pointer={translatedMessage} | ||
| class:entrance-animation={translatedMessage} | ||
| on:click={() => { | ||
| if (translatedMessage) { | ||
| showOriginal = !showOriginal; | ||
| $refreshScroll = true; | ||
| } | ||
| }} | ||
| > | ||
| {#if !showTL} | ||
| <span in:fade={{ duration: translatedMessage ? duration : 0 }}> | ||
| {text} | ||
| </span> | ||
| {/if} | ||
| {#if showTL} | ||
| <span in:fade={{ duration }}> | ||
| {translatedMessage} | ||
| </span> | ||
| {/if} | ||
| {#if translatedMessage} | ||
| <span class="shifted-icon"> | ||
| <Icon xs={true} block={false}> | ||
| translate | ||
| </Icon> | ||
| </span> | ||
| {/if} | ||
| </span> | ||
|
|
||
| <style> | ||
| .shifted-icon :global(.material-icons) { | ||
| transform: translateY(1px); | ||
| } | ||
| </style> |
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,41 @@ | ||
| <script lang="ts"> | ||
| import type { Writable } from 'svelte/store'; | ||
| import Select from 'smelte/src/components/Select'; | ||
| import { getDropdownOffsetY } from '../../ts/component-utils'; | ||
| type DropdownItem = { value: string, text: string } | string; | ||
| /** Dropdown label. */ | ||
| export let name = ''; | ||
| /** Writable store for value updates. */ | ||
| export let store: Writable<string>; | ||
| /** Dropdown items. */ | ||
| export let items: DropdownItem[] = []; | ||
| /** Dense variant. */ | ||
| export let dense = false; | ||
| /** Parent div used to determine top/bottom */ | ||
| export let boundingDiv: HTMLElement | null = null; | ||
| $: value = $store; | ||
| $: store.set(value); | ||
| let showList = false; | ||
| let div: HTMLElement; | ||
| let offsetY = ''; | ||
| const onShowListChange = async (showList: boolean) => { | ||
| if (!showList || !boundingDiv) return; | ||
| offsetY = await getDropdownOffsetY(div, boundingDiv); | ||
| }; | ||
| $: onShowListChange(showList); | ||
| const classes = 'dropdown-wrapper cursor-pointer relative'; | ||
| $: optionsClasses = 'dropdown-options absolute left-0 bg-white rounded ' + | ||
| 'shadow w-full z-20 dark:bg-dark-500 max-h-60 overflow-auto ' + offsetY; | ||
| </script> | ||
|
|
||
| <div bind:this={div} class={$$props.class ? $$props.class : ''}> | ||
| <Select | ||
| bind:value | ||
| bind:showList | ||
| label={name} | ||
| {items} | ||
| {classes} | ||
| {dense} | ||
| {optionsClasses} | ||
| /> | ||
| </div> |
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,51 @@ | ||
| <script lang="ts"> | ||
| import { | ||
| translateTargetLanguage | ||
| } from '../../ts/storage'; | ||
| import DropdownStore from '../common/DropdownStore.svelte'; | ||
| import Card from '../common/Card.svelte'; | ||
| import Checkbox from '../common/CheckboxStore.svelte'; | ||
| import { AvailableLanguages } from 'iframe-translator'; | ||
| import { writable } from 'svelte/store'; | ||
| import { onMount, tick } from 'svelte'; | ||
| const enabled = writable(true); | ||
| $: if (!$enabled) { | ||
| $translateTargetLanguage = ''; | ||
| } | ||
| onMount(() => { | ||
| const unsub = translateTargetLanguage.subscribe(value => { | ||
| $enabled = Boolean(value); | ||
| setTimeout(() => unsub(), 0); | ||
| }); | ||
| }); | ||
| const priority = [ | ||
| 'English', | ||
| 'Japanese', | ||
| 'Indonesian', | ||
| 'Korean', | ||
| 'Spanish', | ||
| 'Russian', | ||
| 'French', | ||
| 'Chinese (Traditional)', | ||
| 'Chinese (Simplified)' | ||
| ]; | ||
|
|
||
| async function scrollToBottom() { | ||
| await tick(); | ||
| window.scrollTo(0, document.body.scrollHeight); | ||
| } | ||
| </script> | ||
|
|
||
| <Card title="Additional Options" icon="tune"> | ||
| <span on:click={scrollToBottom}> | ||
| <Checkbox name="Translate chat messages with Google Translate" store={enabled} /> | ||
| {#if $enabled} | ||
| <DropdownStore name="Target language" | ||
| store={translateTargetLanguage} | ||
| items={[ | ||
| ...priority, | ||
| ...AvailableLanguages.filter(e => !priority.includes(e)) | ||
| ]} /> | ||
| {/if} | ||
| </span> | ||
| </Card> |
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,32 @@ | ||
| import { tick } from 'svelte'; | ||
|
|
||
| interface Rect { top: number, right: number, bottom: number, left: number } | ||
|
|
||
| export const getRelativeRect = (element: HTMLElement, boundingElement: HTMLElement): Rect => { | ||
| const rect = element.getBoundingClientRect(); | ||
| const boundingRect = boundingElement.getBoundingClientRect(); | ||
| return { | ||
| top: rect.top - boundingRect.top, | ||
| right: boundingElement.clientWidth - (boundingRect.right - rect.right), | ||
| bottom: boundingElement.clientHeight - (boundingRect.bottom - rect.bottom), | ||
| left: rect.left - boundingRect.left | ||
| }; | ||
| }; | ||
|
|
||
| export const getDropdownOffsetY = async (div: HTMLElement, boundingDiv: HTMLElement): Promise<string> => { | ||
| await tick(); | ||
|
|
||
| const wrapper = div.querySelector('.dropdown-wrapper'); | ||
| const options = wrapper?.querySelector('.dropdown-options'); | ||
| if (wrapper == null || options == null) { | ||
| console.error('Dropdown wrapper or options not found'); | ||
| return ''; | ||
| } | ||
|
|
||
| const relativeRect = getRelativeRect(wrapper as HTMLElement, boundingDiv); | ||
| if (relativeRect.bottom + options.clientHeight > boundingDiv.clientHeight) { | ||
| return 'bottom-12'; | ||
| } else { | ||
| return 'top-12'; | ||
| } | ||
| }; |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice