Skip to content

Commit 7b354f9

Browse files
committed
feat(mobile): dictation language picker lists what the device supports
1 parent 40b2040 commit 7b354f9

10 files changed

Lines changed: 117 additions & 57 deletions

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { AsyncResult } from "effect/reactivity";
44
import { View } from "react-native";
55
import { useSafeAreaInsets } from "react-native-safe-area-context";
66

7-
import { DEVICE_DICTATION_LANGUAGE_LABEL, DICTATION_LANGUAGES } from "../../lib/dictationLanguages";
7+
import {
8+
DEVICE_DICTATION_LANGUAGE_LABEL,
9+
useDictationLanguages,
10+
} from "../../lib/dictationLanguages";
811
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
912
import { SettingsChoiceRow } from "./components/SettingsChoiceRow";
1013
import { SettingsSection } from "./components/SettingsSection";
1114

12-
const OPTIONS = [{ id: null, label: DEVICE_DICTATION_LANGUAGE_LABEL }, ...DICTATION_LANGUAGES];
13-
1415
export function SettingsDictationLanguageRouteScreen() {
1516
const insets = useSafeAreaInsets();
1617
const preferencesResult = useAtomValue(mobilePreferencesAtom);
@@ -19,6 +20,8 @@ export function SettingsDictationLanguageRouteScreen() {
1920
const selectedId = AsyncResult.isSuccess(preferencesResult)
2021
? (preferencesResult.value.voiceInputLocale ?? null)
2122
: undefined;
23+
const languages = useDictationLanguages();
24+
const options = [{ id: null, label: DEVICE_DICTATION_LANGUAGE_LABEL }, ...(languages ?? [])];
2225

2326
return (
2427
<View collapsable={false} className="flex-1 bg-sheet">
@@ -29,8 +32,14 @@ export function SettingsDictationLanguageRouteScreen() {
2932
contentContainerClassName="gap-3 px-5 pt-4"
3033
contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
3134
>
32-
<SettingsSection footer="The language you speak when dictating.">
33-
{OPTIONS.map((option, index) => (
35+
<SettingsSection
36+
footer={
37+
languages?.length === 0
38+
? "This device has no dictation languages to choose from."
39+
: "The language you speak when dictating."
40+
}
41+
>
42+
{options.map((option, index) => (
3443
<SettingsChoiceRow
3544
key={option.id ?? "device"}
3645
label={option.label}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { useSafeAreaInsets } from "react-native-safe-area-context";
1111
import { type AppSymbolName, SymbolView } from "../../components/ui/AppSymbol";
1212
import { AppText as Text } from "../../components/ui/AppText";
1313
import { cn } from "../../lib/cn";
14-
import { dictationLanguageLabel } from "../../lib/dictationLanguages";
14+
import { dictationLanguageLabel, useDictationLanguages } from "../../lib/dictationLanguages";
1515
import {
1616
isMicrophoneDevice,
1717
type MicrophoneEntry,
@@ -93,6 +93,7 @@ export function SettingsMicrophoneRouteScreen() {
9393
const insets = useSafeAreaInsets();
9494
const preferencesResult = useAtomValue(mobilePreferencesAtom);
9595
const savePreferences = useAtomSet(updateMobilePreferencesAtom);
96+
const dictationLanguages = useDictationLanguages();
9697
const ready = AsyncResult.isSuccess(preferencesResult) && !preferencesResult.waiting;
9798
const entries = microphoneOrder(
9899
AsyncResult.isSuccess(preferencesResult) ? preferencesResult.value.microphones : undefined,
@@ -221,6 +222,7 @@ export function SettingsMicrophoneRouteScreen() {
221222
icon="globe"
222223
label="Dictation language"
223224
value={dictationLanguageLabel(
225+
dictationLanguages,
224226
AsyncResult.isSuccess(preferencesResult)
225227
? preferencesResult.value.voiceInputLocale
226228
: undefined,
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { dictationLanguageLabel, isDictationLanguage } from "./dictationLanguages";
3+
import { dictationLanguageLabel } from "./dictationLanguages";
44

5-
describe("isDictationLanguage", () => {
6-
it("accepts listed ids and rejects anything else", () => {
7-
expect(isDictationLanguage("pt-BR")).toBe(true);
8-
expect(isDictationLanguage("pt")).toBe(false);
9-
expect(isDictationLanguage("xx-XX")).toBe(false);
10-
expect(isDictationLanguage(42)).toBe(false);
11-
});
12-
});
5+
const languages = [{ id: "pt-BR", label: "Português (Brasil)" }];
136

147
describe("dictationLanguageLabel", () => {
15-
it("names the chosen language and falls back to the device language", () => {
16-
expect(dictationLanguageLabel("pt-BR")).toBe("Português (Brasil)");
17-
expect(dictationLanguageLabel(undefined)).toBe("Device language");
8+
it("names the chosen language from the loaded list", () => {
9+
expect(dictationLanguageLabel(languages, "pt-BR")).toBe("Português (Brasil)");
10+
});
11+
12+
it("shows the raw tag until the list loads or when the list lacks it", () => {
13+
expect(dictationLanguageLabel(undefined, "pt-BR")).toBe("pt-BR");
14+
expect(dictationLanguageLabel(languages, "sv-SE")).toBe("sv-SE");
15+
});
16+
17+
it("names the device language when nothing is chosen", () => {
18+
expect(dictationLanguageLabel(languages, null)).toBe("Device language");
19+
expect(dictationLanguageLabel(undefined, undefined)).toBe("Device language");
1820
});
1921
});
Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
1-
// Every id is in SpeechTranscriber.supportedLocales on iOS 26. Labels are endonyms written out
2-
// because Hermes's Intl.DisplayNames support is not guaranteed.
3-
export const DICTATION_LANGUAGES: ReadonlyArray<{ readonly id: string; readonly label: string }> = [
4-
{ id: "de-DE", label: "Deutsch (Deutschland)" },
5-
{ id: "en-AU", label: "English (Australia)" },
6-
{ id: "en-CA", label: "English (Canada)" },
7-
{ id: "en-IN", label: "English (India)" },
8-
{ id: "en-GB", label: "English (UK)" },
9-
{ id: "en-US", label: "English (US)" },
10-
{ id: "es-ES", label: "Español (España)" },
11-
{ id: "es-US", label: "Español (Estados Unidos)" },
12-
{ id: "es-MX", label: "Español (México)" },
13-
{ id: "fr-CA", label: "Français (Canada)" },
14-
{ id: "fr-FR", label: "Français (France)" },
15-
{ id: "it-IT", label: "Italiano (Italia)" },
16-
{ id: "pt-BR", label: "Português (Brasil)" },
17-
{ id: "pt-PT", label: "Português (Portugal)" },
18-
{ id: "hi-IN", label: "हिन्दी (भारत)" },
19-
{ id: "ko-KR", label: "한국어(대한민국)" },
20-
{ id: "zh-CN", label: "中文(中国大陆)" },
21-
{ id: "zh-HK", label: "中文(香港)" },
22-
{ id: "zh-TW", label: "中文(台灣)" },
23-
{ id: "ja-JP", label: "日本語 (日本)" },
24-
{ id: "yue-CN", label: "粤语(中国大陆)" },
25-
];
1+
import { useEffect, useState } from "react";
2+
3+
import { getDictationLanguages } from "../native/voiceTranscription";
4+
import type { DictationLanguage } from "../native/voiceTranscription.types";
265

276
export const DEVICE_DICTATION_LANGUAGE_LABEL = "Device language";
287

29-
export function isDictationLanguage(value: unknown): value is string {
30-
return DICTATION_LANGUAGES.some((language) => language.id === value);
8+
/** The languages this device can transcribe, or undefined while they load. */
9+
export function useDictationLanguages(): ReadonlyArray<DictationLanguage> | undefined {
10+
const [languages, setLanguages] = useState<ReadonlyArray<DictationLanguage>>();
11+
useEffect(() => {
12+
let cancelled = false;
13+
getDictationLanguages().then(
14+
(loaded) => {
15+
if (!cancelled) setLanguages(loaded);
16+
},
17+
() => {
18+
if (!cancelled) setLanguages([]);
19+
},
20+
);
21+
return () => {
22+
cancelled = true;
23+
};
24+
}, []);
25+
return languages;
3126
}
3227

33-
export function dictationLanguageLabel(id: string | null | undefined): string {
34-
return (
35-
DICTATION_LANGUAGES.find((language) => language.id === id)?.label ??
36-
DEVICE_DICTATION_LANGUAGE_LABEL
37-
);
28+
export function dictationLanguageLabel(
29+
languages: ReadonlyArray<DictationLanguage> | undefined,
30+
id: string | null | undefined,
31+
): string {
32+
if (id == null) return DEVICE_DICTATION_LANGUAGE_LABEL;
33+
return languages?.find((language) => language.id === id)?.label ?? id;
3834
}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { VoiceTranscriptionError } from "@t3tools/client-runtime/voice-input";
66
const mocks = vi.hoisted(() => ({
77
isAvailable: vi.fn<(locale: string) => boolean>(),
88
prepare: vi.fn<(locale: string) => Promise<string>>(),
9+
supportedLanguages:
10+
vi.fn<() => Promise<ReadonlyArray<{ readonly id: string; readonly label: string }>>>(),
911
transcribe: vi.fn<(audio: ArrayBufferLike, locale: string) => Promise<TranscriptionResult>>(),
1012
readAudio: vi.fn<() => Promise<ArrayBuffer>>(),
1113
}));
@@ -14,6 +16,7 @@ vi.mock("@react-native-ai/apple/src/NativeAppleTranscription", () => ({
1416
default: {
1517
isAvailable: mocks.isAvailable,
1618
prepare: mocks.prepare,
19+
supportedLanguages: mocks.supportedLanguages,
1720
transcribe: mocks.transcribe,
1821
},
1922
}));
@@ -24,7 +27,7 @@ vi.mock("expo-file-system", () => ({
2427
},
2528
}));
2629

27-
import { getLocalVoiceTranscriber } from "./voiceTranscription.ios";
30+
import { getDictationLanguages, getLocalVoiceTranscriber } from "./voiceTranscription.ios";
2831

2932
const audio = new ArrayBuffer(4);
3033
const nativeTranscript: TranscriptionResult = {
@@ -154,3 +157,25 @@ describe("getLocalVoiceTranscriber", () => {
154157
},
155158
);
156159
});
160+
161+
describe("getDictationLanguages", () => {
162+
it("lists what the device supports, sorted by name", async () => {
163+
mocks.supportedLanguages.mockResolvedValue([
164+
{ id: "pt-BR", label: "Português (Brasil)" },
165+
{ id: "de-DE", label: "Deutsch (Deutschland)" },
166+
{ id: "en-US", label: "English (US)" },
167+
]);
168+
169+
expect(await getDictationLanguages()).toEqual([
170+
{ id: "de-DE", label: "Deutsch (Deutschland)" },
171+
{ id: "en-US", label: "English (US)" },
172+
{ id: "pt-BR", label: "Português (Brasil)" },
173+
]);
174+
});
175+
176+
it("is empty where the device cannot transcribe", async () => {
177+
mocks.supportedLanguages.mockResolvedValue([]);
178+
179+
expect(await getDictationLanguages()).toEqual([]);
180+
});
181+
});

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
type VoiceTranscriptionOptions,
1010
} from "@t3tools/client-runtime/voice-input";
1111

12+
import type { DictationLanguage } from "./voiceTranscription.types";
13+
1214
function getDeviceLocale(): string {
1315
return Intl.DateTimeFormat().resolvedOptions().locale;
1416
}
@@ -39,6 +41,11 @@ export function getLocalVoiceTranscriber(preferredLocale?: string): VoiceTranscr
3941
return { prepare: (options) => prepareVoiceTranscription(locale, options) };
4042
}
4143

44+
export async function getDictationLanguages(): Promise<ReadonlyArray<DictationLanguage>> {
45+
const languages = await AppleTranscription.supportedLanguages();
46+
return [...languages].sort((a, b) => a.label.localeCompare(b.label));
47+
}
48+
4249
async function prepareVoiceTranscription(
4350
locale: string,
4451
{ signal }: VoiceTranscriptionOptions,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import type { VoiceTranscriber } from "@t3tools/client-runtime/voice-input";
22

3+
import type { DictationLanguage } from "./voiceTranscription.types";
4+
35
export function getLocalVoiceTranscriber(_preferredLocale?: string): VoiceTranscriber | null {
46
return null;
57
}
8+
9+
export function getDictationLanguages(): Promise<ReadonlyArray<DictationLanguage>> {
10+
return Promise.resolve([]);
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface DictationLanguage {
2+
readonly id: string;
3+
readonly label: string;
4+
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
} from "@t3tools/contracts";
1717
import type { ComposerEnterBehavior } from "../lib/composerEnterBehavior";
1818
import type { FollowUpBehavior } from "../lib/followUpBehavior";
19-
import { isDictationLanguage } from "../lib/dictationLanguages";
2019
import { isMicrophoneKind, type MicrophoneEntry } from "../lib/microphonePriority";
2120
import { MOBILE_THEME_IDS, type MobileThemeId, type MobileThemeMode } from "../lib/mobileTheme";
2221
import * as MobileDatabase from "./mobile-database";
@@ -73,7 +72,7 @@ export interface Preferences {
7372
readonly composerEnterBehavior?: ComposerEnterBehavior;
7473
readonly followUpBehavior?: FollowUpBehavior;
7574
readonly microphones?: ReadonlyArray<MicrophoneEntry>;
76-
/** An id from `DICTATION_LANGUAGES`, or null for the device language. */
75+
/** A BCP 47 tag the device can transcribe, or null for the device language. */
7776
readonly voiceInputLocale?: string | null;
7877
readonly projectGroupingEnabled?: boolean;
7978
readonly projectGroupingMode?: SidebarProjectGroupingMode;
@@ -127,6 +126,13 @@ export class MobilePreferencesStore extends Context.Service<
127126
}
128127
>()("@t3tools/mobile/persistence/MobilePreferencesStore") {}
129128

129+
// Shape only; whether the device can transcribe the tag is checked when preparing voice input.
130+
const LANGUAGE_TAG = /^[A-Za-z]{2,3}(-[A-Za-z0-9]{2,8})*$/;
131+
132+
function isLanguageTag(value: unknown): value is string {
133+
return typeof value === "string" && LANGUAGE_TAG.test(value);
134+
}
135+
130136
function sanitizePreferences(parsed: Preferences): Preferences {
131137
const preferences: {
132138
pullRequestHideTestFiles?: boolean;
@@ -244,7 +250,7 @@ function sanitizePreferences(parsed: Preferences): Preferences {
244250
: [];
245251
});
246252
}
247-
if (isDictationLanguage(parsed.voiceInputLocale) || parsed.voiceInputLocale === null) {
253+
if (parsed.voiceInputLocale === null || isLanguageTag(parsed.voiceInputLocale)) {
248254
preferences.voiceInputLocale = parsed.voiceInputLocale;
249255
}
250256
if (typeof parsed.projectGroupingEnabled === "boolean") {

‎apps/mobile/src/state/preferences.test.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,16 @@ function makePreferencesStore(initialPayload: MobilePreferences.Preferences) {
9898
}
9999

100100
describe("mobile preferences store", () => {
101-
it.effect("keeps a null dictation language and drops an unknown one", () =>
101+
it.effect("keeps a null or well-formed dictation language and drops a malformed one", () =>
102102
Effect.gen(function* () {
103103
const cleared = yield* makePreferencesStore({ voiceInputLocale: null });
104104
expect(yield* cleared.store.load).toEqual({ voiceInputLocale: null });
105105

106-
const unknown = yield* makePreferencesStore({ voiceInputLocale: "xx-XX" });
107-
expect(yield* unknown.store.load).toEqual({});
106+
const chosen = yield* makePreferencesStore({ voiceInputLocale: "yue-Hant-HK" });
107+
expect(yield* chosen.store.load).toEqual({ voiceInputLocale: "yue-Hant-HK" });
108+
109+
const malformed = yield* makePreferencesStore({ voiceInputLocale: "Portuguese" });
110+
expect(yield* malformed.store.load).toEqual({});
108111
}),
109112
);
110113
});

0 commit comments

Comments
 (0)