|
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"; |
26 | 5 |
|
27 | 6 | export const DEVICE_DICTATION_LANGUAGE_LABEL = "Device language"; |
28 | 7 |
|
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; |
31 | 26 | } |
32 | 27 |
|
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; |
38 | 34 | } |
0 commit comments