languages¶
languages
¶
Language-related constants for the AI Translate application.
iter_languages_sorted_for_ui
¶
Returns LANGUAGES sorted by the currently-localized label.
Picker populate sites should iterate this rather than LANGUAGES
directly so a Vietnamese user sees Vietnamese alphabetical order,
a Japanese user sees gojūon order, etc. — matching how macOS /
Windows present their localized language pickers.
Sort key runs through normalize_for_search so accented forms
sort with their base letter rather than after Z — Vietnamese
"Tiếng Đan Mạch" lands between D and E, French "Élève" with E,
German "Über" with U, etc. Plain casefold would push every
non-ASCII initial to the end of the list (the bug reported by the
Vietnamese picker that prompted this change). Ties fall back to
the English label so the order is deterministic even when two
locales translate to identical text.
ソースコード位置: src/constants/languages.py
_language_i18n_key
¶
English label → language.<key> i18n suffix.
Lower-cases and replaces non-alphanumeric runs with a single underscore, then strips trailing underscores. Examples:
"Japanese"→"japanese""Chinese (Simplified)"→"chinese_simplified""Portuguese (Brazil)"→"portuguese_brazil""English (UK)"→"english_uk"
Kept stable so the keys don't churn when display text is refined per locale; tests rely on this mapping.
ソースコード位置: src/constants/languages.py
format_language_picker_label
¶
Returns the display string for a language in UI pickers.
Looks up the per-locale translation under language.<key>
(where <key> is :func:_language_i18n_key) and returns it
when present. Falls back to "<native> (<English>)" when the
translation is genuinely missing — insurance against future
drift if a new language is added without updating every locale.
The double-fallback to plain English when native == english
stays in place to avoid the silly "English (UK) (English (UK))"
repeat.
The DB and LLM prompts continue to use the bare English label; this helper only affects what the user sees in pickers.
ソースコード位置: src/constants/languages.py
localized_language_label
¶
Returns the user-facing display string for an English language label.
Convenience wrapper over :func:format_language_picker_label for
callers that only have the canonical English label (e.g. history
tables that read it from the DB) — looks the native name up from
:data:LANGUAGES and delegates.
Empty input passes through unchanged so callers don't have to special-case the auto-detect placeholder. Unknown labels (legacy DB entries with typos, removed languages, etc.) fall back to the raw English label rather than raise — the history table would rather render "Klingon" than blank cell.
ソースコード位置: src/constants/languages.py
get_locale_code
¶
Returns the BCP-47 locale code for a language label.
Falls back to the lowercased label if not found.
| 引数 | デスクリプション |
|---|---|
label
|
English language name (e.g. "Vietnamese").
タイプ:
|
| 戻り値 | デスクリプション |
|---|---|
str
|
Locale code string (e.g. "vi"). |
ソースコード位置: src/constants/languages.py
is_rtl_language
¶
Returns True when label names a right-to-left language.
Empty / unknown labels return False — the natural default for Latin-script languages and the safe default for the auto-detect case where the source language isn't known yet.