diff --git a/apps/mobile/src/lib/voiceInputLanguage.test.ts b/apps/mobile/src/lib/voiceInputLanguage.test.ts new file mode 100644 index 0000000000000..da336f80aa14d --- /dev/null +++ b/apps/mobile/src/lib/voiceInputLanguage.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { normalizeLanguages, resolveVoiceInputLanguage } from "./voiceInputLanguage"; + +describe("normalizeLanguages", () => { + it("keeps string tags in order without duplicates", () => { + expect(normalizeLanguages(["de-CH", "en-CH", 4, "", "de-CH"])).toEqual(["de-CH", "en-CH"]); + }); + + it("returns nothing for a missing value", () => { + expect(normalizeLanguages(undefined)).toEqual([]); + }); +}); + +describe("resolveVoiceInputLanguage", () => { + it("follows the device's first language rather than the app locale", () => { + expect(resolveVoiceInputLanguage(["ko-KR", "en-KR"], "en-KR")).toBe("ko-KR"); + }); + + it("falls back to the app locale without a language list", () => { + expect(resolveVoiceInputLanguage([], "en-US")).toBe("en-US"); + }); +}); diff --git a/apps/mobile/src/lib/voiceInputLanguage.ts b/apps/mobile/src/lib/voiceInputLanguage.ts new file mode 100644 index 0000000000000..da6ab56ad939b --- /dev/null +++ b/apps/mobile/src/lib/voiceInputLanguage.ts @@ -0,0 +1,14 @@ +export function normalizeLanguages(value: unknown): ReadonlyArray { + if (!Array.isArray(value)) return []; + return [ + ...new Set(value.filter((tag): tag is string => typeof tag === "string" && tag.length > 0)), + ]; +} + +/** The device's first language, or the app locale when the device reports none. */ +export function resolveVoiceInputLanguage( + languages: ReadonlyArray, + fallback: string, +): string { + return languages[0] ?? fallback; +} diff --git a/apps/mobile/src/native/deviceLanguages.ts b/apps/mobile/src/native/deviceLanguages.ts new file mode 100644 index 0000000000000..93d1ca4b6cbc3 --- /dev/null +++ b/apps/mobile/src/native/deviceLanguages.ts @@ -0,0 +1,15 @@ +import { Platform, Settings } from "react-native"; + +import { normalizeLanguages } from "../lib/voiceInputLanguage"; + +/** + * The languages from the device's language list, most preferred first. + * + * `Intl` cannot answer this: Hermes takes its default locale from `NSLocale.currentLocale`, which + * follows the app's own localizations. The app ships English only, so a device set to German + * still reports an English locale there. + */ +export function deviceLanguages(): ReadonlyArray { + if (Platform.OS !== "ios") return []; + return normalizeLanguages(Settings.get("AppleLanguages")); +} diff --git a/apps/mobile/src/native/voiceTranscription.ios.test.ts b/apps/mobile/src/native/voiceTranscription.ios.test.ts index d83c371bc2a90..c5a113c232abb 100644 --- a/apps/mobile/src/native/voiceTranscription.ios.test.ts +++ b/apps/mobile/src/native/voiceTranscription.ios.test.ts @@ -8,8 +8,11 @@ const mocks = vi.hoisted(() => ({ prepare: vi.fn<(locale: string) => Promise>(), transcribe: vi.fn<(audio: ArrayBufferLike, locale: string) => Promise>(), readAudio: vi.fn<() => Promise>(), + deviceLanguages: vi.fn<() => ReadonlyArray>(), })); +vi.mock("./deviceLanguages", () => ({ deviceLanguages: mocks.deviceLanguages })); + vi.mock("@react-native-ai/apple/src/NativeAppleTranscription", () => ({ default: { isAvailable: mocks.isAvailable, @@ -49,6 +52,7 @@ beforeEach(() => { mocks.prepare.mockResolvedValue("sv-SE"); mocks.readAudio.mockResolvedValue(audio); mocks.transcribe.mockResolvedValue(nativeTranscript); + mocks.deviceLanguages.mockReturnValue(["sv-SE", "en-GB"]); }); afterEach(() => { @@ -79,4 +83,14 @@ describe("getLocalVoiceTranscriber", () => { expect(error).toMatchObject({ code: "cancelled" }); expect(mocks.transcribe).not.toHaveBeenCalled(); }); + + it("transcribes in the device's first language", async () => { + const options = { signal: new AbortController().signal }; + const prepared = await (await getLocalVoiceTranscriber())!.prepare(options); + await prepared.transcribe("file:///voice.m4a", options); + + expect(mocks.isAvailable).toHaveBeenCalledWith("sv-SE"); + expect(mocks.prepare).toHaveBeenCalledWith("sv-SE"); + expect(mocks.transcribe).toHaveBeenCalledWith(audio, "sv-SE"); + }); }); diff --git a/apps/mobile/src/native/voiceTranscription.ios.ts b/apps/mobile/src/native/voiceTranscription.ios.ts index 5fc6c9a0b8a2b..a2408445ec643 100644 --- a/apps/mobile/src/native/voiceTranscription.ios.ts +++ b/apps/mobile/src/native/voiceTranscription.ios.ts @@ -10,10 +10,16 @@ import { type VoiceTranscriptionOptions, } from "@t3tools/client-runtime/voice-input"; +import { resolveVoiceInputLanguage } from "../lib/voiceInputLanguage"; +import { deviceLanguages } from "./deviceLanguages"; + const isVoiceTranscriptionError = Schema.is(VoiceTranscriptionError); function getDeviceLocale(): string { - return Intl.DateTimeFormat().resolvedOptions().locale; + return resolveVoiceInputLanguage( + deviceLanguages(), + Intl.DateTimeFormat().resolvedOptions().locale, + ); } function wrapError( diff --git a/docs/user/composer.md b/docs/user/composer.md index c236da3c82c60..044590d672087 100644 --- a/docs/user/composer.md +++ b/docs/user/composer.md @@ -215,6 +215,10 @@ microphone on iOS. Swipe a device left to forget it. Android may use another inp when the preferred microphone cannot be routed. Devices with the same name may share a saved preference, but simultaneous same-name inputs remain separately selectable. +Voice input transcribes in the first language of your iPhone's language list +(**Settings → General → Language & Region**). Apple transcribes one language per +recording and does not detect it. + Transcription runs on your device. T3 Code deletes the temporary audio after transcription or cancellation; only the message text is sent when you submit.