Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Support for XML/HTML tags on the API calls and a myriad of oth… #25

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
charset = utf-8
indent_style = tab
indent_size = tab
tab_width = 4
trim_trailing_whitespace = true

# The property below is not yet universally supported
[*.md]
max_line_length = 108
word_wrap = true
# Markdown sometimes uses two spaces at the end to
# mark soft line breaks
trim_trailing_whitespace = false

[*.css]
indent_style = space
indent_size = 2
8 changes: 4 additions & 4 deletions .github/workflows/autorelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ jobs:
steps:
-
name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
-
name: Unshallow
run: git fetch --prune --unshallow
-
name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.17
go-version: stable
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: release --rm-dist
Expand Down
16 changes: 5 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,21 @@ name: go-test

on:
push:
paths:
- "**/*.go"
- ".github/workflows/ci.yml"
pull_request:
paths:
- "**/*.go"
- ".github/workflows/ci.yml"


jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Git Checkout Go Module
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Set up go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.17
go-version: stable
check-latest: true
- name: Dependencies
run: go get -v -t -d ./...
- name: Go test
run: go test -v
run: go test -v -timeout 30m ./...
58 changes: 58 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,59 @@
~*
# Stupid macOS temporary files

# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


Icon?

# Thumbnails
._*
nohup.out

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# Stuff from the Nova editor
.nova
node_modules
package.json
package-lock.json
.eslintrc.yml
.prettierrc.json
.env

#
logs
testdata

profile.out
coverage.html
coverage.txt
delay.txt

*.log

# executables
deepl-translate-cli

# originally by @Omochice
dist/
30 changes: 0 additions & 30 deletions .goreleaser.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Omochice
Copyright (c) 2021,2024 Omochice

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
131 changes: 101 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,125 @@
# ✍️Deepl translate cli
[![go-test](https://github.com/Omochice/deepl-translate-cli/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/Omochice/deepl-translate-cli/actions/workflows/ci.yml)
[![goreleaser](https://github.com/Omochice/deepl-translate-cli/actions/workflows/autorelease.yml/badge.svg)](https://github.com/Omochice/deepl-translate-cli/actions/workflows/autorelease.yml)

![sampleMovie](https://i.gyazo.com/907bae0779d11c324576ee7777768312.gif)
# ✍️ [DeepL](https://www.deepl.com) Translate CLI (Unofficial)

![sampleMovie](https://i.gyazo.com/09a4801d44e85980f83666dceda0166e.gif)

## Installation

### Via the [GitHub release page](https://github.com/Omochice/deepl-translate-cli/releases)

1. Download zipped file from [Releases](https://github.com/Omochice/deepl-translate-cli/releases).

2. Unzip downloaded file.

3. Move executable file into directory in PATH. (like `$HOME/.local/bin/`)
3. Move the executable file into a directory in your `PATH` (e.g., `$HOME/.local/bin/`).

### By `go install`

## Installation
```console
go install github.com/Omochice/deepl-translate-cli@latest
```

### By [github release page](https://github.com/Omochice/deepl-translate-cli/releases)
1. Get deepl access token. See [here](https://www.deepl.com/docs-api).
## Basic usage

2. Set access token as `DEEPL_TOKEN`
1. First, [get a DeepL access token](https://www.deepl.com/docs-api). It looks like a [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier) with the characters `:fx` appended to it.

ex. in `Bash`.
2. Assign the access token to the `DEEPL_TOKEN` environment variable.

```bash
export DEEPL_TOKEN <YOUR TOKEN>
e.g., in `bash`:

```console
export DEEPL_TOKEN=<YOUR DEEPL API TOKEN>
```

3. Make configure file in `<user home directory>/.config/deepl-translate-cli/setting.json`.
3. On the first run, if `$HOME/.config/deepl-translate-cli/setting.json` does not exist, it gets automatically created.

If run command without existing setting file, auto make it.
The format of the settings file is as shown below:

For write setting file, see [this page](https://www.deepl.com/docs-api/translating-text/request/).
```json
{
"source_lang": "FILLIN",
"target_lang": "FILLIN"
}
```

### By `go install`
```sh
go install github.com/Omochice/deepl-translate-cli@latest
```
For all existing languages that can be translated, as well as their identifying tags, see [this page](https://www.deepl.com/docs-api/translating-text/request/). You can also query the server directly:

## Usage
```console
deepl-translate-cli languages

```

4. If the filename path is not specified, text is read from `STDIN`.

Currently, only one file path can be specified as an argument.

- If you want to select `source_lang`/`target_lang` _without_ using the settings file, you can use the command-line parameters `--source_lang (-s)` and `target_lang (-t)` instead.

- If you want translate from existing file.
```console
$ deepl-translate-cli <text.txt>
cat <text.txt> | deepl-translate-cli --source_lang ES --target_lang DE
```

- If you want use stdin.
- with pipe
```console
$ echo "hello" | deepl-translate-cli --stdin
```
- with input
```console
$ deepl-translate-cli --stdin
<input text that wanted translate> <Enter>
```
- If you are a Pro plan user, switch to the correct endpoint URL with the `--pro` flag.

_**Note**: This feature has not been tested, because the developers only have a free plan._

```console
cat <text.txt> | deepl-translate-cli --pro

```

- Note that it's also possible to run `deepl-translate-cli` in interactive mode, when the input comes from a TTY and not a pipe. In this case, only the first sentence typed (terminated by pressing **ENTER**) will be sent via the API for translation. The before-mentioned flags will also be available in this mode.

## More advanced usage

`deepl-translate-cli` now includes more commands, namely,

- `deepl-translate-cli usage` which will query DeepL to return the number of characters still available for translations.
- `deepl-translate-cli languages` will show the languages currently supported by DeepL. By default, only the _source_ languages are listed; with the `--type target` flag, it will also show those languages (and variants) that are available as translation targets.
- `deepl-translate-cli glossary-language-pairs` retrieves the list of language pairs supported by the glossary feature. Right now, it only does that — you cannot use glossaries yet.

DeepL is also able to translate structured text, i.e. text inside HTML or XML tags. This requires using a few more parameters; see `./deepl-translate-cli translate --help` for a list of all the options. While all are supported and sent to DeepL for processing, there are many possible combinations (some of which make no sense) which haven't been thoroughly tested.

## Shell autocompletion (⚠️ experimental)

Under the `autocomplete` folder are three scripts to enable auto-completion (for `bash`, `zsh`, and PowerShell). To use these, do the following (the example is for `bash`):

```console
PROG=deepl-translate-cli source autocomplete/bash_autocomplete
```

## ⚠️ Warning! ⚠️

If you run the tests, these may actually use your API Token, and consume some of your monthly credits!

Make sure you call `deepl-translate-cli usage` every now and then, to be sure you're well within your limits (half a million characters per month for free accounts; however, unlike other services, Unicode characters just count as one character each!).

## TODO

- Support uploading documents for translation (the API allows that as well)
- Better configuration/settings support (the system, as it is now, offers too few choices)
- Make calls purely in JSON (as opposed to using `application/x-www-form-urlencoded` to post data, while retrieving the results in JSON)
- Write tests!
- Add more glossary-related options

## Known bugs 🪳

- When trying to run help _without_ a valid authentication token (which will be the case), the error message is confusing
- Help formatting is quite a bit off on many of the (larger) entries
- Wrong orders of parameters/commands give unexpected errors
- You can only give _one_ filename as input (to do more, you'll have to use shell scripting to browse through all files and feed them to `deepl-translate-cli`)
- The interactive command has some annoing quirks and just translates one single (non-structured) sentence; additionally, it has a _huge_ overhead (but it sort of works)

## Building

If you wish to embed the build's author in the executable binary (to distinguish _your_ build from someone else's), you can build this with

```console
go build -ldflags "-X main.TheBuilder=<YOUR NAME HERE>"
```

## Disclaimer

None of the developers are affiliated with [DeepL](https://www.deepl.com/) and this code should not be considered to represent an endorsement by DeepL or any of its affiliates, partners or subsidiaries. It is released in the hope that it might be helpful to the Go programming community (which lacks official support by DeepL at the time of writing), without any warranty whatsoever (see [LICENSE](./LICENSE) for more information).
35 changes: 35 additions & 0 deletions autocomplete/bash_autocomplete
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#! /bin/bash

: ${PROG:=$(basename ${BASH_SOURCE})}

# Macs have bash3 for which the bash-completion package doesn't include
# _init_completion. This is a minimal version of that function.
_cli_init_completion() {
COMPREPLY=()
_get_comp_words_by_ref "$@" cur prev words cword
}

_cli_bash_autocomplete() {
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
local cur opts base words
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return
else
_cli_init_completion -n "=:" || return
fi
words=("${words[@]:0:$cword}")
if [[ "$cur" == "-"* ]]; then
requestComp="${words[*]} ${cur} --generate-shell-completion"
else
requestComp="${words[*]} --generate-shell-completion"
fi
opts=$(eval "${requestComp}" 2>/dev/null)
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
}

complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG
unset PROG
13 changes: 13 additions & 0 deletions autocomplete/powershell_autocomplete.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
$fn = $($MyInvocation.MyCommand.Name)
$name = $fn -replace "(.*)\.ps1$", '$1'
Register-ArgumentCompleter -Native -CommandName $name -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
$other = "$wordToComplete --generate-shell-completion"
Try {
Invoke-Expression $other | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
} Catch {
Write-Error "Error generating completions: $_"
}
}
Loading
Loading