soimort / translate-shell

:speech_balloon: Command-line translator using Google Translate, Bing Translator, Yandex.Translate, etc.
https://www.soimort.org/translate-shell
The Unlicense
7k stars 392 forks source link

[feature] Get translate content from clipboard automatically #472

Closed Freed-Wu closed 1 year ago

Freed-Wu commented 2 years ago

Such as watch when xsel -ob's result has changed, translate it and print to stdout.

Thanks!

WhiteBlackGoose commented 2 years ago

Exactly what i needed right now. Although imo it's not trans' job to do it. Here's what I use

#!/bin/bash
echo "" | xclip -sel clip
cpbrd=$(xclip -sel clip -o 2> /dev/null)
while :
do
    sleep 1
    newcpbrd=$(xclip -sel clip -o 2> /dev/null)
    if [[ "$cpbrd" != "$newcpbrd" ]]; then
        cpbrd=$newcpbrd
        printf "DE: $cpbrd\n"
        printf "EN: "
        trans -s "de" -t "en" -b "$cpbrd"
        printf "\n"
    fi
done
Freed-Wu commented 2 years ago

More clippers can be found https://github.com/felixonmars/ydcv/pull/71/files

And I hope it is multi process, that is after trans -I, I can input anything to translate, and trans check clipboard's change to translate it in the same time. Like https://github.com/felixonmars/ydcv/issues/66

Freed-Wu commented 2 years ago

Share my script:

#!/usr/bin/env bash
cpbrd="$(xsel -o | perl -0pe 's/-\n//g;s/\n/ /g;s/^ | $//g')"
while :
do
    newcpbrd="$(xsel -o | perl -0pe 's/-\n//g;s/\n/ /g;s/^ | $//g')"
    if [[ "$cpbrd" != "$newcpbrd" ]]; then
        cpbrd=$newcpbrd
        trans "$cpbrd"
    fi
done

Refer https://github.com/felixonmars/ydcv/issues/67

Freed-Wu commented 1 year ago

I realize one alternative for myself. Support translate clipboard, shell completion, etc. So I close this issue now.