https://github.com/freed-wu/translate-shell
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.
https://github.com/freed-wu/translate-shell
bing chatgpt commandline-tool google haici lftp llama llamacpp lsp-server openai prompt python repl shell stardict translate vim youdao
Last synced: 3 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/freed-wu/translate-shell
- Owner: Freed-Wu
- License: gpl-3.0
- Created: 2022-12-02T23:30:38.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-24T18:18:12.000Z (3 months ago)
- Last Synced: 2025-03-28T16:40:33.032Z (3 months ago)
- Topics: bing, chatgpt, commandline-tool, google, haici, lftp, llama, llamacpp, lsp-server, openai, prompt, python, repl, shell, stardict, translate, vim, youdao
- Language: Python
- Homepage: https://translate-shell.readthedocs.io/
- Size: 452 KB
- Stars: 38
- Watchers: 2
- Forks: 5
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Citation: CITATION.cff
Awesome Lists containing this project
README
# translate-shell
[](https://results.pre-commit.ci/latest/github/Freed-Wu/translate-shell/main)
[](https://github.com/Freed-Wu/translate-shell/actions)
[](https://codecov.io/gh/Freed-Wu/translate-shell)
[](https://translate-shell.readthedocs.io)
[](https://deepsource.io/gh/Freed-Wu/translate-shell)[](https://github.com/Freed-Wu/translate-shell/releases)
[](https://github.com/Freed-Wu/translate-shell/releases/latest)
[](https://github.com/Freed-Wu/translate-shell/issues)
[](https://github.com/Freed-Wu/translate-shell/issues?q=is%3Aissue+is%3Aclosed)
[](https://github.com/Freed-Wu/translate-shell/pulls)
[](https://github.com/Freed-Wu/translate-shell/pulls?q=is%3Apr+is%3Aclosed)
[](https://github.com/Freed-Wu/translate-shell/discussions)
[](https://github.com/Freed-Wu/translate-shell/milestones)
[](https://github.com/Freed-Wu/translate-shell/network/members)
[](https://github.com/Freed-Wu/translate-shell/stargazers)
[](https://github.com/Freed-Wu/translate-shell/watchers)
[](https://github.com/Freed-Wu/translate-shell/graphs/contributors)
[](https://github.com/Freed-Wu/translate-shell/graphs/commit-activity)
[](https://github.com/Freed-Wu/translate-shell/commits)
[](https://github.com/Freed-Wu/translate-shell/releases/latest)[](https://github.com/Freed-Wu/translate-shell/blob/main/LICENSE)
[](https://github.com/Freed-Wu/translate-shell)
[](https://github.com/Freed-Wu/translate-shell)
[](https://github.com/Freed-Wu/translate-shell)
[](https://github.com/Freed-Wu/translate-shell)
[](https://github.com/Freed-Wu/translate-shell)
[](https://github.com/Freed-Wu/translate-shell)[](https://pypi.org/project/translate-shell/#description)
[](https://pypi.org/project/translate-shell/#history)
[](https://pypi.org/project/translate-shell/#files)
[](https://pypi.org/project/translate-shell/#files)
[](https://pypi.org/project/translate-shell/#files)
[](https://pypi.org/project/translate-shell/#files)Translate text by:
- online translators
- bing
- youdaozhiyun
- haici
- offline dictionaries
- stardict
- LLM
- OpenAI
- llama: use your local modelSupports:
- 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
```sh
trans --translators=google,bing,haici,stardict crush
```
#### REPL
```console
$ 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
ハッカー
> 画家 # translate text
painter; artist
> ! # enter shell
$ echo $SHELL # execute a shell command
/usr/bin/zsh
$ exit # exit shell
>
```
#### TUI
##### Vim
```vim
Translate --translators=google,bing Free as in Freedom
```
#### GUI
##### GNU/Linux

##### Android

### Script
#### Python
```pycon
>>> 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
```console
$ xsel -o | trans --format json | jq -r '"《\(.results[].paraphrase)》的英文是 \(.text)."'
《大教堂和集市》的英文是 the cathedral and the bazaar.
```#### Vim Script
```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### CI/CD
#### Github Action
This repo provides an action to translate `*.po` of a repository. See
[inputs](https://github.com/Freed-Wu/translate-shell/blob/main/action.yml).
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
[`.po`](https://www.gnu.org/software/gettext/manual/gettext.html#PO-Files)s,
then translate the changed `.po`s and `git commit`.Examples:
- [tmux-zh](https://github.com/Freed-Wu/tmux-zh/blob/main/.github/workflows/version.yml)
```yaml
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:[email protected]/$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:
```bash
# 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 `.po`s:
- [sphinx-intl](https://sphinx-intl.readthedocs.io): Generate `.po` for any
project using sphinx to generate document.
- [po4a](https://po4a.org): Generate `.po` for any project which use markdown,
`LaTeX`, man, ... to write document.## Similar Projects
See [comparison](https://translate-shell.readthedocs.io/en/latest/resources/translator.html).
## Features
- Translate with different translators at same time, like [translator](https://github.com/skywind3000/translator)
- Translate clipboard contents automatically, like [ydcv](https://github.com/felixonmars/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, `