Update option name to forceGoogleTranslate

This commit is contained in:
DS Boyce
2026-03-03 11:38:22 -08:00
parent b7ab03bf18
commit 116784463a
13 changed files with 98 additions and 112 deletions
+11 -8
View File
@@ -1,16 +1,19 @@
import {createContext} from 'react'
import {type Options, type TranslationState} from './types'
import {type TranslationState} from './types'
export const Context = createContext<{
translationState: Record<string, TranslationState>
translate: (
key: string,
text: string,
targetLangCode: string,
sourceLangCode?: string,
options?: Options,
) => Promise<void>
translate: (parameters: {
key: string
text: string
targetLangCode: string
sourceLangCode?: string
/**
* Whether to force the use of Google Translate. Default is false.
*/
forceGoogleTranslate?: boolean
}) => Promise<void>
clearTranslation: (key: string) => void
acquireTranslation: (key: string) => () => void
} | null>(null)
+16 -12
View File
@@ -6,10 +6,9 @@ import {useFocusEffect} from '@react-navigation/native'
import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate'
import {logger} from '#/logger'
import {useLanguagePrefs} from '#/state/preferences'
import {useAnalytics} from '#/analytics'
import {Context} from './context'
import {type Options, type TranslationState} from './types'
import {type TranslationState} from './types'
/**
* Attempts on-device translation via @bsky.app/expo-translate-text.
@@ -111,7 +110,6 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
>({})
const [refCounts, setRefCounts] = useState<Record<string, number>>({})
const ax = useAnalytics()
const {primaryLanguage} = useLanguagePrefs()
const googleTranslate = useGoogleTranslate()
useEffect(() => {
@@ -163,14 +161,20 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
}, [])
const translate = useCallback(
async (
key: string,
text: string,
targetLangCode: string = primaryLanguage,
sourceLangCode?: string,
options?: Options,
) => {
if (options?.googleTranslate) {
async ({
key,
text,
targetLangCode,
sourceLangCode,
...options
}: {
key: string
text: string
targetLangCode: string
sourceLangCode?: string
forceGoogleTranslate?: boolean
}) => {
if (options?.forceGoogleTranslate) {
ax.metric('translate:result', {
method: 'google-translate',
os: Platform.OS,
@@ -225,7 +229,7 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
await googleTranslate(text, targetLangCode, sourceLangCode)
}
},
[ax, googleTranslate, primaryLanguage],
[ax, googleTranslate],
)
const ctx = useMemo(
+12 -11
View File
@@ -1,10 +1,9 @@
import {useCallback, useContext, useMemo} from 'react'
import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate'
import {useLanguagePrefs} from '#/state/preferences'
import {useAnalytics} from '#/analytics'
import {Context} from './context'
import {type Options, type TranslationState} from './types'
import {type TranslationState} from './types'
const translationState: Record<string, TranslationState> = {}
const acquireTranslation = (_key: string) => {
@@ -29,17 +28,19 @@ export function useTranslate() {
export function Provider({children}: React.PropsWithChildren<unknown>) {
const ax = useAnalytics()
const {primaryLanguage} = useLanguagePrefs()
const googleTranslate = useGoogleTranslate()
const translate = useCallback(
async (
_key: string,
text: string,
targetLangCode: string = primaryLanguage,
sourceLangCode?: string,
_options?: Options,
) => {
async ({
text,
targetLangCode,
sourceLangCode,
}: {
key: string
text: string
targetLangCode: string
sourceLangCode?: string
}) => {
ax.metric('translate:result', {
method: 'google-translate',
os: 'web',
@@ -48,7 +49,7 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
})
await googleTranslate(text, targetLangCode, sourceLangCode)
},
[ax, googleTranslate, primaryLanguage],
[ax, googleTranslate],
)
const ctx = useMemo(
-7
View File
@@ -9,10 +9,3 @@ export type TranslationState =
sourceLanguage: TranslationTaskResult['sourceLanguage']
targetLanguage: TranslationTaskResult['targetLanguage']
}
export type Options = {
/**
* Whether to force the use of Google Translate. Default is false.
*/
googleTranslate?: boolean
}