$ show xxnuo/MTranServer
MTranServer is a high-performance, offline machine translation server that provides a REST API, web UI, and desktop Electron application. It leverages Mozilla's Bergamot (Marian NMT) engine compiled to WebAssembly for on-device translation, with no GPU required and average response times around 50ms. The server supports major world languages, language auto-detection via CLD2 (also compiled to WASM), and offers compatibility endpoints for popular browser translation plugins such as Immersive Translate, DeepL, Google Translate, and Kiss Translator. The architecture is a TypeScript/Node.js monolith using Express with tsoa for typed controllers and Swagger generation, a React frontend with shadcn/ui, and an Electron desktop wrapper. The system includes model auto-downloading, LRU caching, worker-per-language concurrency, and offline mode. The repository contains a large deprecated codebase (Go, Node.js v1, Rust, C++ versions) which is not actively used but remains in the source tree, potentially causing confusion. The active code is well-structured and follows modern TypeScript practices, though some vendored dependencies (CLD2, Bergamot) are old and have been compiled to WASM to mitigate drift.
The LLM identified 4 files worth highlighting for reuse:
Exports a composed middleware chain for Express: request ID generation, structured request logging (with duration, method, path, status), CORS, auth token validation (Bearer token or query parameter), and a centralized error handler. This pattern is self-contained and could be extracted as a reusable middleware library for any Express-based API server.
Implements a generic LRU cache with configurable max size and TTL. Uses a Map for O(1) access and tracks most recent access order. Provides get, set, delete, clear, and size methods. The implementation is standalone and could be reused in any JavaScript/TypeScript project needing a simple in-memory cache.
Provides a function `normalizeLanguageCode` that maps a wide variety of language code aliases (e.g., 'zh-cn', 'chi', 'chinese', 'zho') to a canonical code. The mapping is comprehensive and based on ISO 639-1/2/3 and common variants. This module is self-contained and could be extracted as a standalone language code normalization utility for any multilingual application.
Manages the lifecycle of translation model files: downloads from a remote repository (Mozilla/Bergamot), verifies SHA256 hashes, decompresses fzstd-compressed archives, and caches results. Includes retry logic and progress reporting. The pattern is a reusable model/resource downloader that can be adapted for any project needing to fetch and verify large binary assets.