Skip to content

Commit

Permalink
feat: improve posts translation logic (elk-zone#1211)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhang1030 authored Jan 16, 2023
1 parent 3b73d11 commit d745bd0
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 23 deletions.
8 changes: 0 additions & 8 deletions components/status/StatusActionsMore.vue
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,6 @@ const showFavoritedAndBoostedBy = () => {
/>
</NuxtLink>

<CommonDropdownItem
v-if="isTranslationEnabled && status.language !== languageCode"
:text="translation.visible ? $t('menu.show_untranslated') : $t('menu.translate_post')"
icon="i-ri:translate"
:command="command"
@click="toggleTranslation"
/>

<template v-if="isHydrated && currentUser">
<template v-if="isAuthor">
<CommonDropdownItem
Expand Down
5 changes: 4 additions & 1 deletion components/status/StatusBody.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ const vnode = $computed(() => {
<div v-else />
<template v-if="translation.visible">
<div my2 h-px border="b base" bg-base />
<ContentRich class="line-compact" :content="translation.text" :emojis="status.emojis" />
<ContentRich v-if="translation.success" class="line-compact" :content="translation.text" :emojis="status.emojis" />
<div v-else text-red-4>
Error: {{ translation.error }}
</div>
</template>
</div>
</template>
Expand Down
1 change: 1 addition & 0 deletions components/status/StatusContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const isFiltered = $computed(() => filterPhrase && (context && context !== 'deta
<p>{{ status.spoilerText }}</p>
</template>
<StatusBody v-if="!status.sensitive || status.spoilerText" :status="status" :with-action="!isDetails" :class="isDetails ? 'text-xl' : ''" />
<StatusTranslation :status="status" />
<StatusPoll v-if="status.poll" :status="status" />
<StatusMedia
v-if="status.mediaAttachments?.length"
Expand Down
41 changes: 41 additions & 0 deletions components/status/StatusTranslation.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<script setup lang="ts">
import type { mastodon } from 'masto'
const { status } = defineProps<{
status: mastodon.v1.Status
}>()
const {
toggle: _toggleTranslation,
translation,
enabled: isTranslationEnabled,
} = useTranslation(status)
let translating = $ref(false)
const toggleTranslation = async () => {
translating = true
try {
await _toggleTranslation()
}
finally {
translating = false
}
}
</script>

<template>
<div>
<button
v-if="isTranslationEnabled && status.language !== languageCode" pl-0 flex="~ center" gap-2
:disabled="translating" disabled-bg-transparent btn-text @click="toggleTranslation"
>
<span v-if="translating" block animate-spin preserve-3d>
<span block i-ri:loader-2-fill />
</span>
<div v-else i-ri:translate />
{{ translation.visible ? $t('menu.show_untranslated') : $t('menu.translate_post') }}
</button>
</div>
</template>

<style scoped></style>
45 changes: 31 additions & 14 deletions composables/masto/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,50 @@ export interface TranslationResponse {
export const languageCode = process.server ? 'en' : navigator.language.replace(/-.*$/, '')
export async function translateText(text: string, from?: string | null, to?: string) {
const config = useRuntimeConfig()
const { translatedText } = await $fetch<TranslationResponse>(config.public.translateApi, {
method: 'POST',
body: {
q: text,
source: from ?? 'auto',
target: to ?? languageCode,
format: 'html',
api_key: '',
},
const status = $ref({
success: false,
error: '',
text: '',
})
return translatedText
try {
const response = await $fetch<TranslationResponse>(config.public.translateApi, {
method: 'POST',
body: {
q: text,
source: from ?? 'auto',
target: to ?? languageCode,
format: 'html',
api_key: '',
},
})
status.success = true
status.text = response.translatedText
}
catch (err) {
// TODO: improve type
status.error = (err as { data: { error: string } }).data.error
}
return status
}

const translations = new WeakMap<mastodon.v1.Status | mastodon.v1.StatusEdit, { visible: boolean; text: string }>()
const translations = new WeakMap<mastodon.v1.Status | mastodon.v1.StatusEdit, { visible: boolean; text: string; success: boolean; error: string }>()

export function useTranslation(status: mastodon.v1.Status | mastodon.v1.StatusEdit) {
if (!translations.has(status))
translations.set(status, reactive({ visible: false, text: '' }))
translations.set(status, reactive({ visible: false, text: '', success: false, error: '' }))

const translation = translations.get(status)!

async function toggle() {
if (!('language' in status))
return

if (!translation.text)
translation.text = await translateText(status.content, status.language)
if (!translation.text) {
const { success, text, error } = await translateText(status.content, status.language)
translation.error = error
translation.text = text
translation.success = success
}

translation.visible = !translation.visible
}
Expand Down
1 change: 1 addition & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export default defineNuxtConfig({
env: '', // set in build-env module
buildInfo: {} as BuildInfo, // set in build-env module
pwaEnabled: !isDevelopment || process.env.VITE_DEV_PWA === 'true',
// We use LibreTranslate(https://github.com/LibreTranslate/LibreTranslate) as our default translation server #76
translateApi: '',
defaultServer: 'mas.to',
},
Expand Down

0 comments on commit d745bd0

Please sign in to comment.