Skip to content

Commit 8b65cb8

Browse files
gabrielschclaude
andcommitted
feat(mobile): choose the dictation language for voice input
Voice input always transcribed in the device language. Add a Dictation language choice under Settings → Microphone; unset keeps following the device language. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e34067e commit 8b65cb8

9 files changed

Lines changed: 93 additions & 6 deletions

File tree

‎apps/mobile/src/features/settings/SettingsMicrophoneRouteScreen.tsx‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useSafeAreaInsets } from "react-native-safe-area-context";
1010
import { type AppSymbolName } from "../../components/ui/AppSymbol";
1111
import { AppText as Text } from "../../components/ui/AppText";
1212
import { cn } from "../../lib/cn";
13+
import { DICTATION_LANGUAGES } from "../../lib/dictationLanguage";
1314
import { makePermissionStatusAtom } from "../../lib/permissionStatusAtom";
1415
import {
1516
isMicrophoneDevice,
@@ -22,6 +23,7 @@ import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/
2223
import { ListRowLayout } from "../../components/ui/ListRowLayout";
2324
import { ThreadSwipeable } from "../home/thread-swipe-actions";
2425
import { ListActionRow as SettingsActionRow } from "../../components/ui/ListActionRow";
26+
import { ListChoiceRow as SettingsChoiceRow } from "../../components/ui/ListChoiceRow";
2527
import { SettingsDragHandle } from "./components/SettingsDragHandle";
2628
import { SettingsSection } from "./components/SettingsSection";
2729

@@ -96,6 +98,9 @@ export function SettingsMicrophoneRouteScreen() {
9698
const entries = microphoneOrder(
9799
AsyncResult.isSuccess(preferencesResult) ? preferencesResult.value.microphones : undefined,
98100
);
101+
const dictationLocale = AsyncResult.isSuccess(preferencesResult)
102+
? (preferencesResult.value.dictationLocale ?? null)
103+
: null;
99104
const { permission, request: requestPermission } = useMicrophonePermission();
100105
const [drag, setDrag] = useState<{ readonly key: string; readonly translation: number } | null>(
101106
null,
@@ -201,6 +206,27 @@ export function SettingsMicrophoneRouteScreen() {
201206
devices are added by name after you use voice input with them connected; until then, the
202207
Other row ranks them. Swipe a device left to forget it.
203208
</Text>
209+
<SettingsSection title="Dictation language">
210+
<SettingsChoiceRow
211+
label="Device language"
212+
selected={ready && dictationLocale === null}
213+
disabled={!ready}
214+
onPress={() => savePreferences({ dictationLocale: null })}
215+
/>
216+
{DICTATION_LANGUAGES.map((language) => (
217+
<SettingsChoiceRow
218+
key={language.locale}
219+
label={language.label}
220+
selected={dictationLocale === language.locale}
221+
separated
222+
disabled={!ready}
223+
onPress={() => savePreferences({ dictationLocale: language.locale })}
224+
/>
225+
))}
226+
</SettingsSection>
227+
<Text className="px-2 text-sm text-foreground-muted">
228+
Voice input transcribes on this device. The first use of a language downloads it.
229+
</Text>
204230
</ScrollView>
205231
</View>
206232
);

‎apps/mobile/src/features/settings/settings-search.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const SETTINGS_SEARCH_TERMS = {
2929
],
3030
SettingsNavigation: ["tabs", "tab order", "reorder", "hide tabs"],
3131
SettingsKeyboard: ["return key", "send message", "new line", "hardware keyboard"],
32-
SettingsMicrophone: ["voice", "dictation", "audio input", "bluetooth"],
32+
SettingsMicrophone: ["voice", "dictation", "audio input", "bluetooth", "language"],
3333
SettingsScheduledTasks: ["automations", "schedule", "cron", "recurring", "webhook"],
3434
SettingsProjectOverview: ["project", "overview", "checkouts", "delete project"],
3535
SettingsOrganization: [

‎apps/mobile/src/features/voice-input/VoiceInputProvider.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@ const voiceInputAtom = Atom.make((get) => {
9191
});
9292
const session: VoiceInputSession = new VoiceInputSession({
9393
recorder,
94-
getTranscriber: getLocalVoiceTranscriber,
94+
getTranscriber: () => {
95+
const preferences = registry.get(mobilePreferencesAtom);
96+
return getLocalVoiceTranscriber(
97+
AsyncResult.isSuccess(preferences)
98+
? (preferences.value.dictationLocale ?? undefined)
99+
: undefined,
100+
);
101+
},
95102
requestPermission: async () => {
96103
const permission = await requestRecordingPermissionsAsync();
97104
return { granted: permission.granted, canAskAgain: permission.canAskAgain };
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/** Locales Apple's on-device SpeechTranscriber offers. Unset follows the device language. */
2+
export const DICTATION_LANGUAGES = [
3+
{ locale: "en-US", label: "English (US)" },
4+
{ locale: "en-GB", label: "English (UK)" },
5+
{ locale: "en-AU", label: "English (Australia)" },
6+
{ locale: "en-CA", label: "English (Canada)" },
7+
{ locale: "en-IN", label: "English (India)" },
8+
{ locale: "pt-BR", label: "Português (Brasil)" },
9+
{ locale: "pt-PT", label: "Português (Portugal)" },
10+
{ locale: "es-ES", label: "Español (España)" },
11+
{ locale: "es-MX", label: "Español (México)" },
12+
{ locale: "es-US", label: "Español (EE. UU.)" },
13+
{ locale: "fr-FR", label: "Français (France)" },
14+
{ locale: "fr-CA", label: "Français (Canada)" },
15+
{ locale: "de-DE", label: "Deutsch" },
16+
{ locale: "it-IT", label: "Italiano" },
17+
{ locale: "ja-JP", label: "日本語" },
18+
{ locale: "ko-KR", label: "한국어" },
19+
{ locale: "zh-CN", label: "中文(普通话)" },
20+
{ locale: "zh-TW", label: "中文(台灣)" },
21+
{ locale: "yue-CN", label: "粵語" },
22+
] as const;
23+
24+
export type DictationLocale = (typeof DICTATION_LANGUAGES)[number]["locale"];
25+
26+
const DICTATION_LOCALES: ReadonlySet<string> = new Set(
27+
DICTATION_LANGUAGES.map((language) => language.locale),
28+
);
29+
30+
export function isDictationLocale(value: unknown): value is DictationLocale {
31+
return typeof value === "string" && DICTATION_LOCALES.has(value);
32+
}

‎apps/mobile/src/native/voiceTranscription.ios.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,16 @@ describe("getLocalVoiceTranscriber", () => {
7979
expect(error).toMatchObject({ code: "cancelled" });
8080
expect(mocks.transcribe).not.toHaveBeenCalled();
8181
});
82+
83+
it("prepares and transcribes the preferred locale instead of the device locale", async () => {
84+
mocks.prepare.mockResolvedValue("pt-BR");
85+
const options = { signal: new AbortController().signal };
86+
const prepared = await getLocalVoiceTranscriber("pt-BR")!.prepare(options);
87+
await prepared.transcribe("file:///voice.m4a", options);
88+
89+
expect(mocks.isAvailable).toHaveBeenCalledWith("pt-BR");
90+
expect(mocks.prepare).toHaveBeenCalledWith("pt-BR");
91+
expect(mocks.transcribe).toHaveBeenCalledWith(audio, "pt-BR");
92+
expect(prepared.locale).toBe("pt-BR");
93+
});
8294
});

‎apps/mobile/src/native/voiceTranscription.ios.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ function getNativeErrorCode(error: unknown): string | undefined {
3333
return typeof error.code === "string" ? error.code : undefined;
3434
}
3535

36-
export function getLocalVoiceTranscriber(): VoiceTranscriber | null {
37-
const locale = getDeviceLocale();
36+
export function getLocalVoiceTranscriber(preferredLocale?: string): VoiceTranscriber | null {
37+
const locale = preferredLocale ?? getDeviceLocale();
3838
if (!AppleTranscription.isAvailable(locale)) return null;
3939
return { prepare: (options) => prepareVoiceTranscription(locale, options) };
4040
}
@@ -63,7 +63,7 @@ async function prepareVoiceTranscription(
6363
if (getNativeErrorCode(error) === "AppleTranscriptionUnsupportedLocale") {
6464
throw new VoiceTranscriptionError(
6565
"unsupported-locale",
66-
"Voice transcription does not support this device language.",
66+
"Voice transcription does not support this language.",
6767
{ cause: error },
6868
);
6969
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { VoiceTranscriber } from "@t3tools/client-runtime/voice-input";
22

3-
export function getLocalVoiceTranscriber(): VoiceTranscriber | null {
3+
export function getLocalVoiceTranscriber(_preferredLocale?: string): VoiceTranscriber | null {
44
return null;
55
}

‎apps/mobile/src/persistence/mobile-preferences.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from "@t3tools/contracts";
1717
import type { AndroidNavigationStyle } from "../lib/androidNavigationStyle";
1818
import type { ComposerEnterBehavior } from "../lib/composerEnterBehavior";
19+
import { isDictationLocale, type DictationLocale } from "../lib/dictationLanguage";
1920
import type { FollowUpBehavior } from "../lib/followUpBehavior";
2021
import { isMicrophoneKind, type MicrophoneEntry } from "../lib/microphonePriority";
2122
import { MOBILE_THEME_IDS, type MobileThemeId, type MobileThemeMode } from "../lib/mobileTheme";
@@ -112,6 +113,8 @@ export interface Preferences {
112113
readonly composerEnterBehavior?: ComposerEnterBehavior;
113114
readonly followUpBehavior?: FollowUpBehavior;
114115
readonly microphones?: ReadonlyArray<MicrophoneEntry>;
116+
/** Unset or null means dictation follows the device language. */
117+
readonly dictationLocale?: DictationLocale | null;
115118
readonly projectGroupingEnabled?: boolean;
116119
readonly projectGroupingMode?: SidebarProjectGroupingMode;
117120
readonly planModeEnabled?: boolean;
@@ -192,6 +195,7 @@ function sanitizePreferences(parsed: Preferences): Preferences {
192195
composerEnterBehavior?: ComposerEnterBehavior;
193196
followUpBehavior?: FollowUpBehavior;
194197
microphones?: ReadonlyArray<MicrophoneEntry>;
198+
dictationLocale?: DictationLocale | null;
195199
projectGroupingEnabled?: boolean;
196200
projectGroupingMode?: SidebarProjectGroupingMode;
197201
planModeEnabled?: boolean;
@@ -308,6 +312,9 @@ function sanitizePreferences(parsed: Preferences): Preferences {
308312
: [];
309313
});
310314
}
315+
if (isDictationLocale(parsed.dictationLocale)) {
316+
preferences.dictationLocale = parsed.dictationLocale;
317+
}
311318
if (typeof parsed.projectGroupingEnabled === "boolean") {
312319
preferences.projectGroupingEnabled = parsed.projectGroupingEnabled;
313320
}

‎docs/user/composer.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ for example, put **Other Bluetooth** above your AirPods so a new headset is pref
193193
while the AirPods microphone stays low. By default, CarPlay comes after the built-in
194194
microphone. Swipe a device left to forget it.
195195

196+
Voice input transcribes in your iPhone's language. To dictate in another language,
197+
such as Brazilian Portuguese, pick it under **Settings → Microphone → Dictation language**.
198+
196199
Transcription runs on your device. T3 Code deletes the temporary audio after
197200
transcription or cancellation; only the message text is sent when you submit.
198201

0 commit comments

Comments
 (0)