diff --git a/src/components/PostControls/PostMenu/PostMenuItems.tsx b/src/components/PostControls/PostMenu/PostMenuItems.tsx index ece506feba..9f10838ef8 100644 --- a/src/components/PostControls/PostMenu/PostMenuItems.tsx +++ b/src/components/PostControls/PostMenu/PostMenuItems.tsx @@ -137,6 +137,7 @@ let PostMenuItems = ({ const openLink = useOpenLink() const {clearTranslation, translate, translationState} = useTranslate({ key: post.uri, + forceGoogleTranslate, }) const navigation = useNavigation() const {mutedWordsDialogControl} = useGlobalDialogsControlContext() @@ -261,7 +262,6 @@ let PostMenuItems = ({ void translate({ text: record.text, targetLangCode: langPrefs.primaryLanguage, - forceGoogleTranslate, }) if ( diff --git a/src/lib/translation/context.ts b/src/lib/translation/context.ts index b897f99fa4..423efc4f1c 100644 --- a/src/lib/translation/context.ts +++ b/src/lib/translation/context.ts @@ -7,6 +7,7 @@ export const Context = createContext<{ translate: ( parameters: TranslationFunctionParams & { key: string + forceGoogleTranslate: boolean }, ) => Promise clearTranslation: (key: string) => void diff --git a/src/lib/translation/index.tsx b/src/lib/translation/index.tsx index 0f54a5b14e..88e46bee50 100644 --- a/src/lib/translation/index.tsx +++ b/src/lib/translation/index.tsx @@ -77,7 +77,13 @@ async function attemptTranslation( * * Web uses index.web.ts which always opens Google Translate. */ -export function useTranslate({key}: {key: string}) { +export function useTranslate({ + key, + forceGoogleTranslate = false, +}: { + key: string + forceGoogleTranslate?: boolean +}) { const context = useContext(Context) if (!context) { throw new Error( @@ -94,14 +100,14 @@ export function useTranslate({key}: {key: string}) { const translate = useCallback( async (params: TranslationFunctionParams) => { - return context.translate({...params, key}) + return context.translate({...params, key, forceGoogleTranslate}) }, - [key, context], + [context, forceGoogleTranslate, key], ) const clearTranslation = useCallback( () => context.clearTranslation(key), - [key, context], + [context, key], ) return { diff --git a/src/lib/translation/index.web.tsx b/src/lib/translation/index.web.tsx index fbcb4e9f55..8f913a14e5 100644 --- a/src/lib/translation/index.web.tsx +++ b/src/lib/translation/index.web.tsx @@ -3,7 +3,7 @@ import {useCallback, useContext, useMemo} from 'react' import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate' import {useAnalytics} from '#/analytics' import {Context} from './context' -import {type TranslationState} from './types' +import {type TranslationFunctionParams, type TranslationState} from './types' const translationState: Record = {} const acquireTranslation = (_key: string) => { @@ -14,7 +14,12 @@ const clearTranslation = (_key: string) => {} /** * Web always opens Google Translate. */ -export function useTranslate(key: string) { +export function useTranslate({ + key, +}: { + key: string + forceGoogleTranslate?: boolean +}) { const context = useContext(Context) if (!context) { throw new Error( @@ -24,42 +29,23 @@ export function useTranslate(key: string) { // Always call hooks in consistent order const translate = useCallback( - async (params: { - text: string - targetLangCode: string - sourceLangCode?: string - }) => { - if (!key) { - throw new Error( - 'translate requires a key. Either pass key to useTranslate() or use context.translate() with key parameter', - ) - } - return context.translate({...params, key}) + async (params: TranslationFunctionParams) => { + return context.translate({...params, key, forceGoogleTranslate: true}) }, [key, context], ) const clearTranslation = useCallback(() => { - if (!key) { - throw new Error( - 'clearTranslation requires a key. Either pass key to useTranslate() or use context.clearTranslation() with key parameter', - ) - } return context.clearTranslation(key) }, [key, context]) - // If a key is provided, return wrapped versions that automatically use the key - if (key) { - return { - translationState: context.translationState[key] ?? { - status: 'idle' as const, - }, - translate, - clearTranslation, - } + return { + translationState: context.translationState[key] ?? { + status: 'idle' as const, + }, + translate, + clearTranslation, } - - return context } export function Provider({children}: React.PropsWithChildren) { diff --git a/src/lib/translation/types.ts b/src/lib/translation/types.ts index ced23e1981..23eb73ba1e 100644 --- a/src/lib/translation/types.ts +++ b/src/lib/translation/types.ts @@ -23,10 +23,6 @@ export type TranslationFunctionParams = { * 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 = (