Call useTranslate once and pass down translate function
This commit is contained in:
@@ -4,6 +4,7 @@ import {Trans, useLingui} from '@lingui/react/macro'
|
|||||||
|
|
||||||
import {HITSLOP_30} from '#/lib/constants'
|
import {HITSLOP_30} from '#/lib/constants'
|
||||||
import {useTranslate} from '#/lib/translation'
|
import {useTranslate} from '#/lib/translation'
|
||||||
|
import {type TranslationFunction} from '#/lib/translation/types'
|
||||||
import {codeToLanguageName, languageName} from '#/locale/helpers'
|
import {codeToLanguageName, languageName} from '#/locale/helpers'
|
||||||
import {LANGUAGES} from '#/locale/languages'
|
import {LANGUAGES} from '#/locale/languages'
|
||||||
import {useLanguagePrefs} from '#/state/preferences'
|
import {useLanguagePrefs} from '#/state/preferences'
|
||||||
@@ -21,7 +22,7 @@ export function TranslatedPost({
|
|||||||
translationKey: string
|
translationKey: string
|
||||||
postText: string
|
postText: string
|
||||||
}) {
|
}) {
|
||||||
const {translationState} = useTranslate({key: translationKey})
|
const {translate, translationState} = useTranslate({key: translationKey})
|
||||||
|
|
||||||
if (translationState.status === 'loading') {
|
if (translationState.status === 'loading') {
|
||||||
return <TranslationLoading />
|
return <TranslationLoading />
|
||||||
@@ -30,7 +31,7 @@ export function TranslatedPost({
|
|||||||
if (translationState.status === 'success') {
|
if (translationState.status === 'success') {
|
||||||
return (
|
return (
|
||||||
<TranslationResult
|
<TranslationResult
|
||||||
translationKey={translationKey}
|
translate={translate}
|
||||||
postText={postText}
|
postText={postText}
|
||||||
sourceLanguage={translationState.sourceLanguage}
|
sourceLanguage={translationState.sourceLanguage}
|
||||||
translatedText={translationState.translatedText}
|
translatedText={translationState.translatedText}
|
||||||
@@ -57,12 +58,12 @@ function TranslationLoading() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function TranslationResult({
|
function TranslationResult({
|
||||||
translationKey,
|
translate,
|
||||||
postText,
|
postText,
|
||||||
sourceLanguage,
|
sourceLanguage,
|
||||||
translatedText,
|
translatedText,
|
||||||
}: {
|
}: {
|
||||||
translationKey: string
|
translate: TranslationFunction
|
||||||
postText: string
|
postText: string
|
||||||
sourceLanguage: string | null
|
sourceLanguage: string | null
|
||||||
translatedText: string
|
translatedText: string
|
||||||
@@ -92,7 +93,7 @@ function TranslationResult({
|
|||||||
</Text>
|
</Text>
|
||||||
<TranslationLanguageSelect
|
<TranslationLanguageSelect
|
||||||
sourceLanguage={sourceLanguage}
|
sourceLanguage={sourceLanguage}
|
||||||
translationKey={translationKey}
|
translate={translate}
|
||||||
postText={postText}
|
postText={postText}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
@@ -106,18 +107,17 @@ function TranslationResult({
|
|||||||
}
|
}
|
||||||
|
|
||||||
function TranslationLanguageSelect({
|
function TranslationLanguageSelect({
|
||||||
translationKey,
|
translate,
|
||||||
postText,
|
postText,
|
||||||
sourceLanguage,
|
sourceLanguage,
|
||||||
}: {
|
}: {
|
||||||
translationKey: string
|
translate: TranslationFunction
|
||||||
postText: string
|
postText: string
|
||||||
sourceLanguage: string
|
sourceLanguage: string
|
||||||
}) {
|
}) {
|
||||||
const ax = useAnalytics()
|
const ax = useAnalytics()
|
||||||
const {t: l} = useLingui()
|
const {t: l} = useLingui()
|
||||||
const langPrefs = useLanguagePrefs()
|
const langPrefs = useLanguagePrefs()
|
||||||
const {translate} = useTranslate({key: translationKey})
|
|
||||||
|
|
||||||
const items = useMemo(
|
const items = useMemo(
|
||||||
() =>
|
() =>
|
||||||
|
|||||||
@@ -1,28 +1,14 @@
|
|||||||
import {createContext} from 'react'
|
import {createContext} from 'react'
|
||||||
|
|
||||||
import {type TranslationState} from './types'
|
import {type TranslationFunctionParams, type TranslationState} from './types'
|
||||||
|
|
||||||
export const Context = createContext<{
|
export const Context = createContext<{
|
||||||
translationState: Record<string, TranslationState>
|
translationState: Record<string, TranslationState>
|
||||||
translate: (parameters: {
|
translate: (
|
||||||
key: string
|
parameters: TranslationFunctionParams & {
|
||||||
/**
|
key: string
|
||||||
* The text to be translated.
|
},
|
||||||
*/
|
) => Promise<void>
|
||||||
text: string
|
|
||||||
/**
|
|
||||||
* The language to translate the text into.
|
|
||||||
*/
|
|
||||||
targetLangCode: string
|
|
||||||
/**
|
|
||||||
* The source language of the text. Will auto-detect if not provided.
|
|
||||||
*/
|
|
||||||
sourceLangCode?: string
|
|
||||||
/**
|
|
||||||
* Whether to force the use of Google Translate. Default is false.
|
|
||||||
*/
|
|
||||||
forceGoogleTranslate?: boolean
|
|
||||||
}) => Promise<void>
|
|
||||||
clearTranslation: (key: string) => void
|
clearTranslation: (key: string) => void
|
||||||
acquireTranslation: (key: string) => () => void
|
acquireTranslation: (key: string) => () => void
|
||||||
} | null>(null)
|
} | null>(null)
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate'
|
|||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
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'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts on-device translation via @bsky.app/expo-translate-text.
|
* Attempts on-device translation via @bsky.app/expo-translate-text.
|
||||||
@@ -94,12 +94,7 @@ export function useTranslate({key}: {key: string}) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
const translate = useCallback(
|
const translate = useCallback(
|
||||||
async (params: {
|
async (params: TranslationFunctionParams) => {
|
||||||
text: string
|
|
||||||
targetLangCode: string
|
|
||||||
sourceLangCode?: string
|
|
||||||
forceGoogleTranslate?: boolean
|
|
||||||
}) => {
|
|
||||||
return context.translate({...params, key})
|
return context.translate({...params, key})
|
||||||
},
|
},
|
||||||
[key, context],
|
[key, context],
|
||||||
|
|||||||
@@ -9,3 +9,26 @@ export type TranslationState =
|
|||||||
sourceLanguage: TranslationTaskResult['sourceLanguage']
|
sourceLanguage: TranslationTaskResult['sourceLanguage']
|
||||||
targetLanguage: TranslationTaskResult['targetLanguage']
|
targetLanguage: TranslationTaskResult['targetLanguage']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type TranslationFunctionParams = {
|
||||||
|
/**
|
||||||
|
* The text to be translated.
|
||||||
|
*/
|
||||||
|
text: string
|
||||||
|
/**
|
||||||
|
* The language to translate the text into.
|
||||||
|
*/
|
||||||
|
targetLangCode: string
|
||||||
|
/**
|
||||||
|
* The source language of the text. Will auto-detect if not provided.
|
||||||
|
*/
|
||||||
|
sourceLangCode?: string
|
||||||
|
/**
|
||||||
|
* Whether to force the use of Google Translate. Default is false.
|
||||||
|
*/
|
||||||
|
forceGoogleTranslate?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TranslationFunction = (
|
||||||
|
parameters: TranslationFunctionParams,
|
||||||
|
) => Promise<void>
|
||||||
|
|||||||
Reference in New Issue
Block a user