diff --git a/src/analytics/metrics/types.ts b/src/analytics/metrics/types.ts index 494c7ee1d9..8ad54fbcac 100644 --- a/src/analytics/metrics/types.ts +++ b/src/analytics/metrics/types.ts @@ -713,14 +713,42 @@ export type Events = { textLength: number } 'translate:result': { - method: 'on-device' | 'fallback-alert' + success: boolean os: Platform['OS'] - sourceSelection: 'automatic' | 'manual' - sourceLanguage: string | null - targetLanguage: string - - /* Only relevant to posts */ - postLanguages?: string[] + /** + * The languages the content might be in, such as the user-supplied + * language codes on posts. Currently only available on posts. + */ + possibleSourceLanguages: string[] | undefined + /** + * The language we expected the content to be in. This could be based on + * user selection or on our confidence in the detected language. This is + * nullable because we may not always have an expected source language. + */ + expectedSourceLanguage: string | null + /** + * This is the user's configured primary language, which is always defined. + */ + expectedTargetLanguage: string + /** + * The language the translation result was actually in. This is nullable + * because the translation could have failed, in which case we won't have a + * result source language. + */ + resultSourceLanguage: string | null + /** + * The language the translation result was translated into. This should be + * the same as `expectedTargetLanguage`, but we include it for completeness + * and in case there are any edge cases where they differ. This is nullable + * because if the translation failed, we won't have a result target + * language. + */ + resultTargetLanguage: string | null + /** + * The length of the text being translated. We assume shorter texts are + * more likely to have inaccurate translations. + */ + textLength: number } 'translate:override': { os: Platform['OS'] diff --git a/src/components/Post/Translated/index.tsx b/src/components/Post/Translated/index.tsx index 1100c81bfa..444bdb446d 100644 --- a/src/components/Post/Translated/index.tsx +++ b/src/components/Post/Translated/index.tsx @@ -408,7 +408,6 @@ function TranslationLanguageSelect({ text: postText, targetLangCode: langPrefs.primaryLanguage, sourceLangCode, - sourceSelection: 'manual', }) } diff --git a/src/lib/translation/index.tsx b/src/lib/translation/index.tsx index 9f41a4e894..5d8e1989f2 100644 --- a/src/lib/translation/index.tsx +++ b/src/lib/translation/index.tsx @@ -212,13 +212,12 @@ export function Provider({children}: React.PropsWithChildren) { key, text, targetLangCode, - sourceLangCode, - sourceSelection = 'automatic', + sourceLangCode: expectedSourceLanguage, postLangCodes, ...options }) => { if (options?.forceGoogleTranslate || !HAS_ON_DEVICE_TRANSLATION) { - await googleTranslate(text, targetLangCode, sourceLangCode) + await googleTranslate(text, targetLangCode, expectedSourceLanguage) return } @@ -233,15 +232,17 @@ export function Provider({children}: React.PropsWithChildren) { const result = await attemptTranslation( text, targetLangCode, - sourceLangCode, + expectedSourceLanguage, ) ax.metric('translate:result', { - method: 'on-device', + success: true, os: Platform.OS, - sourceSelection, - sourceLanguage: result.sourceLanguage, - targetLanguage: result.targetLanguage, - postLanguages: postLangCodes, + possibleSourceLanguages: postLangCodes, + expectedSourceLanguage: expectedSourceLanguage ?? null, + expectedTargetLanguage: targetLangCode, + resultSourceLanguage: result.sourceLanguage, + resultTargetLanguage: result.targetLanguage, + textLength: text.length, }) if (!IS_ANDROID) { LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut) @@ -261,12 +262,14 @@ export function Provider({children}: React.PropsWithChildren) { // On-device translation failed (language pack missing or user // dismissed the download prompt). ax.metric('translate:result', { - method: 'fallback-alert', + success: false, os: Platform.OS, - sourceSelection, - sourceLanguage: sourceLangCode ?? null, - targetLanguage: targetLangCode, - postLanguages: postLangCodes, + possibleSourceLanguages: postLangCodes, + expectedSourceLanguage: expectedSourceLanguage ?? null, + expectedTargetLanguage: targetLangCode, + resultSourceLanguage: null, + resultTargetLanguage: null, + textLength: text.length, }) let errorMessage = l`Device failed to translate :(` if (!IS_ANDROID) { diff --git a/src/lib/translation/types.ts b/src/lib/translation/types.ts index 352df445f6..c2ff9f0c35 100644 --- a/src/lib/translation/types.ts +++ b/src/lib/translation/types.ts @@ -24,13 +24,12 @@ export type TranslationFunctionParams = { */ targetLangCode: string /** - * The source language of the text. Will auto-detect if not provided. + * 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 + * means the user selected a source language, or we were certain of the + * source language and want to specify it explicitly. */ sourceLangCode?: string - /** - * Whether we auto-detected the language or it was selected manually. Defaults to 'automatic'. - */ - sourceSelection?: 'automatic' | 'manual' } export type TranslationOptions = {