Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions apps/mobile/src/lib/voiceInputLanguage.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
14 changes: 14 additions & 0 deletions apps/mobile/src/lib/voiceInputLanguage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function normalizeLanguages(value: unknown): ReadonlyArray<string> {
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<string>,
fallback: string,
): string {
return languages[0] ?? fallback;
}
15 changes: 15 additions & 0 deletions apps/mobile/src/native/deviceLanguages.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
if (Platform.OS !== "ios") return [];
return normalizeLanguages(Settings.get("AppleLanguages"));
}
14 changes: 14 additions & 0 deletions apps/mobile/src/native/voiceTranscription.ios.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ const mocks = vi.hoisted(() => ({
prepare: vi.fn<(locale: string) => Promise<string>>(),
transcribe: vi.fn<(audio: ArrayBufferLike, locale: string) => Promise<TranscriptionResult>>(),
readAudio: vi.fn<() => Promise<ArrayBuffer>>(),
deviceLanguages: vi.fn<() => ReadonlyArray<string>>(),
}));

vi.mock("./deviceLanguages", () => ({ deviceLanguages: mocks.deviceLanguages }));

vi.mock("@react-native-ai/apple/src/NativeAppleTranscription", () => ({
default: {
isAvailable: mocks.isAvailable,
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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");
});
});
8 changes: 7 additions & 1 deletion apps/mobile/src/native/voiceTranscription.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions docs/user/composer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Loading