返回 2026-05-02
🛠 工具 / 开源

使用 TranslateGemma + Ollama 实现离线命令行翻译Offline command line translation with TranslateGemma + Ollama

evanhahn.com·2026-05-01 节选正文

作者构建了一个完全离线的命令行翻译脚本,利用 Google 的 TranslateGemma 模型和本地运行的 Ollama 服务,实现在终端中直接翻译文本。例如,输入 `echo '¿Cómo estás?' | translate` 即可输出中文结果,无需联网。该项目展示了如何将轻量级开源大模型部署于边缘设备,满足隐私敏感场景下的实时语言处理需求。

by Evan Hahn, posted May 1, 2026, tagged #tool #release #ai #ollama

I wrote a simple script that translates text at the command line, completely offline. Here’s an example of how it works on my computer:

echo '¿Cómo estás?' | translate
# => How are you?

It combines a few tools:

  • TranslateGemma, a special-purpose language model for translation
  • Ollama, a tool for running language models locally
  • Efficient Language Detector, a library that detects the language for a piece of text
  • Here’s the pseudocode of how it works:

    source = read_stdin()
    
    # Uses Efficient Language Detector
    source_language = detect_language(source)
    
    # Uses JavaScript's `navigator.language`
    target_language = get_system_language()
    
    # Uses Ollama + TranslateGemma
    return translate(source, source_language, target_language)

    I built this because I couldn’t find anyone else who had done it. It’s written in Deno for my specific needs—for example, it only translates text into your system’s language—but could easily be adapted if you need something else.

    I like that I can do offline, private, automatic translation. It’s imperfect, but useful for me!

    Here’s the source code.

    需要完整排版与评论请前往来源站点阅读。