feat: add post and comment translation
This commit is contained in:
1 parent
e7d12916c3
commit
990f073715
9 files changed
+156
-3
No files matched your search
@@ -8,6 +8,7 @@
|
||||
EllipsisHorizontal,
|
||||
Flag,
|
||||
Icon,
|
||||
Language,
|
||||
PencilSquare,
|
||||
Square2Stack,
|
||||
Trash,
|
||||
@@ -24,6 +25,9 @@
|
||||
import { deleteItem, save } from '$lib/lemmy/contentview.js'
|
||||
import { Button, Menu, MenuButton, MenuDivider } from 'mono-svelte'
|
||||
import { t } from '$lib/translations'
|
||||
import Translation from '$lib/components/translate/Translation.svelte'
|
||||
import { text } from '$lib/components/translate/translation'
|
||||
import { userSettings } from '$lib/settings'
|
||||
|
||||
export let comment: CommentView
|
||||
export let replying: boolean = false
|
||||
@@ -31,8 +35,13 @@
|
||||
const dispatcher = createEventDispatcher<{ edit: CommentView }>()
|
||||
|
||||
let reply = ''
|
||||
let translating = false
|
||||
</script>
|
||||
|
||||
{#if translating}
|
||||
<Translation bind:open={translating} />
|
||||
{/if}
|
||||
|
||||
<div class="flex flex-row items-center gap-0.5 h-7 relative">
|
||||
<CommentVote
|
||||
bind:upvotes={comment.counts.upvotes}
|
||||
@@ -79,6 +88,18 @@
|
||||
<Icon src={Square2Stack} mini size="16" />
|
||||
<div>{$t('comment.actions.link')}</div>
|
||||
</MenuButton>
|
||||
{#if $userSettings.translator}
|
||||
<MenuButton
|
||||
on:click={() => {
|
||||
// @ts-ignore
|
||||
text.set(comment.comment.content)
|
||||
translating = !translating
|
||||
}}
|
||||
>
|
||||
<Icon src={Language} size="16" mini slot="prefix" />
|
||||
{$t('post.actions.more.translate')}
|
||||
</MenuButton>
|
||||
{/if}
|
||||
{#if $profile?.jwt}
|
||||
{#if comment.creator.id == $profile.user?.local_user_view.person.id}
|
||||
<MenuButton on:click={() => dispatcher('edit', comment)}>
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
EyeSlash,
|
||||
Flag,
|
||||
Icon,
|
||||
Language,
|
||||
Newspaper,
|
||||
PencilSquare,
|
||||
ServerStack,
|
||||
@@ -50,6 +51,8 @@
|
||||
import { fediseer, type Data } from '$lib/fediseer/fediseer'
|
||||
import Fediseer from '$lib/fediseer/Fediseer.svelte'
|
||||
import { t } from '$lib/translations'
|
||||
import { text } from '$lib/components/translate/translation'
|
||||
import Translation from '$lib/components/translate/Translation.svelte'
|
||||
|
||||
export let post: PostView
|
||||
|
||||
@@ -62,6 +65,8 @@
|
||||
let fediseerOpen = false
|
||||
let fediseerData: Data | null = null
|
||||
let fediseerLoading = false
|
||||
|
||||
let translating = false
|
||||
</script>
|
||||
|
||||
{#if fediseerData}
|
||||
@@ -94,6 +99,8 @@
|
||||
</Modal>
|
||||
{/if}
|
||||
|
||||
<Translation bind:open={translating} />
|
||||
|
||||
<div
|
||||
class="flex flex-row gap-2 items-center !h-8 flex-shrink-0"
|
||||
style={$$props.style ?? ''}
|
||||
@@ -247,6 +254,18 @@
|
||||
<Icon src={Share} width={16} mini slot="prefix" />
|
||||
{$t('post.actions.more.share')}
|
||||
</MenuButton>
|
||||
{#if post.post.body && $userSettings.translator}
|
||||
<MenuButton
|
||||
on:click={() => {
|
||||
// @ts-ignore
|
||||
text.set(post.post.body)
|
||||
translating = !translating
|
||||
}}
|
||||
>
|
||||
<Icon src={Language} size="16" mini slot="prefix" />
|
||||
{$t('post.actions.more.translate')}
|
||||
</MenuButton>
|
||||
{/if}
|
||||
{#if $profile?.jwt}
|
||||
<MenuButton
|
||||
on:click={() => {
|
||||
|
||||
@@ -97,7 +97,7 @@
|
||||
loading = false
|
||||
}
|
||||
|
||||
let previewing = false
|
||||
export let previewing = false
|
||||
|
||||
const shortcuts = {
|
||||
KeyB: () => wrapSelection('**', '**'),
|
||||
@@ -174,7 +174,8 @@ overflow-hidden transition-colors"
|
||||
>
|
||||
{#if previewing}
|
||||
<div
|
||||
class="px-3 py-2.5 h-32 overflow-auto text-sm resize-y bg-white dark:bg-zinc-950"
|
||||
class="px-3 py-2.5 overflow-auto text-sm resize-y bg-white dark:bg-zinc-950"
|
||||
style="height: {rows * 2}em"
|
||||
>
|
||||
<Markdown source={beforePreview(value)} />
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
<script lang="ts">
|
||||
import { Button, Modal, Select, Spinner, TextArea } from 'mono-svelte'
|
||||
import { text } from './translation'
|
||||
import { t } from '$lib/translations'
|
||||
import { userSettings } from '$lib/settings'
|
||||
import { ArrowRight, Icon } from 'svelte-hero-icons'
|
||||
import MarkdownEditor from '../markdown/MarkdownEditor.svelte'
|
||||
|
||||
interface TranslateResponse {
|
||||
translatedText: string
|
||||
}
|
||||
|
||||
export let open: boolean = false
|
||||
|
||||
$: if (open && $text) translate($text)
|
||||
|
||||
let result: Promise<TranslateResponse> | undefined = undefined
|
||||
let from = 'auto'
|
||||
let to = 'en'
|
||||
|
||||
async function translate(text: string) {
|
||||
const url = $userSettings.translator
|
||||
|
||||
if (!url) {
|
||||
return
|
||||
}
|
||||
|
||||
const requestURL = new URL(`https://${url}/translate`)
|
||||
requestURL.searchParams.set('q', text)
|
||||
requestURL.searchParams.set('source', from)
|
||||
requestURL.searchParams.set('target', to)
|
||||
requestURL.searchParams.set('alternatives', '')
|
||||
|
||||
result = fetch(requestURL.toString(), {
|
||||
method: 'POST',
|
||||
}).then(async (res) => await res.json())
|
||||
}
|
||||
</script>
|
||||
|
||||
<Modal title={$t('post.actions.more.translate')} bind:open>
|
||||
<TextArea value={$text} rows={2}></TextArea>
|
||||
<div class="flex items-center gap-2 w-full">
|
||||
<Select bind:value={from} class="flex-1">
|
||||
<option value="auto">Auto</option>
|
||||
</Select>
|
||||
<Icon src={ArrowRight} mini size="20" />
|
||||
<Select bind:value={to} class="flex-1">
|
||||
<option value="en">English</option>
|
||||
<option value="ar">Arabic</option>
|
||||
<option value="zh">Chinese</option>
|
||||
<option value="cs">Czech</option>
|
||||
<option value="nl">Dutch</option>
|
||||
<option value="fi">Finnish</option>
|
||||
<option value="fr">French</option>
|
||||
<option value="de">German</option>
|
||||
<option value="el">Greek</option>
|
||||
<option value="it">Italian</option>
|
||||
<option value="ja">Japanese</option>
|
||||
<option value="pl">Polish</option>
|
||||
<option value="es">Spanish</option>
|
||||
<option value="sv">Swedish</option>
|
||||
</Select>
|
||||
</div>
|
||||
{#if result}
|
||||
{#await result}
|
||||
<div
|
||||
style="height: 194px; width:100%;"
|
||||
class="flex items-center justify-center"
|
||||
>
|
||||
<Spinner width={32} />
|
||||
</div>
|
||||
{:then result}
|
||||
<MarkdownEditor
|
||||
previewButton={false}
|
||||
previewing
|
||||
tools={false}
|
||||
rows={8}
|
||||
value={result.translatedText}
|
||||
disabled
|
||||
></MarkdownEditor>
|
||||
{:catch err}
|
||||
<TextArea rows={8} value={err}></TextArea>
|
||||
{/await}
|
||||
{:else}
|
||||
<TextArea disabled />
|
||||
{/if}
|
||||
<Button on:click={() => translate($text)} color="primary" size="lg">
|
||||
{$t('post.actions.more.translate')}
|
||||
</Button>
|
||||
</Modal>
|
||||
@@ -0,0 +1,3 @@
|
||||
import { writable } from 'svelte/store'
|
||||
|
||||
export let text = writable<string>(undefined)
|
||||
Reference in new issue
Block a user