diff --git a/src/components/Post/Translated/index.tsx b/src/components/Post/Translated/index.tsx index 2b6c199a41..069f5e80d8 100644 --- a/src/components/Post/Translated/index.tsx +++ b/src/components/Post/Translated/index.tsx @@ -4,6 +4,7 @@ import {Trans, useLingui} from '@lingui/react/macro' import {HITSLOP_30} from '#/lib/constants' import {useTranslate} from '#/lib/translation' +import {type TranslationFunction} from '#/lib/translation/types' import {codeToLanguageName, languageName} from '#/locale/helpers' import {LANGUAGES} from '#/locale/languages' import {useLanguagePrefs} from '#/state/preferences' @@ -21,7 +22,7 @@ export function TranslatedPost({ translationKey: string postText: string }) { - const {translationState} = useTranslate({key: translationKey}) + const {translate, translationState} = useTranslate({key: translationKey}) if (translationState.status === 'loading') { return @@ -30,7 +31,7 @@ export function TranslatedPost({ if (translationState.status === 'success') { return ( @@ -106,18 +107,17 @@ function TranslationResult({ } function TranslationLanguageSelect({ - translationKey, + translate, postText, sourceLanguage, }: { - translationKey: string + translate: TranslationFunction postText: string sourceLanguage: string }) { const ax = useAnalytics() const {t: l} = useLingui() const langPrefs = useLanguagePrefs() - const {translate} = useTranslate({key: translationKey}) const items = useMemo( () => diff --git a/src/lib/translation/context.ts b/src/lib/translation/context.ts index 30b3af8e36..b897f99fa4 100644 --- a/src/lib/translation/context.ts +++ b/src/lib/translation/context.ts @@ -1,28 +1,14 @@ import {createContext} from 'react' -import {type TranslationState} from './types' +import {type TranslationFunctionParams, type TranslationState} from './types' export const Context = createContext<{ translationState: Record - translate: (parameters: { - key: string - /** - * The text to be translated. - */ - text: string - /** - * The language to translate the text into. - */ - targetLangCode: string - /** - * The source language of the text. Will auto-detect if not provided. - */ - sourceLangCode?: string - /** - * Whether to force the use of Google Translate. Default is false. - */ - forceGoogleTranslate?: boolean - }) => Promise + translate: ( + parameters: TranslationFunctionParams & { + key: string + }, + ) => Promise clearTranslation: (key: string) => void acquireTranslation: (key: string) => () => void } | null>(null) diff --git a/src/lib/translation/index.tsx b/src/lib/translation/index.tsx index 4b04c9bf15..928485b41b 100644 --- a/src/lib/translation/index.tsx +++ b/src/lib/translation/index.tsx @@ -8,7 +8,7 @@ import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate' import {logger} from '#/logger' import {useAnalytics} from '#/analytics' import {Context} from './context' -import {type TranslationState} from './types' +import {type TranslationFunctionParams, type TranslationState} from './types' /** * Attempts on-device translation via @bsky.app/expo-translate-text. @@ -94,12 +94,7 @@ export function useTranslate({key}: {key: string}) { ) const translate = useCallback( - async (params: { - text: string - targetLangCode: string - sourceLangCode?: string - forceGoogleTranslate?: boolean - }) => { + async (params: TranslationFunctionParams) => { return context.translate({...params, key}) }, [key, context], diff --git a/src/lib/translation/types.ts b/src/lib/translation/types.ts index bd898fa5b6..ced23e1981 100644 --- a/src/lib/translation/types.ts +++ b/src/lib/translation/types.ts @@ -9,3 +9,26 @@ export type TranslationState = sourceLanguage: TranslationTaskResult['sourceLanguage'] targetLanguage: TranslationTaskResult['targetLanguage'] } + +export type TranslationFunctionParams = { + /** + * The text to be translated. + */ + text: string + /** + * The language to translate the text into. + */ + targetLangCode: string + /** + * The source language of the text. Will auto-detect if not provided. + */ + sourceLangCode?: string + /** + * Whether to force the use of Google Translate. Default is false. + */ + forceGoogleTranslate?: boolean +} + +export type TranslationFunction = ( + parameters: TranslationFunctionParams, +) => Promise