Skip to content

armgit / Handy

Public Imported from GitHub
0

A free, open source, and extensible speech-to-text application that works completely offline.

update language selector

CJ Paisdc01346d1cf2
diff --git a/src-tauri/src/managers/model.rs b/src-tauri/src/managers/model.rs
index ae735834..116f4b2c 100644
--- a/src-tauri/src/managers/model.rs
+++ b/src-tauri/src/managers/model.rs
@@ -89,6 +89,18 @@ fn recognition_language(language: &str) -> &str {
}
}
+/// The base code Handy matches a language *intent* on: a tag's primary subtag,
+/// with any BCP-47 region or script suffix dropped (`en-US` → `en`, `zh-CN` →
+/// `zh`, `zh-Hant` → `zh`). Bare and three-letter codes (`haw`) pass through
+/// unchanged. Lets a bare intent (`en`) match a model that advertises full
+/// locales (`en-US`) without discarding the real code the engine needs.
+fn base_language(language: &str) -> &str {
+ match language.split_once('-') {
+ Some((base, _)) => base,
+ None => language,
+ }
+}
+
fn canonicalize_supported_languages(languages: Vec<String>) -> Vec<String> {
let mut seen = HashSet::new();
let mut canonical = Vec::with_capacity(languages.len());
@@ -208,6 +220,14 @@ impl ModelDescriptor {
/// The canonical coercion used on every transcription path: computed at the
/// point of use and **never written back** to settings, so the user's last
/// explicit intent survives switching to an incompatible model and back.
+///
+/// Matching is base-aware ([`base_language`]) and returns the model's own
+/// *concrete* code, so a bare intent (`en`) resolves to the exact string the
+/// engine's prompt table expects (`en-US`) for models that advertise full
+/// BCP-47 locales. Chinese *script* intents (`zh-Hans`/`zh-Hant`) are the sole
+/// exception: they pass through unchanged so the downstream Simplified /
+/// Traditional output conversion still fires (the engine path collapses them to
+/// a plain Chinese code separately).
pub fn effective_language(
intent: &str,
supported_languages: &[String],
@@ -217,12 +237,16 @@ pub fn effective_language(
return intent.to_string();
}
- if intent != "auto"
- && supported_languages
+ if intent != "auto" {
+ if let Some(code) = supported_languages
.iter()
- .any(|language| recognition_language(language) == recognition_language(intent))
- {
- return intent.to_string();
+ .find(|language| base_language(language) == base_language(intent))
+ {
+ if intent == "zh-Hans" || intent == "zh-Hant" {
+ return intent.to_string();
+ }
+ return code.clone();
+ }
}
if supports_language_detection {
@@ -231,7 +255,10 @@ pub fn effective_language(
// Model can't auto-detect and the intent isn't usable: fall back to a
// concrete language (prefer English) so we never hand the engine "auto".
- if let Some(en) = supported_languages.iter().find(|l| l.as_str() == "en") {
+ if let Some(en) = supported_languages
+ .iter()
+ .find(|language| base_language(language) == "en")
+ {
return en.clone();
}
recognition_language(&supported_languages[0]).to_string()
@@ -2347,6 +2374,38 @@ mod tests {
assert_eq!(effective_language("auto", &languages, false), "zh");
}
+ #[test]
+ fn test_effective_language_resolves_bare_intent_to_concrete_locale() {
+ // A model advertising full BCP-47 locales (e.g. Nemotron Streaming):
+ // a bare intent must resolve to the exact code the engine expects, not
+ // be handed back as the bare form the prompt table may not contain.
+ let languages = vec![
+ "en-US".to_string(),
+ "en-GB".to_string(),
+ "es-ES".to_string(),
+ "zh-CN".to_string(),
+ "ja-JP".to_string(),
+ ];
+
+ assert_eq!(effective_language("en", &languages, true), "en-US");
+ assert_eq!(effective_language("es", &languages, true), "es-ES");
+ // `zh`/`ja` have no bare entry in this model's table; resolve to locale.
+ assert_eq!(effective_language("zh", &languages, true), "zh-CN");
+ assert_eq!(effective_language("ja", &languages, true), "ja-JP");
+ // An unsupported intent still auto-detects when the model can.
+ assert_eq!(effective_language("fr", &languages, true), "auto");
+ }
+
+ #[test]
+ fn test_effective_language_preserves_chinese_script_intent_for_locale_model() {
+ // Script intents survive so Simplified/Traditional output conversion
+ // still fires, even when the model advertises a regioned Chinese code.
+ let languages = vec!["en-US".to_string(), "zh-CN".to_string()];
+
+ assert_eq!(effective_language("zh-Hans", &languages, true), "zh-Hans");
+ assert_eq!(effective_language("zh-Hant", &languages, true), "zh-Hant");
+ }
+
#[test]
fn test_canonicalize_supported_languages_collapses_chinese_scripts() {
let languages = canonicalize_supported_languages(
diff --git a/src/components/settings/LanguageSelector.tsx b/src/components/settings/LanguageSelector.tsx
index d224d13a..b814c855 100644
--- a/src/components/settings/LanguageSelector.tsx
+++ b/src/components/settings/LanguageSelector.tsx
@@ -5,8 +5,8 @@ import { ResetButton } from "../ui/ResetButton";
import { useSettings } from "../../hooks/useSettings";
import {
getLanguageLabel,
- LANGUAGES,
recognitionLanguage,
+ SELECTABLE_LANGUAGES,
supportsLanguageCode,
} from "../../lib/constants/languages";
@@ -19,9 +19,12 @@ interface LanguageSelectorProps {
supportsLanguageDetection?: boolean;
}
-// Mirrors `effective_language` in src-tauri/src/managers/model.rs. The Rust
-// function is authoritative for what the engine receives; this resolves the
-// same value for *display* so the picker shows what will actually be used.
+// Mirrors the matching logic of `effective_language` in
+// src-tauri/src/managers/model.rs. The Rust function is authoritative for the
+// *concrete* code the engine receives (e.g. "en-US"); this resolves the
+// canonical *base* code ("en") so the highlighted picker item matches an entry
+// in the LANGUAGES list. Matching is base-aware (`supportsLanguageCode` strips
+// region/script subtags), so a model advertising full locales still resolves.
const effectiveLanguage = (
intent: string,
supported: string[],
@@ -31,7 +34,7 @@ const effectiveLanguage = (
if (intent !== "auto" && supportsLanguageCode(supported, intent))
return intent;
if (supportsDetection) return "auto";
- if (supported.includes("en")) return "en";
+ if (supportsLanguageCode(supported, "en")) return "en";
return recognitionLanguage(supported[0]);
};
@@ -82,8 +85,8 @@ export const LanguageSelector: React.FC<LanguageSelectorProps> = ({
const availableLanguages = useMemo(() => {
if (!supportedLanguages || supportedLanguages.length === 0)
- return LANGUAGES;
- return LANGUAGES.filter((lang) =>
+ return SELECTABLE_LANGUAGES;
+ return SELECTABLE_LANGUAGES.filter((lang) =>
lang.value === "auto"
? supportsLanguageDetection
: supportsLanguageCode(supportedLanguages, lang.value),
diff --git a/src/lib/constants/languages.ts b/src/lib/constants/languages.ts
index 790d1ac3..e250664a 100644
--- a/src/lib/constants/languages.ts
+++ b/src/lib/constants/languages.ts
@@ -9,8 +9,8 @@ export const LANGUAGES: Language[] = [
{ value: "auto", label: "Auto Detect" },
{ value: "en", label: "English" },
{ value: CHINESE_LANGUAGE_CODE, label: "Chinese" },
- { value: "zh-Hans", label: "Chinese (Simplified output)" },
- { value: "zh-Hant", label: "Chinese (Traditional output)" },
+ { value: "zh-Hans", label: "Chinese (Simplified)" },
+ { value: "zh-Hant", label: "Chinese (Traditional)" },
{ value: "yue", label: "Cantonese" },
{ value: "de", label: "German" },
{ value: "es", label: "Spanish" },
@@ -122,10 +122,29 @@ export const MODEL_CAPABILITY_LANGUAGES: Language[] = LANGUAGES.filter(
language.value !== "auto" && !CHINESE_OUTPUT_INTENTS.has(language.value),
);
-export const recognitionLanguage = (languageCode: string): string =>
- CHINESE_OUTPUT_INTENTS.has(languageCode)
- ? CHINESE_LANGUAGE_CODE
- : languageCode;
+// Languages offered in the transcription-language picker. We surface the two
+// explicit Chinese *output* variants (Simplified / Traditional) and hide the
+// bare recognition code `zh` ("Chinese"): all three recognize identically, so
+// the plain option only adds ambiguity about which script you get. `zh` stays in
+// LANGUAGES — it's still a valid *effective* language (auto-detect and must-pick
+// fallback can resolve to it) and its label is needed to render that state — it
+// just isn't directly selectable.
+export const SELECTABLE_LANGUAGES: Language[] = LANGUAGES.filter(
+ (language) => language.value !== CHINESE_LANGUAGE_CODE,
+);
+
+// Collapse a language tag to the base code Handy matches on, dropping any
+// BCP-47 region or script subtag: "en-US" → "en", "zh-CN" → "zh", "zh-Hant" →
+// "zh". Bare and three-letter codes ("haw") pass through unchanged. This lets
+// the picker match a model's *real* codes — which may be full locales like
+// "en-US" (e.g. Nemotron Streaming) — against Handy's canonical bare-code
+// LANGUAGES list without the backend having to mangle the codes the engine needs.
+export const recognitionLanguage = (languageCode: string): string => {
+ const separatorIndex = languageCode.indexOf("-");
+ return separatorIndex === -1
+ ? languageCode
+ : languageCode.slice(0, separatorIndex);
+};
export const supportsLanguageCode = (
supportedLanguages: string[],