📜 ⬆️ ⬇️

Translation of selected text from any language into Russian

I want to share with you my life hacking.
First of all, it is intended for people who are too lazy to climb into the dictionary whenever they meet an unfamiliar foreign word in the text.

I want to tell you how to get the translation of the selected text as a desktop alert.


Many desktop "dictionaries" have a function for translating a selection, which is precisely intended to "not climb into the dictionary", but for some reason I have always had problems with it: it does not translate when necessary; translates when not needed, etc.

Surely, many of you have seen various scripts for using google translate from the console, but I find this method inconvenient in most cases, despite the fact that I prefer the CLI.
')
I thought that it would be nice to be able to quickly translate the selected text or word and see the result, in the form of a desktop alert.
It remains only to combine google translate cli + selections + notifications .

Decision


A very simple interface for sending notifications from the console is the notify-send utility, which can be found in the libnotify-bin package. Example:
notify-send 'title' 'message'
Will give the following result


The xsel utility is used to obtain the current selection in the X Window System .
xsel -o
Print the current selection to standard output.

Step 1
Install the necessary packages:
sudo apt-get install libnotify-bin xsel
I’m sure everyone knows how to install packages on their system, so I don’t give examples.

Step 2
Create a file named seltr and copy the following 2 lines into it:

 #!/usr/bin/env bash notify-send -u critical "$(xsel -o)" "$(wget -U "Mozilla/5.0" -qO - "http://translate.google.com/translate_a/t?client=t&text=$(xsel -o | sed "s/[\"'<>]//g")&sl=auto&tl=ru" | sed 's/\[\[\[\"//' | cut -d \" -f 1)" 

The -u critical parameter is needed so that the notification can overlap any windows.

Parameters google translate request:
text=$(xsel -o | sed "s/[\"']//g") → the source text is the current selection with the cuts ' and ' .
sl=auto → auto detection of the source language.
tl=ru → result language - Russian.

Step 3
Make this file executable and move it to / usr / bin / :
sudo chmod 777 ./seltr && sudo mv ./seltr / usr / bin /

Step 4
It now remains to assign a key combination to the seltr command (my choice is alt + 5). To do this, you can use the standard heartboards provided by your working environment, or use xbindkeys .

How it works



Few screenshots


English

Deutsch

French

Ukrainian

Chinese


Advantages and disadvantages


+ Ability to translate text snippets.
+ Requires a minimum number of actions to receive a transfer.
+ Support almost all languages.
+ No need to install separate dictionaries.

- Does not work without internet connection.
- There is no possibility to select the text of the translation.
* Unity, GNOME2, Xfce4, KDE4.

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


All Articles