Skip to main content

Translate text by google, bing, youdaozhiyun, haici, stardict, openai, large language model of local machine, etc at same time from CLI, GUI (GNU/Linux, Android, macOS and Windows), REPL, python, shell and vim.

Project description

translate-shell

pre-commit.ci status github/workflow codecov readthedocs DeepSource

github/downloads github/downloads/latest github/issues github/issues-closed github/issues-pr github/issues-pr-closed github/discussions github/milestones github/forks github/stars github/watchers github/contributors github/commit-activity github/last-commit github/release-date

github/license github/languages github/languages/top github/directory-file-count github/code-size github/repo-size github/v

pypi/status pypi/v pypi/downloads pypi/format pypi/implementation pypi/pyversions

Translate text by:

  • online translators
    • google
    • bing
    • youdaozhiyun
    • haici
  • offline dictionaries
    • stardict
  • LLM
    • OpenAI
    • llama: use your local model

Supports:

  • CLI
  • GUI
    • GNU/Linux
    • Android
    • macOS
    • Windows
  • REPL
  • script:
    • python
    • shell
    • vim: DEPRECATION: vim port will be replaced by language server
  • language server
  • CI/CD
    • github action

Usage

UI

CLI

trans --translators=google,bing,haici,stardict crush

CLI

REPL

$ trans  # enter REPL
> en:ja  # change source language to english and target language to japanese
> :  # swap source and target languages
> =stardict  # use stardict to translate text
> !cat example/test.txt  # execute a shell command
ハッカー
> <example/test.txt   # translate a file
hacker
> 画家  # translate text
painter; artist
> !  # enter shell
$ echo $SHELL  # execute a shell command
/usr/bin/zsh
$ exit  # exit shell
>

REPL

TUI

Vim
Translate --translators=google,bing Free as in Freedom

Vim

GUI

GNU/Linux

GNU/Linux

Android

android-toast

Script

Python

>>> from translate_shell.translate import translate
>>> translate("The Mythical Man-Month", "zh_TW")
... Translations(
...     status=1,
...     results=[
...         Translation(
...             translator="google",
...             sl="auto",
...             tl="zh_TW",
...             text="The Mythical Man-Month",
...             phonetic="",
...             paraphrase="神話般的人月",
...             explains={},
...             details={},
...             alternatives=["神話般的月"]
...         )
...     ],
...     text="The Mythical Man-Month",
...     to_lang="zh_TW",
...     from_lang="auto",
... )

Shell Script

$ xsel -o | trans --format json | jq -r '"《\(.results[].paraphrase)》的英文是 \(.text)."'
《大教堂和集市》的英文是 the cathedral and the bazaar.

Vim Script

:let g:text = 'Just for Fun'
:let g:translation = json_decode(translate_shell#call('--format=json', g:text))
:echo g:text 'is' g:translation.results[0].paraphrase 'in Chinese.'
Just for Fun is 纯娱乐 in Chinese.

Language server

  • document hover: display translated results
  • completions: complete translated words

CI/CD

Github Action

This repo provides an action to translate *.po of a repository. See inputs. For example, you have a repository which contains translations of another project's documents (upstream), you can write a github workflow to detect if upstream has update. If a new version exist, update the version and generate new .pos, then translate the changed .pos and git commit.

Examples:

on:
  schedule:
    # Run this CI/CD at 0:00 on Friday
    - cron: 0 0 * * 5
  workflow_dispatch:

jobs:
  translate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Generate new .po
        id: version
        run: |
          # update version
          # then use perl / sed / ... to replace the version string of your file
          # then generate new .po
          echo VERSION=XXX > $GITHUB_OUTPUT
      - name: Translate your *.po
        uses: Freed-Wu/translate-shell@main
      - name: Git commit
        run: |
          git add **.po
          git config --global user.name 'Github Actions'
          git config --global user.email '41898282+github-actions[bot]@users.noreply.github.com'
          git commit -m ":bookmark: Dump version to $VERSION"
          git tag "$VERSION"
          git remote set-url origin "https://x-access-token:$GH_TOKEN@github.com/$GITHUB_REPOSITORY"
          git push
          git push --tags
        env:
          VERSION: ${{steps.version.outputs.VERSION}}
          GH_TOKEN: ${{secrets.GH_TOKEN}}

You can use the following commands to get the new version:

# get a github repo's version:
curl https://api.github.com/repos/user/repo/releases/latest | jq -r .tag_name
# get a gitlab repo's version
curl 'https://gitlab.com/api/v4/projects/41218592/repository/tags?per_page=1' |
jq -r '.[].name'

You can use the following tools to generate the new .pos:

  • sphinx-intl: Generate .po for any project using sphinx to generate document.
  • po4a: Generate .po for any project which use markdown, LaTeX, man, ... to write document.

Similar Projects

See comparison.

Features

  • Translate with different translators at same time, like translator
  • Translate clipboard contents automatically, like ydcv
  • Speak the pronunciation of words
  • Support online translate engines
  • Support offline dictionaries
  • Many methods to use, from shell, python and vim
  • Magic text, like en: to change source language, :zh_CN to change target language, <file to translate file, etc.
  • Allow customization by config.py
  • Good shell completions, especially for zsh, complete options and translation history
  • Manpage: man trans
  • Beautiful UI
  • Cross platforms
  • Rich API, can be easily called from shell and python
  • Good document
  • Unit test, keep code quality
  • CI/CD
  • clean code
  • Respect PEP484
  • Respect PEP621
  • Respect XDG

The last but not least: it is a libre software.

See document to know more.

PS: PR is welcome! Please make code clean and keep test pass!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

translate-shell-0.0.51.tar.gz (116.5 kB view details)

Uploaded Source

Built Distribution

translate_shell-0.0.51-py3-none-any.whl (85.5 kB view details)

Uploaded Python 3

File details

Details for the file translate-shell-0.0.51.tar.gz.

File metadata

  • Download URL: translate-shell-0.0.51.tar.gz
  • Upload date:
  • Size: 116.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for translate-shell-0.0.51.tar.gz
Algorithm Hash digest
SHA256 0aba9c7f2507d44bda81fa1150430e768b0ae779e7428e7a47afe1c97f9e7bba
MD5 439bd4f85bdea2957feba09cee4567a9
BLAKE2b-256 8adb3d2ee4595864846145241662372c0aa82c835946e122df83a72a60a517db

See more details on using hashes here.

File details

Details for the file translate_shell-0.0.51-py3-none-any.whl.

File metadata

File hashes

Hashes for translate_shell-0.0.51-py3-none-any.whl
Algorithm Hash digest
SHA256 3af99e796fc1698e909313fcae258d8080e2983d7dae1b37a8c8333c8383e5ba
MD5 48a62ec84588c32733abd9a630474a6e
BLAKE2b-256 b08ed8de7530f10193e0bb00bf20a4df48e37941ad0827e96846b3a26b0dc4d1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page