2023-01-08 06:21:09 +00:00
|
|
|
import type { mastodon } from 'masto'
|
2022-11-25 00:14:16 +00:00
|
|
|
|
|
|
|
export interface TranslationResponse {
|
|
|
|
translatedText: string
|
|
|
|
detectedLanguage: {
|
|
|
|
confidence: number
|
|
|
|
language: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 12:18:49 +00: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 19:01:24 +00:00
|
|
|
export function getLanguageCode() {
|
2023-01-17 21:41:26 +00:00
|
|
|
let code = 'en'
|
|
|
|
const getCode = (code: string) => code.replace(/-.*$/, '')
|
2024-02-24 16:46:14 +00:00
|
|
|
if (import.meta.client) {
|
2023-01-17 21:41:26 +00:00
|
|
|
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 16:55:29 +00:00
|
|
|
const config = useRuntimeConfig()
|
2024-02-21 15:20:08 +00:00
|
|
|
const status = ref({
|
2023-01-16 09:55:00 +00:00
|
|
|
success: false,
|
|
|
|
error: '',
|
|
|
|
text: '',
|
2022-11-25 00:14:16 +00:00
|
|
|
})
|
2023-10-13 07:12:48 +00:00
|
|
|
const regex = /<a[^>]*>.*?<\/a>/g
|
2023-01-16 09:55:00 +00:00
|
|
|
try {
|
2023-08-01 21:11:26 +00:00
|
|
|
const response = await ($fetch as any)(config.public.translateApi, {
|
2023-01-16 09:55:00 +00:00
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
q: text,
|
|
|
|
source: from ?? 'auto',
|
2023-01-17 21:41:26 +00:00
|
|
|
target: to,
|
2023-01-16 09:55:00 +00:00
|
|
|
format: 'html',
|
|
|
|
api_key: '',
|
|
|
|
},
|
2023-08-01 09:36:19 +00:00
|
|
|
}) as TranslationResponse
|
2024-02-21 15:20:08 +00:00
|
|
|
status.value.success = true
|
2023-10-13 07:12:48 +00:00
|
|
|
// replace the translated links with the original
|
2024-02-21 15:20:08 +00:00
|
|
|
status.value.text = response.translatedText.replace(regex, (match) => {
|
2023-10-13 07:12:48 +00:00
|
|
|
const tagLink = regex.exec(text)
|
|
|
|
return tagLink ? tagLink[0] : match
|
|
|
|
})
|
2023-01-16 09:55:00 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
// TODO: improve type
|
2023-01-17 21:41:26 +00:00
|
|
|
if ((err as TranslationErr).data?.error)
|
2024-02-21 15:20:08 +00:00
|
|
|
status.value.error = (err as TranslationErr).data!.error!
|
2023-01-17 21:41:26 +00:00
|
|
|
else
|
2024-02-21 15:20:08 +00:00
|
|
|
status.value.error = 'Unknown Error, Please check your console in browser devtool.'
|
2023-01-17 21:41:26 +00:00
|
|
|
console.error('Translate Post Error: ', err)
|
2023-01-16 09:55:00 +00:00
|
|
|
}
|
|
|
|
return status
|
2022-11-25 00:14:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 09:55:00 +00:00
|
|
|
const translations = new WeakMap<mastodon.v1.Status | mastodon.v1.StatusEdit, { visible: boolean; text: string; success: boolean; error: string }>()
|
2022-11-25 00:14:16 +00:00
|
|
|
|
2023-01-17 21:41:26 +00:00
|
|
|
export function useTranslation(status: mastodon.v1.Status | mastodon.v1.StatusEdit, to: string) {
|
2022-11-25 00:14:16 +00:00
|
|
|
if (!translations.has(status))
|
2023-01-16 09:55:00 +00:00
|
|
|
translations.set(status, reactive({ visible: false, text: '', success: false, error: '' }))
|
2022-11-25 00:14:16 +00:00
|
|
|
|
|
|
|
const translation = translations.get(status)!
|
2023-01-29 12:18:49 +00: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 00:14:16 +00:00
|
|
|
|
|
|
|
async function toggle() {
|
2023-01-29 12:18:49 +00:00
|
|
|
if (!shouldTranslate)
|
2023-01-05 16:48:20 +00:00
|
|
|
return
|
|
|
|
|
2023-01-16 09:55:00 +00:00
|
|
|
if (!translation.text) {
|
2024-02-21 15:20:08 +00:00
|
|
|
const translated = await translateText(status.content, status.language, to)
|
|
|
|
translation.error = translated.value.error
|
|
|
|
translation.text = translated.value.text
|
|
|
|
translation.success = translated.value.success
|
2023-01-16 09:55:00 +00:00
|
|
|
}
|
2022-11-25 00:14:16 +00:00
|
|
|
|
|
|
|
translation.visible = !translation.visible
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2023-01-29 12:18:49 +00:00
|
|
|
enabled,
|
2022-11-25 00:14:16 +00:00
|
|
|
toggle,
|
|
|
|
translation,
|
|
|
|
}
|
|
|
|
}
|