Skip to content
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

update: use instance's translation service #2426

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion composables/masto/masto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Pausable } from '@vueuse/core'
import type { CreateClientParams, WsEvents, mastodon } from 'masto'
import { createClient, fetchV1Instance } from 'masto'
import { createClient, fetchV1Instance, fetchV2Instance } from 'masto'
import type { Ref } from 'vue'
import type { ElkInstance } from '../users'
import type { Mutable } from '~/types/utils'
Expand Down Expand Up @@ -52,6 +52,15 @@ export function mastoLogin(masto: ElkMasto, user: Pick<UserLogin, 'server' | 'to
streamingApiUrl: newInstance.urls.streamingApi,
})
instanceStorage.value[server] = newInstance

try {
fetchV2Instance({ url }).then((newInstance) => {
instanceStorage.value[server].configuration.translationEnabled = newInstance.configuration.translation.enabled
})
}
catch(err) {
//
}
})

return instance
Expand Down
59 changes: 36 additions & 23 deletions composables/masto/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,48 @@ interface TranslationErr {
}
}

export async function translateText(text: string, from: string | null | undefined, to: string) {
export async function translateText(input: mastodon.v1.Status, from: string | null | undefined, to: string) {
const config = useRuntimeConfig()
const status = $ref({
success: false,
error: '',
text: '',
})
try {
const response = await ($fetch as any)(config.public.translateApi, {
method: 'POST',
body: {
q: text,
source: from ?? 'auto',
target: to,
format: 'html',
api_key: '',
},
}) as TranslationResponse
status.success = true
status.text = response.translatedText
}
catch (err) {
// TODO: improve type
if ((err as TranslationErr).data?.error)
status.error = (err as TranslationErr).data!.error!
else
status.error = 'Unknown Error, Please check your console in browser devtool.'
console.error('Translate Post Error: ', err)
if (currentInstance.value.configuration?.translationEnabled) {
try {
const { client } = $(useMasto())
const response = await client.v1.statuses.translate(input.id)
status.success = true
status.text = response.content
}
catch (err) {
status.error = response.error
}
} else {
try {
const response = await ($fetch as any)(config.public.translateApi, {
method: 'POST',
body: {
q: input.content,
source: from ?? 'auto',
target: to,
format: 'html',
api_key: '',
},
}) as TranslationResponse
status.success = true
status.text = response.translatedText
}
catch (err) {
// TODO: improve type
if ((err as TranslationErr).data?.error)
status.error = (err as TranslationErr).data!.error!
else
status.error = 'Unknown Error, Please check your console in browser devtool.'
console.error('Translate Post Error: ', err)
}
}

return status
}

Expand All @@ -110,7 +123,7 @@ export function useTranslation(status: mastodon.v1.Status | mastodon.v1.StatusEd
return

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