fix: respect source language of post and move translation UI into menu
This commit is contained in:
parent
bb05003fa3
commit
3f382bdad0
2 changed files with 15 additions and 14 deletions
|
@ -10,13 +10,14 @@ export interface TranslationResponse {
|
|||
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
export async function translateText(text: string) {
|
||||
export const languageCode = process.server ? 'en' : navigator.language.replace(/-.*$/, '')
|
||||
export async function translateText(text: string, from?: string | null, to?: string) {
|
||||
const { translatedText } = await $fetch<TranslationResponse>(config.public.translateApi, {
|
||||
method: 'POST',
|
||||
body: {
|
||||
q: text,
|
||||
source: 'auto',
|
||||
target: navigator.language.replace(/-.*$/, ''),
|
||||
source: from ?? 'auto',
|
||||
target: to ?? languageCode,
|
||||
format: 'html',
|
||||
api_key: '',
|
||||
},
|
||||
|
@ -34,7 +35,7 @@ export function useTranslation(status: Status) {
|
|||
|
||||
async function toggle() {
|
||||
if (!translation.text)
|
||||
translation.text = await translateText(status.content)
|
||||
translation.text = await translateText(status.content, status.language)
|
||||
|
||||
translation.visible = !translation.visible
|
||||
}
|
||||
|
@ -45,3 +46,4 @@ export function useTranslation(status: Status) {
|
|||
translation,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue