diff options
| author | historia <historiavg@proton.me> | 2026-08-26 01:43:41 -0400 |
|---|---|---|
| committer | historia <historiavg@proton.me> | 2026-08-26 01:43:41 -0400 |
| commit | acbd9ff2c91182d96c57ffb57bee6e9b3fcbcbd4 (patch) | |
| tree | e336c11f2a57cff5566e249aa5d5477a3dc63c55 /app/converter/clients/languages.py | |
| parent | 104a0d65c1ba37847c15b64212b7fec8ba371ccb (diff) | |
| download | tts-audiobook-generator-acbd9ff2c91182d96c57ffb57bee6e9b3fcbcbd4.tar.gz | |
refactor: split tts.py into per-backend packages
Diffstat (limited to 'app/converter/clients/languages.py')
| -rw-r--r-- | app/converter/clients/languages.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/app/converter/clients/languages.py b/app/converter/clients/languages.py new file mode 100644 index 0000000..079dead --- /dev/null +++ b/app/converter/clients/languages.py @@ -0,0 +1,74 @@ +"""Language tables shared by the TTS clients and their UIs.""" + +from typing import Optional + +# Languages the Qwen3-TTS demo accepts as display names (its API silently +# falls back to "Auto" for anything else, so unknown names are rejected +# before a run starts instead of mispronouncing a whole book). +TTS_LANGUAGES = ( + "Auto", "Chinese", "English", "German", "Italian", "Portuguese", + "Spanish", "Japanese", "Korean", "French", "Russian", +) + +# Short aliases accepted on the command line (ISO 639-1 codes and common +# shorthands), mapped to the display names above. +TTS_LANGUAGE_ALIASES = { + "zh": "Chinese", + "en": "English", + "de": "German", + "it": "Italian", + "pt": "Portuguese", + "es": "Spanish", + "ja": "Japanese", + "ko": "Korean", + "fr": "French", + "ru": "Russian", + "zh-cn": "Chinese", + "zh-tw": "Chinese", + "pt-br": "Portuguese", + "en-us": "English", + "en-gb": "English", +} + +# Qwen display names -> ISO 639-1 codes, for audio.cpp families whose +# language request option takes a code instead of a display name. "Auto" +# has no code and maps to None so the field is omitted and the server +# applies its own default. +LANGUAGE_ISO_CODES = { + "Chinese": "zh", + "English": "en", + "German": "de", + "Italian": "it", + "Portuguese": "pt", + "Spanish": "es", + "Japanese": "ja", + "Korean": "ko", + "French": "fr", + "Russian": "ru", +} + + +def normalize_language(value: Optional[str]) -> str: + """Normalize a user-provided language name to a Qwen3-TTS display name. + + Accepts the display names in TTS_LANGUAGES case-insensitively as + well as the short aliases in TTS_LANGUAGE_ALIASES (ISO 639-1 codes + and common shorthands). Raises ValueError for anything else, since the + Qwen3-TTS demo silently falls back to "Auto" for unrecognized languages. + """ + if value is None: + raise ValueError("Language must not be None") + candidate = value.strip() + if not candidate: + raise ValueError("Language must not be empty") + for name in TTS_LANGUAGES: + if candidate.lower() == name.lower(): + return name + alias = TTS_LANGUAGE_ALIASES.get(candidate.lower()) + if alias: + return alias + raise ValueError( + f"Unknown language: {value!r}. Expected one of " + f"{', '.join(TTS_LANGUAGES)} (or an alias: " + f"{', '.join(sorted(TTS_LANGUAGE_ALIASES))})." + ) |
