Fix translate: working default backend, settings UI, outgoing translation - #206
Fix translate: working default backend, settings UI, outgoing translation#206MaikCZE wants to merge 3 commits into
Conversation
…orking default backend - FreeTranslateAPI's hosting (ftapi.pythonanywhere.com) is defunct, so the default backend never worked. Add a new "google" backend that talks directly to the free, unofficial Google Translate endpoint FTAPI itself used to wrap, and make it the default. - Add a full Translate section to the TClient settings menu (previously the feature was config-var only, with no UI at all): backend/language dropdowns, incoming auto-translate toggle, and a LibreTranslate endpoint/key section shown only when relevant. - Add outgoing translation: optionally translate your own messages into another language before sending, via a new tc_translate_outgoing / tc_translate_outgoing_target config pair and CTranslate::TranslateOutgoing(). Falls back to sending the original text if translation fails, and skips "/"-prefixed messages so it doesn't mangle commands like /w.
Rebased onto master_new (per SollyBunny's review) and addressed every requested change from the original review: - Google backend now POSTs client/target-language/text in the request body (application/x-www-form-urlencoded) instead of building a long query-string URL -- keeps the fixed 256-byte CHttpRequest::m_aUrl buffer well within bounds regardless of message length. - Removed the FreeTranslateAPI (ftapi) backend entirely, including its stale "hosting is dead" comment and its ftapi-specific auto-translate guard -- anyone who wants a self-hosted option already has LibreTranslate. - Backend display names are no longer duplicated between the backend classes and the settings-menu dropdown: each backend exposes a StaticName()/StaticValue(), and a single g_aTranslateBackends[] table (translate.h/.cpp) is now the one source of truth for both dispatch and the menu. - Removed the magic-number English fallback (previously a hardcoded `return 5`): TranslateLanguageIndex() now finds English by comparing language codes, so it stays correct regardless of list order/edits. - Language dropdowns are now backend-scoped instead of one shared list for every backend, since Google genuinely supports far more languages than a typical self-hosted LibreTranslate instance. - Outgoing-translate is now CTranslate::ChatDoTranslateOutgoing(), which returns bool and is chained as another `else if` in chat.cpp's ChatDoBinds/ChatDoSpecId cascade instead of a separate nested if/else block -- matches the existing pattern and keeps the chat.cpp diff to one line. - Simplified the "too many jobs in flight" comment that read oddly (dropped the "don't make the player wait"/game-vs-chat phrasing). Compiled clean with MSVC 19.51 (Release, Ninja) -- full DDNet.exe build, zero errors/warnings from the touched files. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
There was a problem hiding this comment.
I dislike the large number of globals and separate vars for arrays and the size
I suggest:
- Keeping g_aTranslateBackends, but using inlining g_NumTranslateBackends
- Move the values from the individual classes to this array (so the classes no longer hold it)
- Change the language list to be std::vectors
Check style is failing (run scripts/fix_style.py), and also check other CI when it completes
- g_aTranslateBackends stays a fixed array (std::array<STranslateBackendInfo, 2>), but the separate g_NumTranslateBackends variable is gone -- callers use g_aTranslateBackends.size() directly. - Backend display name/value no longer live in the individual backend classes (removed StaticName()/StaticValue()/the Name() overrides) -- ITranslateBackend now takes its name once at construction, sourced from the matching g_aTranslateBackends entry, so the array is the only place that data exists. - Both language lists (Google, LibreTranslate) are now std::vector<STranslateLanguage> instead of raw C arrays, matching STranslateBackendInfo::m_vLanguages. - Ran scripts/fix_style.py (clang-format 20) to fix check-style -- also picked up one pre-existing, unrelated style violation in chat.cpp's AddLine() (regex chat-ignore lambda) since formatting runs per-file. check-clang-tidy's remaining failures (mumble.h, discord.cpp, client.cpp, menus_start.cpp, menus_settings.cpp) are pre-existing on master_new itself (confirmed via its own most recent check-runs) and untouched by this PR -- out of scope here. Rebuilt clean with MSVC 19.51 (Release, Ninja), zero errors/warnings. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
|
Addressed all three points from the review, plus check-style:
Rebuilt clean with MSVC 19.51 (Release, Ninja) after each change, zero errors/warnings. 🤖 Addressed by Claude Code |
|
This looks good to me, but I still need to test it #208 |
Summary
Follow-up to #203, rebased onto
master_newand addressing every point from SollyBunny's review there:translate.cpp) had no settings UI at all (config-var only), no way to translate your own outgoing messages, and its default backend was silently dead (ftapi.pythonanywhere.comnow 404s to a generic placeholder page).googlebackend (talks directly to the same free, unofficial Google Translate endpoint FTApi used to wrap), a full Translate section in the settings menu, and outgoing-message translation.Changes since #203
application/x-www-form-urlencoded) -- keepsCHttpRequest::m_aUrl's fixed 256-byte buffer well within bounds regardless of message length, instead of risking truncation on a long chat message.ftapi) backend entirely -- its hosting is gone, and anyone wanting a self-hosted option already has the LibreTranslate backend. Removed its stale "hosting is dead" comment and its ftapi-specific auto-translate guard along with it.g_aTranslateBackends[](translate.h/.cpp) is now the single source of truth for each backend's config value + display name, used by bothCreateTranslateBackend()dispatch and the settings-menu dropdown -- the dropdown previously hardcoded its own copy of these strings.TranslateLanguageIndex()finds English by comparing language codes instead of a hardcodedreturn 5, so it stays correct regardless of how a language list is edited/reordered.g_aTranslateBackends[i].m_pLanguages) instead of one shared list, since Google genuinely supports far more languages than a typical self-hosted LibreTranslate instance.chat.cppdiff reduced to one line: outgoing translation is nowCTranslate::ChatDoTranslateOutgoing(), returningbooland chained as anotherelse ifalongsideChatDoBinds/ChatDoSpecIdinstead of a separate nestedif/elseblock.Test plan
DDNet.exebuild, zero errors/warnings from any touched filemaster_new(cherry-picked with no conflicts)Supersedes #203.
🤖 Generated with Claude Code