Bluesky app fork with some witchin' additions 馃挮
at main 90 lines 3.3 kB view raw
1import {useCallback} from 'react' 2import * as IntentLauncher from 'expo-intent-launcher' 3 4import {useOpenLink} from '#/lib/hooks/useOpenLink' 5import { 6 getTranslatorLink, 7 getTranslatorLinkKagi, 8 getTranslatorLinkLibreTranslate, 9 getTranslatorLinkPapago, 10} from '#/locale/helpers' 11import {useTranslationServicePreference} from '#/state/preferences/translation-service-preference' 12import {IS_ANDROID} from '#/env' 13 14/** 15 * @deprecated Will always link out to Google Translate. Prefer `useTranslate`. 16 */ 17export function useGoogleTranslate() { 18 const openLink = useOpenLink() 19 20 const translationServicePreference = useTranslationServicePreference() 21 22 return useCallback( 23 async (text: string, targetLangCode: string, sourceLanguage?: string) => { 24 let translateUrl 25 26 // if ur curious why this isnt a switch case, good question, for some reason making this a switch case breaks the functionality 27 // it is a mystery https://www.youtube.com/watch?v=fq3abPnEEGE 28 if (translationServicePreference == 'kagi') { 29 translateUrl = getTranslatorLinkKagi( 30 text, 31 targetLangCode, 32 sourceLanguage, 33 ) 34 } else if (translationServicePreference == 'papago') { 35 translateUrl = getTranslatorLinkPapago( 36 text, 37 targetLangCode, 38 sourceLanguage, 39 ) 40 } else if (translationServicePreference == 'libreTranslate') { 41 translateUrl = getTranslatorLinkLibreTranslate( 42 text, 43 targetLangCode, 44 sourceLanguage, 45 ) 46 } else { 47 translateUrl = getTranslatorLink(text, targetLangCode, sourceLanguage) 48 } 49 50 if (IS_ANDROID && translationServicePreference == 'google') { 51 try { 52 // use `getApplicationIconAsync` to determine if the translate app is installed 53 if ( 54 !(await IntentLauncher.getApplicationIconAsync( 55 'com.google.android.apps.translate', 56 )) 57 ) { 58 throw new Error('Translate app not installed') 59 } 60 61 // TODO: this should only be called one at a time, use something like 62 // RQ's `scope` - otherwise can trigger the browser to open unexpectedly when the call throws -sfn 63 await IntentLauncher.startActivityAsync( 64 'android.intent.action.PROCESS_TEXT', 65 { 66 type: 'text/plain', 67 extra: { 68 'android.intent.extra.PROCESS_TEXT': text, 69 'android.intent.extra.PROCESS_TEXT_READONLY': true, 70 }, 71 // note: to skip the intermediate app select, we need to specify a 72 // `className`. however, this isn't safe to hardcode, we'd need to query the 73 // package manager for the correct activity. this requires native code, so 74 // skip for now -sfn 75 // packageName: 'com.google.android.apps.translate', 76 // className: 'com.google.android.apps.translate.TranslateActivity', 77 }, 78 ) 79 } catch (err) { 80 if (__DEV__) console.error(err) 81 // most likely means they don't have the translate app 82 await openLink(translateUrl) 83 } 84 } else { 85 await openLink(translateUrl) 86 } 87 }, 88 [openLink, translationServicePreference], 89 ) 90}