Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162import {useCallback} from 'react'import * as IntentLauncher from 'expo-intent-launcher'
import {useOpenLink} from '#/lib/hooks/useOpenLink'import {getTranslatorLink} from '#/locale/helpers'import {IS_ANDROID} from '#/env'
/** * @deprecated Will always link out to Google Translate. Prefer `useTranslate`. */export function useGoogleTranslate() { const openLink = useOpenLink()
return useCallback( async (text: string, targetLangCode: string, sourceLanguage?: string) => { const translateUrl = getTranslatorLink( text, targetLangCode, sourceLanguage, ) if (IS_ANDROID) { try { // use `getApplicationIconAsync` to determine if the translate app is installed const installed = await IntentLauncher.getApplicationIconAsync( 'com.google.android.apps.translate', ) if (!installed) { openLink(translateUrl) return }
// TODO: this should only be called one at a time, use something like // RQ's `scope` - otherwise can trigger the browser to open unexpectedly when the call throws -sfn await IntentLauncher.startActivityAsync( 'android.intent.action.PROCESS_TEXT', { type: 'text/plain', extra: { 'android.intent.extra.PROCESS_TEXT': text, 'android.intent.extra.PROCESS_TEXT_READONLY': true, }, // note: to skip the intermediate app select, we need to specify a // `className`. however, this isn't safe to hardcode, we'd need to query the // package manager for the correct activity. this requires native code, so // skip for now -sfn // packageName: 'com.google.android.apps.translate', // className: 'com.google.android.apps.translate.TranslateActivity', }, ) } catch (err) { if (__DEV__) console.error(err) // most likely means they don't have the translate app openLink(translateUrl) } } else { openLink(translateUrl) } }, [openLink], )}