2023-01-08 09:21:09 +03:00
|
|
|
import type { mastodon } from 'masto'
|
2022-11-25 03:14:16 +03:00
|
|
|
|
|
|
|
export interface TranslationResponse {
|
|
|
|
translatedText: string
|
|
|
|
detectedLanguage: {
|
|
|
|
confidence: number
|
|
|
|
language: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 15:18:49 +03:00
|
|
|
// @see https://github.com/LibreTranslate/LibreTranslate/tree/main/libretranslate/locales
|
|
|
|
export const supportedTranslationCodes = [
|
|
|
|
'ar',
|
|
|
|
'az',
|
|
|
|
'cs',
|
|
|
|
'da',
|
|
|
|
'de',
|
|
|
|
'el',
|
|
|
|
'en',
|
|
|
|
'eo',
|
|
|
|
'es',
|
|
|
|
'fa',
|
|
|
|
'fi',
|
|
|
|
'fr',
|
|
|
|
'ga',
|
|
|
|
'he',
|
|
|
|
'hi',
|
|
|
|
'hu',
|
|
|
|
'id',
|
|
|
|
'it',
|
|
|
|
'ja',
|
|
|
|
'ko',
|
|
|
|
'nl',
|
|
|
|
'pl',
|
|
|
|
'pt',
|
|
|
|
'ru',
|
|
|
|
'sk',
|
|
|
|
'sv',
|
|
|
|
'tr',
|
|
|
|
'uk',
|
|
|
|
'vi',
|
|
|
|
'zh',
|
|
|
|
] as const
|
|
|
|
|
2023-03-30 22:01:24 +03:00
|
|
|
export function getLanguageCode() {
|
2023-01-18 00:41:26 +03:00
|
|
|
let code = 'en'
|
|
|
|
const getCode = (code: string) => code.replace(/-.*$/, '')
|
|
|
|
if (!process.server) {
|
|
|
|
const { locale } = useI18n()
|
|
|
|
code = getCode(locale.value ? locale.value : navigator.language)
|
|
|
|
}
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TranslationErr {
|
|
|
|
data?: {
|
|
|
|
error?: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function translateText(text: string, from: string | null | undefined, to: string) {
|
2022-12-17 19:55:29 +03:00
|
|
|
const config = useRuntimeConfig()
|
2023-01-16 12:55:00 +03:00
|
|
|
const status = $ref({
|
|
|
|
success: false,
|
|
|
|
error: '',
|
|
|
|
text: '',
|
2022-11-25 03:14:16 +03:00
|
|
|
})
|
2023-01-16 12:55:00 +03:00
|
|
|
try {
|
2023-08-01 12:36:19 +03:00
|
|
|
const response = await $fetch(config.public.translateApi, {
|
2023-01-16 12:55:00 +03:00
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
q: text,
|
|
|
|
source: from ?? 'auto',
|
2023-01-18 00:41:26 +03:00
|
|
|
target: to,
|
2023-01-16 12:55:00 +03:00
|
|
|
format: 'html',
|
|
|
|
api_key: '',
|
|
|
|
},
|
2023-08-01 12:36:19 +03:00
|
|
|
}) as TranslationResponse
|
2023-01-16 12:55:00 +03:00
|
|
|
status.success = true
|
|
|
|
status.text = response.translatedText
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
// TODO: improve type
|
2023-01-18 00:41:26 +03:00
|
|
|
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)
|
2023-01-16 12:55:00 +03:00
|
|
|
}
|
|
|
|
return status
|
2022-11-25 03:14:16 +03:00
|
|
|
}
|
|
|
|
|
2023-01-16 12:55:00 +03:00
|
|
|
const translations = new WeakMap<mastodon.v1.Status | mastodon.v1.StatusEdit, { visible: boolean; text: string; success: boolean; error: string }>()
|
2022-11-25 03:14:16 +03:00
|
|
|
|
2023-01-18 00:41:26 +03:00
|
|
|
export function useTranslation(status: mastodon.v1.Status | mastodon.v1.StatusEdit, to: string) {
|
2022-11-25 03:14:16 +03:00
|
|
|
if (!translations.has(status))
|
2023-01-16 12:55:00 +03:00
|
|
|
translations.set(status, reactive({ visible: false, text: '', success: false, error: '' }))
|
2022-11-25 03:14:16 +03:00
|
|
|
|
|
|
|
const translation = translations.get(status)!
|
2023-01-29 15:18:49 +03:00
|
|
|
const userSettings = useUserSettings()
|
|
|
|
|
|
|
|
const shouldTranslate = 'language' in status && status.language && status.language !== to
|
|
|
|
&& supportedTranslationCodes.includes(to as any)
|
|
|
|
&& supportedTranslationCodes.includes(status.language as any)
|
|
|
|
&& !userSettings.value.disabledTranslationLanguages.includes(status.language)
|
|
|
|
const enabled = /*! !useRuntimeConfig().public.translateApi && */ shouldTranslate
|
2022-11-25 03:14:16 +03:00
|
|
|
|
|
|
|
async function toggle() {
|
2023-01-29 15:18:49 +03:00
|
|
|
if (!shouldTranslate)
|
2023-01-05 19:48:20 +03:00
|
|
|
return
|
|
|
|
|
2023-01-16 12:55:00 +03:00
|
|
|
if (!translation.text) {
|
2023-01-18 00:41:26 +03:00
|
|
|
const { success, text, error } = await translateText(status.content, status.language, to)
|
2023-01-16 12:55:00 +03:00
|
|
|
translation.error = error
|
|
|
|
translation.text = text
|
|
|
|
translation.success = success
|
|
|
|
}
|
2022-11-25 03:14:16 +03:00
|
|
|
|
|
|
|
translation.visible = !translation.visible
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2023-01-29 15:18:49 +03:00
|
|
|
enabled,
|
2022-11-25 03:14:16 +03:00
|
|
|
toggle,
|
|
|
|
translation,
|
|
|
|
}
|
|
|
|
}
|