Just align naming conventions

This commit is contained in:
Eric Bailey
2026-03-17 13:20:02 -05:00
parent 389bea7c93
commit a00e5dd724
5 changed files with 63 additions and 36 deletions
+4 -4
View File
@@ -57,7 +57,7 @@ export function TranslatedPost({
const initialTranslationParams = useMemo<TranslationFunctionParams>(() => { const initialTranslationParams = useMemo<TranslationFunctionParams>(() => {
return { return {
text: record?.text || '', text: record?.text || '',
targetLangCode: langPrefs.primaryLanguage, expectedTargetLanguage: langPrefs.primaryLanguage,
possibleSourceLanguages: getPostLanguageTags(post), possibleSourceLanguages: getPostLanguageTags(post),
} }
}, [post, record, langPrefs]) }, [post, record, langPrefs])
@@ -406,13 +406,13 @@ function TranslationLanguageSelect({
os: Platform.OS, os: Platform.OS,
possibleSourceLanguages: initialTranslationParams.possibleSourceLanguages, possibleSourceLanguages: initialTranslationParams.possibleSourceLanguages,
expectedSourceLanguage: sourceLangCode, expectedSourceLanguage: sourceLangCode,
expectedTargetLanguage: initialTranslationParams.targetLangCode, expectedTargetLanguage: initialTranslationParams.expectedTargetLanguage,
resultSourceLanguage, resultSourceLanguage,
}) })
void translate({ void translate({
text: initialTranslationParams.text, text: initialTranslationParams.text,
targetLangCode: initialTranslationParams.targetLangCode, expectedTargetLanguage: initialTranslationParams.expectedTargetLanguage,
sourceLangCode, expectedSourceLanguage: sourceLangCode,
possibleSourceLanguages: initialTranslationParams.possibleSourceLanguages, possibleSourceLanguages: initialTranslationParams.possibleSourceLanguages,
}) })
} }
@@ -277,7 +277,7 @@ let PostMenuItems = ({
const onPressTranslate = () => { const onPressTranslate = () => {
void translate({ void translate({
text: record.text, text: record.text,
targetLangCode: langPrefs.primaryLanguage, expectedTargetLanguage: langPrefs.primaryLanguage,
possibleSourceLanguages: getPostLanguageTags(post), possibleSourceLanguages: getPostLanguageTags(post),
}) })
} }
+32 -19
View File
@@ -120,11 +120,15 @@ export function useTranslate({
const translate = useCallback( const translate = useCallback(
async (params: TranslationFunctionParams) => { async (params: TranslationFunctionParams) => {
return context.translate({ return context.translate(
...params, {
key, ...params,
forceGoogleTranslate, },
}) {
key,
forceGoogleTranslate,
},
)
}, },
[context, forceGoogleTranslate, key], [context, forceGoogleTranslate, key],
) )
@@ -206,23 +210,32 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
}, []) }, [])
const translate = useCallback<ContextType['translate']>( const translate = useCallback<ContextType['translate']>(
async ({ async (
key, {
text, text,
targetLangCode, expectedTargetLanguage,
sourceLangCode: expectedSourceLanguage, expectedSourceLanguage,
possibleSourceLanguages, possibleSourceLanguages,
...options forceGoogleTranslate: forceGoogleTranslateOverride,
}) => { },
{key, forceGoogleTranslate},
) => {
ax.metric('translate', { ax.metric('translate', {
os: Platform.OS, os: Platform.OS,
possibleSourceLanguages, possibleSourceLanguages,
expectedTargetLanguage: targetLangCode, expectedTargetLanguage: expectedTargetLanguage,
textLength: text.length, textLength: text.length,
}) })
if (options?.forceGoogleTranslate || !HAS_ON_DEVICE_TRANSLATION) { const shouldForceGoogleTranslate =
await googleTranslate(text, targetLangCode, expectedSourceLanguage) forceGoogleTranslateOverride ?? forceGoogleTranslate
if (shouldForceGoogleTranslate || !HAS_ON_DEVICE_TRANSLATION) {
await googleTranslate(
text,
expectedTargetLanguage,
expectedSourceLanguage,
)
return return
} }
@@ -236,7 +249,7 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
try { try {
const result = await attemptTranslation( const result = await attemptTranslation(
text, text,
targetLangCode, expectedTargetLanguage,
expectedSourceLanguage, expectedSourceLanguage,
) )
ax.metric('translate:result', { ax.metric('translate:result', {
@@ -244,7 +257,7 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
os: Platform.OS, os: Platform.OS,
possibleSourceLanguages, possibleSourceLanguages,
expectedSourceLanguage: expectedSourceLanguage ?? null, expectedSourceLanguage: expectedSourceLanguage ?? null,
expectedTargetLanguage: targetLangCode, expectedTargetLanguage,
resultSourceLanguage: result.sourceLanguage, resultSourceLanguage: result.sourceLanguage,
resultTargetLanguage: result.targetLanguage, resultTargetLanguage: result.targetLanguage,
textLength: text.length, textLength: text.length,
@@ -271,7 +284,7 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
os: Platform.OS, os: Platform.OS,
possibleSourceLanguages, possibleSourceLanguages,
expectedSourceLanguage: expectedSourceLanguage ?? null, expectedSourceLanguage: expectedSourceLanguage ?? null,
expectedTargetLanguage: targetLangCode, expectedTargetLanguage,
resultSourceLanguage: null, resultSourceLanguage: null,
resultTargetLanguage: null, resultTargetLanguage: null,
textLength: text.length, textLength: text.length,
+21 -8
View File
@@ -33,11 +33,15 @@ export function useTranslate({key}: TranslationOptions) {
// Always call hooks in consistent order // Always call hooks in consistent order
const translate = useCallback( const translate = useCallback(
async (params: TranslationFunctionParams) => { async (params: TranslationFunctionParams) => {
return context.translate({ return context.translate(
...params, {
key, ...params,
forceGoogleTranslate: true, },
}) {
key,
forceGoogleTranslate: true,
},
)
}, },
[key, context], [key, context],
) )
@@ -60,14 +64,23 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
const googleTranslate = useGoogleTranslate() const googleTranslate = useGoogleTranslate()
const translate = useCallback<ContextType['translate']>( const translate = useCallback<ContextType['translate']>(
async ({text, targetLangCode, sourceLangCode, possibleSourceLanguages}) => { async ({
text,
expectedTargetLanguage,
expectedSourceLanguage,
possibleSourceLanguages,
}) => {
ax.metric('translate', { ax.metric('translate', {
os: 'web', os: 'web',
possibleSourceLanguages, possibleSourceLanguages,
expectedTargetLanguage: targetLangCode, expectedTargetLanguage,
textLength: text.length, textLength: text.length,
}) })
await googleTranslate(text, targetLangCode, sourceLangCode) await googleTranslate(
text,
expectedTargetLanguage,
expectedSourceLanguage,
)
}, },
[ax, googleTranslate], [ax, googleTranslate],
) )
+5 -4
View File
@@ -22,14 +22,14 @@ export type TranslationFunctionParams = {
/** /**
* The language to translate the text into. * The language to translate the text into.
*/ */
targetLangCode: string expectedTargetLanguage: string
/** /**
* We auto-detect the source language by default, but the user has the option * We auto-detect the source language by default, but the user has the option
* to specify a source language if they want to. If this value is present, it * to specify a source language if they want to. If this value is present, it
* means the user selected a source language, or we were certain of the * means the user selected a source language, or we were certain of the
* source language and want to specify it explicitly. * source language and want to specify it explicitly.
*/ */
sourceLangCode?: string expectedSourceLanguage?: string
/** /**
* The languages the content might be in, such as the user-supplied * The languages the content might be in, such as the user-supplied
* language codes on posts. Currently only available on posts. * language codes on posts. Currently only available on posts.
@@ -53,13 +53,14 @@ export type TranslationOptions = {
} }
export type TranslationFunction = ( export type TranslationFunction = (
parameters: TranslationFunctionParams, params: TranslationFunctionParams,
) => Promise<void> ) => Promise<void>
export type ContextType = { export type ContextType = {
translationState: Record<string, TranslationState> translationState: Record<string, TranslationState>
translate: ( translate: (
parameters: TranslationFunctionParams & TranslationOptions, params: TranslationFunctionParams,
options: TranslationOptions,
) => Promise<void> ) => Promise<void>
clearTranslation: (key: string) => void clearTranslation: (key: string) => void
acquireTranslation: (key: string) => () => void acquireTranslation: (key: string) => () => void