Move forceGoogleTranslate param to useTranslate hook
This commit is contained in:
@@ -137,6 +137,7 @@ let PostMenuItems = ({
|
|||||||
const openLink = useOpenLink()
|
const openLink = useOpenLink()
|
||||||
const {clearTranslation, translate, translationState} = useTranslate({
|
const {clearTranslation, translate, translationState} = useTranslate({
|
||||||
key: post.uri,
|
key: post.uri,
|
||||||
|
forceGoogleTranslate,
|
||||||
})
|
})
|
||||||
const navigation = useNavigation<NavigationProp>()
|
const navigation = useNavigation<NavigationProp>()
|
||||||
const {mutedWordsDialogControl} = useGlobalDialogsControlContext()
|
const {mutedWordsDialogControl} = useGlobalDialogsControlContext()
|
||||||
@@ -261,7 +262,6 @@ let PostMenuItems = ({
|
|||||||
void translate({
|
void translate({
|
||||||
text: record.text,
|
text: record.text,
|
||||||
targetLangCode: langPrefs.primaryLanguage,
|
targetLangCode: langPrefs.primaryLanguage,
|
||||||
forceGoogleTranslate,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ export const Context = createContext<{
|
|||||||
translate: (
|
translate: (
|
||||||
parameters: TranslationFunctionParams & {
|
parameters: TranslationFunctionParams & {
|
||||||
key: string
|
key: string
|
||||||
|
forceGoogleTranslate: boolean
|
||||||
},
|
},
|
||||||
) => Promise<void>
|
) => Promise<void>
|
||||||
clearTranslation: (key: string) => void
|
clearTranslation: (key: string) => void
|
||||||
|
|||||||
@@ -77,7 +77,13 @@ async function attemptTranslation(
|
|||||||
*
|
*
|
||||||
* Web uses index.web.ts which always opens Google Translate.
|
* 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)
|
const context = useContext(Context)
|
||||||
if (!context) {
|
if (!context) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -94,14 +100,14 @@ export function useTranslate({key}: {key: string}) {
|
|||||||
|
|
||||||
const translate = useCallback(
|
const translate = useCallback(
|
||||||
async (params: TranslationFunctionParams) => {
|
async (params: TranslationFunctionParams) => {
|
||||||
return context.translate({...params, key})
|
return context.translate({...params, key, forceGoogleTranslate})
|
||||||
},
|
},
|
||||||
[key, context],
|
[context, forceGoogleTranslate, key],
|
||||||
)
|
)
|
||||||
|
|
||||||
const clearTranslation = useCallback(
|
const clearTranslation = useCallback(
|
||||||
() => context.clearTranslation(key),
|
() => context.clearTranslation(key),
|
||||||
[key, context],
|
[context, key],
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import {useCallback, useContext, useMemo} from 'react'
|
|||||||
import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate'
|
import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate'
|
||||||
import {useAnalytics} from '#/analytics'
|
import {useAnalytics} from '#/analytics'
|
||||||
import {Context} from './context'
|
import {Context} from './context'
|
||||||
import {type TranslationState} from './types'
|
import {type TranslationFunctionParams, type TranslationState} from './types'
|
||||||
|
|
||||||
const translationState: Record<string, TranslationState> = {}
|
const translationState: Record<string, TranslationState> = {}
|
||||||
const acquireTranslation = (_key: string) => {
|
const acquireTranslation = (_key: string) => {
|
||||||
@@ -14,7 +14,12 @@ const clearTranslation = (_key: string) => {}
|
|||||||
/**
|
/**
|
||||||
* Web always opens Google Translate.
|
* Web always opens Google Translate.
|
||||||
*/
|
*/
|
||||||
export function useTranslate(key: string) {
|
export function useTranslate({
|
||||||
|
key,
|
||||||
|
}: {
|
||||||
|
key: string
|
||||||
|
forceGoogleTranslate?: boolean
|
||||||
|
}) {
|
||||||
const context = useContext(Context)
|
const context = useContext(Context)
|
||||||
if (!context) {
|
if (!context) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -24,32 +29,16 @@ export function useTranslate(key: string) {
|
|||||||
|
|
||||||
// Always call hooks in consistent order
|
// Always call hooks in consistent order
|
||||||
const translate = useCallback(
|
const translate = useCallback(
|
||||||
async (params: {
|
async (params: TranslationFunctionParams) => {
|
||||||
text: string
|
return context.translate({...params, key, forceGoogleTranslate: true})
|
||||||
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})
|
|
||||||
},
|
},
|
||||||
[key, context],
|
[key, context],
|
||||||
)
|
)
|
||||||
|
|
||||||
const clearTranslation = useCallback(() => {
|
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)
|
return context.clearTranslation(key)
|
||||||
}, [key, context])
|
}, [key, context])
|
||||||
|
|
||||||
// If a key is provided, return wrapped versions that automatically use the key
|
|
||||||
if (key) {
|
|
||||||
return {
|
return {
|
||||||
translationState: context.translationState[key] ?? {
|
translationState: context.translationState[key] ?? {
|
||||||
status: 'idle' as const,
|
status: 'idle' as const,
|
||||||
@@ -59,9 +48,6 @@ export function useTranslate(key: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return context
|
|
||||||
}
|
|
||||||
|
|
||||||
export function Provider({children}: React.PropsWithChildren<unknown>) {
|
export function Provider({children}: React.PropsWithChildren<unknown>) {
|
||||||
const ax = useAnalytics()
|
const ax = useAnalytics()
|
||||||
const googleTranslate = useGoogleTranslate()
|
const googleTranslate = useGoogleTranslate()
|
||||||
|
|||||||
@@ -23,10 +23,6 @@ export type TranslationFunctionParams = {
|
|||||||
* The source language of the text. Will auto-detect if not provided.
|
* The source language of the text. Will auto-detect if not provided.
|
||||||
*/
|
*/
|
||||||
sourceLangCode?: string
|
sourceLangCode?: string
|
||||||
/**
|
|
||||||
* Whether to force the use of Google Translate. Default is false.
|
|
||||||
*/
|
|
||||||
forceGoogleTranslate?: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TranslationFunction = (
|
export type TranslationFunction = (
|
||||||
|
|||||||
Reference in New Issue
Block a user