1
0
mirror of https://github.com/tooot-app/app synced 2025-06-05 22:19:13 +02:00

Local translation works

This commit is contained in:
Zhiyuan Zheng
2021-05-19 23:28:01 +02:00
parent 67d3589f30
commit 0517d2fae2
15 changed files with 159 additions and 42 deletions

View File

@ -1,30 +1,42 @@
import apiGeneral from '@api/general'
import { useQuery } from 'react-query'
import { AxiosError } from 'axios'
import { Constants } from 'react-native-unimodules'
import { useQuery, UseQueryOptions } from 'react-query'
export type QueryKeyTranslate = [
'Translate',
{ toot: string; source: string; target: string }
]
const queryFunction = ({ queryKey }: { queryKey: QueryKeyTranslate }) => {
const queryFunction = async ({ queryKey }: { queryKey: QueryKeyTranslate }) => {
const { toot, source, target } = queryKey[1]
return apiGeneral<Translate.Translate>({
const res = await apiGeneral<Translate.Translate>({
domain: 'translate.tooot.app',
method: 'post',
url: 'translate',
params: {
api_key: '65180371-1ddb-4ec0-9aa3-ac47d371c41a',
api_key: Constants.manifest?.extra?.translateKey,
q: toot,
source,
target
}
}).then(res => res.body.translatedText)
})
return res.body.translatedText
}
const useTranslateQuery = (queryKeyParams: QueryKeyTranslate[1]) => {
const useTranslateQuery = ({
options,
...queryKeyParams
}: QueryKeyTranslate[1] & {
options?: UseQueryOptions<
Translate.Translate['translatedText'],
AxiosError,
Translate.Translate['translatedText']
>
}) => {
const queryKey: QueryKeyTranslate = ['Translate', { ...queryKeyParams }]
return useQuery(queryKey, queryFunction)
return useQuery(queryKey, queryFunction, options)
}
export { useTranslateQuery }