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

Support for XML/HTML tags on the API calls and a myriad of other things #20

Merged
Merged
Show file tree
Hide file tree
Changes from 64 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
99cbd51
:memo: Add badge to README.md
Omochice Oct 1, 2021
7f6c36e
:pencil: change usage text
Omochice Oct 4, 2021
cc47b73
:muscle: Delete `stdin` flag
Omochice Oct 4, 2021
2b3b3d7
:memo: Update README
Omochice Oct 4, 2021
4c68191
⤴ Update version of library
Omochice Oct 5, 2021
abb607b
🥷 divide package
Omochice Oct 5, 2021
05e3e80
:bug: fix test and ci
Omochice Oct 5, 2021
2f009cc
:muscle: Add test
Omochice Oct 6, 2021
7cd58e1
🐛 Fix default config directory permission to 0755
nikukyugamer Mar 12, 2022
05af429
Merge pull request #9 from nikukyugamer/fix/initial-created-config-di…
Omochice Mar 15, 2022
34b894b
Add renovate.json (#11)
renovate[bot] Oct 14, 2023
d678911
chore: use conventional commit (#15)
Omochice Oct 14, 2023
61df4e5
Update module github.com/mattn/go-isatty to v0.0.19 (#12)
renovate[bot] Oct 14, 2023
eddfdc5
fix(deps): update module github.com/urfave/cli/v2 to v2.25.7 (#13)
renovate[bot] Oct 14, 2023
3f96119
chore(deps): update actions/checkout action to v4 (#16)
renovate[bot] Oct 15, 2023
c9827a0
chore(deps): update actions/setup-go action to v4 (#17)
renovate[bot] Oct 15, 2023
fa26521
chore(deps): update goreleaser/goreleaser-action action to v5 (#18)
renovate[bot] Oct 15, 2023
4facc0d
build: update golang version
Omochice Oct 16, 2023
95c5647
ci: update ci
Omochice Oct 16, 2023
a9df66b
ci: fix go-version
Omochice Oct 17, 2023
2518457
fix(deps): update module github.com/mattn/go-isatty to v0.0.20 (#19)
renovate[bot] Oct 17, 2023
aedcf21
chore: update .goreleaser
Omochice Oct 17, 2023
a3ad1ee
build: update go.sum
Omochice Oct 17, 2023
1d66b44
Chore: small fixes
GwynethLlewelyn Oct 31, 2023
87357ee
chore: refactor code for returning error message
GwynethLlewelyn Oct 31, 2023
7eef4a2
chore: major code refactoring
GwynethLlewelyn Nov 1, 2023
58f33f1
feat: add simple function to return usage
GwynethLlewelyn Nov 1, 2023
8b58c63
feat: adding usage call
GwynethLlewelyn Nov 3, 2023
0636257
chore: refactor code to avoid object ambiguities
GwynethLlewelyn Nov 3, 2023
944d39b
chore: add help for languages
GwynethLlewelyn Nov 3, 2023
5e0cd38
fix: add = to flag `type` usage line
GwynethLlewelyn Nov 3, 2023
5e62c0d
feat: adding autocomplete files
GwynethLlewelyn Nov 4, 2023
fbdc54f
doc: mentioned the autocompletion feature
GwynethLlewelyn Nov 4, 2023
5f89a11
fix: correctly display versions and build dates
GwynethLlewelyn Nov 4, 2023
c6e4252
chore: refactor more code, add option for glossary
GwynethLlewelyn Nov 4, 2023
e61e3a4
docs: better organise the explanatons, add links
GwynethLlewelyn Nov 6, 2023
9296235
chore: refactoring code — moving setup to “Before”
GwynethLlewelyn Nov 6, 2023
ad808a9
feat: add more flag support, upgrade dependencies
GwynethLlewelyn Nov 9, 2023
d607b66
fix: revert changes: init must be done in main()
GwynethLlewelyn Nov 9, 2023
980e070
feat: major refactoring, we’ll get rid of settings
GwynethLlewelyn Nov 9, 2023
a430fc8
chore: adding ChatGPT-generated texts for testing
GwynethLlewelyn Nov 9, 2023
5ed3cb0
feat: adding support for (simple) debugging
GwynethLlewelyn Nov 9, 2023
43a488a
test: test data in XML
GwynethLlewelyn Nov 9, 2023
ff8bfa5
docs: update README with latest changes
GwynethLlewelyn Nov 10, 2023
20a21fc
add readline package
GwynethLlewelyn Nov 10, 2023
dc61118
fix: interactive prompt now works with readline
GwynethLlewelyn Nov 10, 2023
b67dfe7
Update README.md
GwynethLlewelyn Apr 12, 2024
a667d2d
chore: upgrade to latest versions yadda yadda
GwynethLlewelyn Apr 12, 2024
7d4cd82
Bug: fix expected error text for DEEPL_TOKEN
GwynethLlewelyn Apr 12, 2024
3216480
Docs: add backticks on comments
GwynethLlewelyn Apr 12, 2024
a84af8d
Docs: changes suggested by @coderabbitai
GwynethLlewelyn Apr 12, 2024
51db229
Docs: comments ending with period
GwynethLlewelyn Apr 12, 2024
83552de
Fix: match correct error text (changed on main.go)
GwynethLlewelyn Apr 12, 2024
2c2531f
Bug: possible scoping issues with deeplToken (?)
GwynethLlewelyn Apr 12, 2024
9697d82
Merge branch 'GwynethLlewelyn-adding-XML-tag-support' of github.com:G…
GwynethLlewelyn Apr 12, 2024
c755de4
Chore: bump year to 2024
GwynethLlewelyn Apr 12, 2024
b8771ea
Fix: add timeout as per @coderabbitai suggestion
GwynethLlewelyn Apr 13, 2024
bfc7478
Fix: add try-catch as per @coderabbitai suggestion
GwynethLlewelyn Apr 13, 2024
004f431
Chore: add test for Exists; err.Error() is redundant
GwynethLlewelyn Apr 13, 2024
48b7bd5
Docs: add comment
GwynethLlewelyn Apr 13, 2024
a439085
Fix: check for edge case of empty string
GwynethLlewelyn Apr 13, 2024
94ebee2
Chore: use http.StatusXXX instead of numbers
GwynethLlewelyn Apr 13, 2024
10970bf
Bug: missing `)`
GwynethLlewelyn Apr 13, 2024
04b8aeb
Bug: fix a testing bug
GwynethLlewelyn Apr 13, 2024
e535d92
Doc: fix stupid typo
GwynethLlewelyn Apr 13, 2024
8fd3387
Docs: add comment made by @coderabbitai
GwynethLlewelyn Apr 13, 2024
36d8b1b
Fix: error checking for writing configuration file
GwynethLlewelyn Apr 13, 2024
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