📜 ⬆️ ⬇️

We translate using Yandex

In the process of messing around with xfce4-dict, I came across almost three years ago: “Translation of selected text from any language into Russian” . The solution turned out to be very simple, but something didn’t work out for me with Google translate. Either because he quickly started issuing captcha, or because he wants money ... He was too lazy to understand, so he simply rewrote the original script using the Yandex.Translate API .

First you need to log in to Yandex and get the key . The key is generated instantly, no one is satisfied with any interrogations and it is not necessary to wait for anything. Of course, before that, you must read and accept the user agreement.

Well, then everything is simple:

#!/bin/sh key="    API" text="$(echo $(xsel -o) | sed "s/[\"\'<>]//g")" translate="$(wget -qO - "https://translate.yandex.net/api/v1.5/tr.json/translate?key=$key&text=$text&lang=ru")" notify-send -u critical "$text" "$(echo $translate | sed 's/.*\[\"\(.*\)\"\].*/\1/')" 

')
Of course, you can do everything where more beautiful: with the insertion of the translation into the clipboard, and with a beautiful answer parser (Yandex can give both JSON [P] and XML ), and with more thoughtful preprocessing of the text, and with larger fragments in POST and with other joys. But this is a matter of taste. A detailed discussion of yuzkeys and different implementations are in the original article .

UPD:

sperson suggests trans . In fact, this is an advanced wrapper for the same googletranslate, which does the preprocessing of the text, and the postprocessing, and the proxy allows you to set, and a lot of useful things. So if Google is satisfied, then it's still easier:

 #!/bin/sh notify-send -u critical "$(xsel -o | trans -no-ansi -l ru)" 


At the exit:


And the article is bigger:

Source: https://habr.com/ru/post/256063/


All Articles