Skip to content

Commit be97ecc

Browse files
fix(mobile): Android dictation works with regional preferences set
Android appends Unicode extensions to the system locale when a regional preference is set (en-US-u-mu-celsius for the temperature unit). ML Kit reports every speech mode UNAVAILABLE for such tags, so the composer hid its microphone button. Strip the extensions before asking ML Kit.
1 parent dd5f98e commit be97ecc

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/GoogleVoiceTranscriber.kt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ internal class VoiceTranscriptionException(
2222
cause: Throwable? = null
2323
) : Exception(message, cause)
2424

25+
// ML Kit reports every mode UNAVAILABLE for tags with Unicode extensions, which Android adds
26+
// for regional preferences such as the temperature unit (en-US-u-mu-celsius).
27+
internal fun speechLocale(systemLocale: Locale): Locale = systemLocale.stripExtensions()
28+
2529
private val recognitionModes = listOf(
2630
SpeechRecognizerOptions.Mode.MODE_ADVANCED,
2731
SpeechRecognizerOptions.Mode.MODE_BASIC

‎apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/T3VoiceTranscriptionModule.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class T3VoiceTranscriptionModule : Module() {
3232
} else {
3333
withContext(Dispatchers.IO) {
3434
val context = requireNotNull(appContext.reactContext)
35-
val locale = Resources.getSystem().configuration.locales[0]
35+
val locale = speechLocale(Resources.getSystem().configuration.locales[0])
3636
isGoogleVoiceTranscriptionSupported { MlKitSpeechClient(context, locale, it) }
3737
}
3838
}
@@ -47,7 +47,7 @@ class T3VoiceTranscriptionModule : Module() {
4747
)
4848
}
4949
val context = requireNotNull(appContext.reactContext)
50-
val locale = Resources.getSystem().configuration.locales[0]
50+
val locale = speechLocale(Resources.getSystem().configuration.locales[0])
5151
val mode = prepareGoogleVoiceTranscription { MlKitSpeechClient(context, locale, it) }
5252
mapOf("locale" to locale.toLanguageTag(), "mode" to mode)
5353
}

‎apps/mobile/modules/t3-voice-transcription/android/src/test/java/expo/modules/t3voicetranscription/GoogleVoiceTranscriberTest.kt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.google.mlkit.genai.common.FeatureStatus
44
import com.google.mlkit.genai.common.GenAiException
55
import com.google.mlkit.genai.speechrecognition.SpeechRecognizerOptions
66
import com.google.mlkit.genai.speechrecognition.SpeechRecognizerResponse
7+
import java.util.Locale
78
import kotlinx.coroutines.CompletableDeferred
89
import kotlinx.coroutines.cancelAndJoin
910
import kotlinx.coroutines.flow.flow
@@ -14,6 +15,18 @@ import org.junit.Assert.assertTrue
1415
import org.junit.Test
1516

1617
class GoogleVoiceTranscriberTest {
18+
@Test
19+
fun dropsRegionalPreferenceExtensionsFromTheSpeechLocale() {
20+
for ((system, expected) in listOf(
21+
"en-US-u-mu-celsius" to "en-US",
22+
"en-GB-u-fw-mon-mu-celsius" to "en-GB",
23+
"ar-EG-u-nu-latn" to "ar-EG",
24+
"zh-Hant-TW" to "zh-Hant-TW"
25+
)) {
26+
assertEquals(expected, speechLocale(Locale.forLanguageTag(system)).toLanguageTag())
27+
}
28+
}
29+
1730
@Test
1831
fun preservesUtterancesAndTheirSpacingWithoutDuplicatingHypotheses() = runBlocking {
1932
for ((finals, trailing, expected) in listOf(

0 commit comments

Comments
 (0)