From 8e2a5abfff38ca0830d7358153e82a354d6c7baf Mon Sep 17 00:00:00 2001 From: DS Boyce <260543580+ds-boyce@users.noreply.github.com> Date: Sat, 28 Feb 2026 15:53:42 -0800 Subject: [PATCH] Fix crash with translate link on web (#9972) --- src/translation/index.tsx | 31 +++++++++++++++++-------- src/translation/index.web.tsx | 43 ----------------------------------- 2 files changed, 21 insertions(+), 53 deletions(-) delete mode 100644 src/translation/index.web.tsx diff --git a/src/translation/index.tsx b/src/translation/index.tsx index 42e62ed6ed..7c5f05f33a 100644 --- a/src/translation/index.tsx +++ b/src/translation/index.tsx @@ -14,6 +14,7 @@ import {getTranslatorLink} from '#/locale/helpers' import {logger} from '#/logger' import {useLanguagePrefs} from '#/state/preferences' import {useAnalytics} from '#/analytics' +import {IS_WEB} from '#/env' type TranslationState = | {status: 'idle'} @@ -119,7 +120,7 @@ export function useTranslateOnDevice() { return context } -export function Provider({children}: {children?: React.ReactNode}) { +export function Provider({children}: React.PropsWithChildren) { const [translationState, setTranslationState] = useState(IDLE) const openLink = useOpenLink() @@ -158,15 +159,25 @@ export function Provider({children}: {children?: React.ReactNode}) { targetLanguage: result.targetLanguage, }) } catch (e) { - logger.error('Failed to translate post on device', {safeMessage: e}) - // On-device translation failed (language pack missing or user dismissed - // the download prompt). Fall back to Google Translate. - ax.metric('translate:result', { - method: 'fallback-alert', - os: Platform.OS, - sourceLanguage: sourceLangCode ?? null, - targetLanguage: targetLangCode, - }) + if (IS_WEB) { + // Web always opens Google Translate. + ax.metric('translate:result', { + method: 'google-translate', + os: Platform.OS, + sourceLanguage: sourceLangCode ?? null, + targetLanguage: targetLangCode, + }) + } else { + logger.error('Failed to translate post on device', {safeMessage: e}) + // On-device translation failed (language pack missing or user dismissed + // the download prompt). Fall back to Google Translate. + ax.metric('translate:result', { + method: 'fallback-alert', + os: Platform.OS, + sourceLanguage: sourceLangCode ?? null, + targetLanguage: targetLangCode, + }) + } setTranslationState({status: 'idle'}) const translateUrl = getTranslatorLink( text, diff --git a/src/translation/index.web.tsx b/src/translation/index.web.tsx deleted file mode 100644 index fbeb6a1b89..0000000000 --- a/src/translation/index.web.tsx +++ /dev/null @@ -1,43 +0,0 @@ -import {useCallback} from 'react' -import {Platform} from 'react-native' - -import {useOpenLink} from '#/lib/hooks/useOpenLink' -import {getTranslatorLink} from '#/locale/helpers' -import {useLanguagePrefs} from '#/state/preferences' -import {useAnalytics} from '#/analytics' - -const translationState = {status: 'idle'} // No on-device translations for web. - -const clearTranslation = () => {} // no-op on web - -/** - * Web always opens Google Translate. - */ -export function useTranslateOnDevice() { - const openLink = useOpenLink() - const ax = useAnalytics() - const {primaryLanguage} = useLanguagePrefs() - - const translate = useCallback( - async ( - text: string, - targetLangCode: string = primaryLanguage, - sourceLangCode: string, - ) => { - const translateUrl = getTranslatorLink( - text, - targetLangCode, - sourceLangCode, - ) - ax.metric('translate:result', { - method: 'google-translate', - os: Platform.OS, - sourceLanguage: sourceLangCode ?? null, - targetLanguage: targetLangCode, - }) - await openLink(translateUrl) - }, - [ax, openLink, primaryLanguage], - ) - return {clearTranslation, translate, translationState} -}