feat: add post and comment translation

This commit is contained in:
Xylight committed 2024-06-05 14:13:43 -07:00
1 parent e7d12916c3
commit 990f073715
9 files changed
+156 -3

No files matched your search

BIN
View File
Binary file not shown.
@@ -8,6 +8,7 @@
EllipsisHorizontal, EllipsisHorizontal,
Flag, Flag,
Icon, Icon,
Language,
PencilSquare, PencilSquare,
Square2Stack, Square2Stack,
Trash, Trash,
@@ -24,6 +25,9 @@
import { deleteItem, save } from '$lib/lemmy/contentview.js' import { deleteItem, save } from '$lib/lemmy/contentview.js'
import { Button, Menu, MenuButton, MenuDivider } from 'mono-svelte' import { Button, Menu, MenuButton, MenuDivider } from 'mono-svelte'
import { t } from '$lib/translations' 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 comment: CommentView
export let replying: boolean = false export let replying: boolean = false
@@ -31,8 +35,13 @@
const dispatcher = createEventDispatcher<{ edit: CommentView }>() const dispatcher = createEventDispatcher<{ edit: CommentView }>()
let reply = '' let reply = ''
let translating = false
</script> </script>
{#if translating}
<Translation bind:open={translating} />
{/if}
<div class="flex flex-row items-center gap-0.5 h-7 relative"> <div class="flex flex-row items-center gap-0.5 h-7 relative">
<CommentVote <CommentVote
bind:upvotes={comment.counts.upvotes} bind:upvotes={comment.counts.upvotes}
@@ -79,6 +88,18 @@
<Icon src={Square2Stack} mini size="16" /> <Icon src={Square2Stack} mini size="16" />
<div>{$t('comment.actions.link')}</div> <div>{$t('comment.actions.link')}</div>
</MenuButton> </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 $profile?.jwt}
{#if comment.creator.id == $profile.user?.local_user_view.person.id} {#if comment.creator.id == $profile.user?.local_user_view.person.id}
<MenuButton on:click={() => dispatcher('edit', comment)}> <MenuButton on:click={() => dispatcher('edit', comment)}>
@@ -14,6 +14,7 @@
EyeSlash, EyeSlash,
Flag, Flag,
Icon, Icon,
Language,
Newspaper, Newspaper,
PencilSquare, PencilSquare,
ServerStack, ServerStack,
@@ -50,6 +51,8 @@
import { fediseer, type Data } from '$lib/fediseer/fediseer' import { fediseer, type Data } from '$lib/fediseer/fediseer'
import Fediseer from '$lib/fediseer/Fediseer.svelte' import Fediseer from '$lib/fediseer/Fediseer.svelte'
import { t } from '$lib/translations' import { t } from '$lib/translations'
import { text } from '$lib/components/translate/translation'
import Translation from '$lib/components/translate/Translation.svelte'
export let post: PostView export let post: PostView
@@ -62,6 +65,8 @@
let fediseerOpen = false let fediseerOpen = false
let fediseerData: Data | null = null let fediseerData: Data | null = null
let fediseerLoading = false let fediseerLoading = false
let translating = false
</script> </script>
{#if fediseerData} {#if fediseerData}
@@ -94,6 +99,8 @@
</Modal> </Modal>
{/if} {/if}
<Translation bind:open={translating} />
<div <div
class="flex flex-row gap-2 items-center !h-8 flex-shrink-0" class="flex flex-row gap-2 items-center !h-8 flex-shrink-0"
style={$$props.style ?? ''} style={$$props.style ?? ''}
@@ -247,6 +254,18 @@
<Icon src={Share} width={16} mini slot="prefix" /> <Icon src={Share} width={16} mini slot="prefix" />
{$t('post.actions.more.share')} {$t('post.actions.more.share')}
</MenuButton> </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} {#if $profile?.jwt}
<MenuButton <MenuButton
on:click={() => { on:click={() => {
@@ -97,7 +97,7 @@
loading = false loading = false
} }
let previewing = false export let previewing = false
const shortcuts = { const shortcuts = {
KeyB: () => wrapSelection('**', '**'), KeyB: () => wrapSelection('**', '**'),
@@ -174,7 +174,8 @@ overflow-hidden transition-colors"
> >
{#if previewing} {#if previewing}
<div <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)} /> <Markdown source={beforePreview(value)} />
</div> </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)
+6
View File
@@ -375,6 +375,7 @@
"actions": "Actions", "actions": "Actions",
"markRead": "Mark as Read", "markRead": "Mark as Read",
"markUnread": "Mark as Unread", "markUnread": "Mark as Unread",
"translate": "Translate",
"share": "Share", "share": "Share",
"crosspost": "Crosspost", "crosspost": "Crosspost",
"delete": "Delete", "delete": "Delete",
@@ -589,6 +590,11 @@
"font": { "font": {
"title": "Font", "title": "Font",
"description": "What font Photon uses in the UI." "description": "What font Photon uses in the UI."
},
"translation": {
"title": "Translation",
"description": "The LibreTranslate instance to use for translation.",
"instance": "Domain"
} }
}, },
"embeds": { "embeds": {
+2
View File
@@ -80,6 +80,7 @@ interface Settings {
compactFeatured: boolean compactFeatured: boolean
} }
language: string | null language: string | null
translator: string | undefined
} }
export const defaultSettings: Settings = { export const defaultSettings: Settings = {
@@ -138,6 +139,7 @@ export const defaultSettings: Settings = {
compactFeatured: toBool(env.PUBLIC_COMPACT_FEATURED) ?? true, compactFeatured: toBool(env.PUBLIC_COMPACT_FEATURED) ?? true,
}, },
language: null, language: null,
translator: undefined,
} }
export const userSettings = writable(defaultSettings) export const userSettings = writable(defaultSettings)
+12 -1
View File
@@ -29,7 +29,7 @@
import { Button, Checkbox, Select } from 'mono-svelte' import { Button, Checkbox, Select } from 'mono-svelte'
import ViewSelect from '$lib/components/lemmy/dropdowns/ViewSelect.svelte' import ViewSelect from '$lib/components/lemmy/dropdowns/ViewSelect.svelte'
import { LINKED_INSTANCE_URL } from '$lib/instance.js' import { LINKED_INSTANCE_URL } from '$lib/instance.js'
import { removeItem } from '$lib/util.js' import { DOMAIN_REGEX_FORMS, removeItem } from '$lib/util.js'
import Section from './Section.svelte' import Section from './Section.svelte'
import ToggleSetting from './ToggleSetting.svelte' import ToggleSetting from './ToggleSetting.svelte'
import { locales, t } from '$lib/translations' import { locales, t } from '$lib/translations'
@@ -224,6 +224,17 @@
bind:selected={$userSettings.font} bind:selected={$userSettings.font}
/> />
</Setting> </Setting>
<Setting>
<span slot="title">{$t('settings.app.translation.title')}</span>
<span slot="description">
{$t('settings.app.translation.description')}
</span>
<TextInput
bind:value={$userSettings.translator}
label={$t('settings.app.translation.instance')}
pattern={DOMAIN_REGEX_FORMS}
/>
</Setting>
</Section> </Section>
<Section title={$t('settings.embeds.title')}> <Section title={$t('settings.embeds.title')}>