Disable animations for translation events on Android (#10013)
This commit is contained in:
@@ -9,7 +9,7 @@ import {useFocusEffect} from '@react-navigation/native'
|
|||||||
import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate'
|
import {useGoogleTranslate} from '#/lib/hooks/useGoogleTranslate'
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {useAnalytics} from '#/analytics'
|
import {useAnalytics} from '#/analytics'
|
||||||
import {HAS_ON_DEVICE_TRANSLATION} from '#/env'
|
import {HAS_ON_DEVICE_TRANSLATION, IS_ANDROID, IS_IOS} from '#/env'
|
||||||
import {Context} from './context'
|
import {Context} from './context'
|
||||||
import {type TranslationFunctionParams, type TranslationState} from './types'
|
import {type TranslationFunctionParams, type TranslationState} from './types'
|
||||||
import {guessLanguage} from './utils'
|
import {guessLanguage} from './utils'
|
||||||
@@ -34,18 +34,16 @@ async function attemptTranslation(
|
|||||||
// Note that Android only supports two-character language codes and will fail
|
// Note that Android only supports two-character language codes and will fail
|
||||||
// on other input.
|
// on other input.
|
||||||
// https://developers.google.com/android/reference/com/google/mlkit/nl/translate/TranslateLanguage
|
// https://developers.google.com/android/reference/com/google/mlkit/nl/translate/TranslateLanguage
|
||||||
let targetLangCode =
|
let targetLangCode = IS_ANDROID
|
||||||
Platform.OS === 'android'
|
|
||||||
? targetLangCodeOriginal.split('-')[0]
|
? targetLangCodeOriginal.split('-')[0]
|
||||||
: targetLangCodeOriginal
|
: targetLangCodeOriginal
|
||||||
const sourceLangCode =
|
const sourceLangCode = IS_ANDROID
|
||||||
Platform.OS === 'android'
|
|
||||||
? sourceLangCodeOriginal?.split('-')[0]
|
? sourceLangCodeOriginal?.split('-')[0]
|
||||||
: sourceLangCodeOriginal
|
: sourceLangCodeOriginal
|
||||||
|
|
||||||
// Special cases for regional languages since iOS differentiates and missing
|
// Special cases for regional languages since iOS differentiates and missing
|
||||||
// language packs must be downloaded and installed.
|
// language packs must be downloaded and installed.
|
||||||
if (Platform.OS === 'ios') {
|
if (IS_IOS) {
|
||||||
const deviceLocales = getLocales()
|
const deviceLocales = getLocales()
|
||||||
const primaryLanguageTag = deviceLocales[0]?.languageTag
|
const primaryLanguageTag = deviceLocales[0]?.languageTag
|
||||||
switch (targetLangCodeOriginal) {
|
switch (targetLangCodeOriginal) {
|
||||||
@@ -192,7 +190,9 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
|
|||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const clearTranslation = useCallback((key: string) => {
|
const clearTranslation = useCallback((key: string) => {
|
||||||
|
if (!IS_ANDROID) {
|
||||||
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
|
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
|
||||||
|
}
|
||||||
setTranslationState(prev => {
|
setTranslationState(prev => {
|
||||||
delete prev[key]
|
delete prev[key]
|
||||||
return {...prev}
|
return {...prev}
|
||||||
@@ -224,8 +224,9 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Translate after the next state change.
|
if (!IS_ANDROID) {
|
||||||
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
|
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
|
||||||
|
}
|
||||||
setTranslationState(prev => ({
|
setTranslationState(prev => ({
|
||||||
...prev,
|
...prev,
|
||||||
[key]: {status: 'loading'},
|
[key]: {status: 'loading'},
|
||||||
@@ -242,7 +243,9 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
|
|||||||
sourceLanguage: result.sourceLanguage,
|
sourceLanguage: result.sourceLanguage,
|
||||||
targetLanguage: result.targetLanguage,
|
targetLanguage: result.targetLanguage,
|
||||||
})
|
})
|
||||||
|
if (!IS_ANDROID) {
|
||||||
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
|
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
|
||||||
|
}
|
||||||
setTranslationState(prev => ({
|
setTranslationState(prev => ({
|
||||||
...prev,
|
...prev,
|
||||||
[key]: {
|
[key]: {
|
||||||
@@ -263,7 +266,9 @@ export function Provider({children}: React.PropsWithChildren<unknown>) {
|
|||||||
targetLanguage: targetLangCode,
|
targetLanguage: targetLangCode,
|
||||||
})
|
})
|
||||||
let errorMessage = l`Device failed to translate :(`
|
let errorMessage = l`Device failed to translate :(`
|
||||||
|
if (!IS_ANDROID) {
|
||||||
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
|
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
|
||||||
|
}
|
||||||
setTranslationState(prev => ({
|
setTranslationState(prev => ({
|
||||||
...prev,
|
...prev,
|
||||||
[key]: {status: 'error', message: errorMessage},
|
[key]: {status: 'error', message: errorMessage},
|
||||||
|
|||||||
Reference in New Issue
Block a user