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:

  • 命令行

  • 图形用户界面

    • GNU/Linux

    • Android

    • macOS

    • Windows

  • REPL

  • script:

    • python

    • shell

    • vim: DEPRECATION: vim port will be replaced by language server

  • language server

  • 持续集成/持续部署

    • github action

用法

用户界面

命令行

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

命令行

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

文本用户界面

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

Vim

图形用户界面

GNU/Linux

GNU/Linux

Android

android-toast

脚本

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 脚本

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

Vim 脚本

: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

  • [x] document hover: display translated results

  • [x] completions: complete translated words

持续集成/持续部署

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.

特征

  • 同时使用多个翻译引擎

  • 划词翻译

  • 播放发音

  • 支持在线翻译引擎

  • 支持离线词典

  • 多种使用方式:shell,python,vim

  • 魔术文本:en: 改变翻译来源为英文,:zh_CN 改变翻译目标为中文,<file 翻译文件 file 的内容

  • 允许自定义

  • 好的命令行补全,尤其是 zsh ,补全选项和翻译历史

  • 用户手册:man trans

  • 漂亮的用户界面

  • 跨平台

  • 丰富的应用编程接口,从 shell 和 python 轻松调用

  • 好的文档

  • 单元测试,保证代码质量

  • 持续集成/持续部署

  • 干净的代码

  • 尊重 PEP484

  • 尊重 PEP621

  • 尊重 XDG

最后但并非不重要:它是开源软件.

看看 文档

此外:欢迎贡献代码啊!请保持代码干净和测试通过!