Today, with so many devices connected to the Internet and so many sites to visit on the Internet, it is very normal that we find the information we are looking for in a language that we do not speak. We can even find a text in a language that we think we know well and read a word that we don't know what it means. In those cases, what do we do? Well, the normal thing is to consult the page of a translator, such as Bing or Google, but wouldn't it be better if we had a faster and more comfortable way to do it? Well, like practically everything in Linux-based operating systems, there are several ways to achieve it and in this article we will teach you how to translate any text in Ubuntu, as long as the language is included in Google Translator.
It is possible that the following guide will give some respect to users who do not like to touch the terminal very much, but it does not explain anything different than when we say how to add a repository and install a package that is not in the official ones. The difference is that let's create a script that will allow us to launch components available in Ubuntu. That said, below you have the steps to follow to translate any text in Ubuntu.
How to translate texts with Ubuntu
Creating the script notitrans
- In order to use the script, we have to do three installations:
- libnotify-bin in order to receive desktop notifications.
- wget to get the Google translation.
- xsell to read the text that we have marked.
To do this, we open a terminal and write the following:
sudo apt-get install libnotify-bin wget xsel
- Next, we create a new text file (just right click on the desktop and select New Document / Empty Document).
- Inside the document we have to paste the following:
#!/usr/bin/env bash notify-send --icon=info "$(xsel -o)" "$(wget -U "Mozilla/5.0" -qO - "http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=es&dt=t&q=$(xsel -o | sed "s/[\"'<>]//g")" | sed "s/,,,0]],,.*//g" | awk -F'"' '{print $2, $6}')"
- Note: in the script above there is a line that says "tl = es". If you do not want to translate into Spanish, you only have to change the language that you have behind the equal symbol (=).
- We save the previous file and call it "notitrans" (without the quotes).
- Next we have to convert the previous document to an executable file. To do this, we copy it to our personal folder and write the following in a terminal:
chmod +x ~/notitrans
- The next step move the created file to the folder / usr / local / bin / with the following command:
sudo mv ~/notitrans /usr/local/bin/
- We are not done yet. We need (at least) two more steps: now we have to create a keyboard shortcut. To do this we go to System Settings / Keyboard / Shortcuts and click on the plus symbol (+). You will see a window like the following:
- In the window, we put the name we want (I have given it Notitrans) and the command "notitrans" (without the quotes). We can put another command, but we would have to use the same name with which we save the script.
- Now you just need to create the key combination. To do this, we click on the text that says Disabled and enter the desired combination. I have used Control + period (.), Since I know that there is no key combination that uses that shortcut.
Variants
If you don't like the way the translation shows us with the script above, which in fact cannot display very long translations because it is limited by the size of the notification, we can also use some variants. To use these variants we will have to do the same process as the previous one (also change the language if we wish), but using the corresponding code in step 2.
Using Xclip
If we want that, in addition to the notification, we will copy the translation to the clipboard, we will also use xclip. First we make sure that we have it installed with the command:
sudo apt-get install xclip
And in the script that we will create we write the following:
#!/usr/bin/env bash text="$(xsel -o)" translate="$(wget -U "Mozilla/5.0" -qO - "http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=es&dt=t&q=$(echo $text | sed "s/[\"'<>]//g")" | sed "s/,,,0]],,.*//g" | awk -F'"' '{print $2, $6}')" echo "$translate" | xclip -selection clipboard notify-send --icon=info "$text" "$translate"
Using Zenity
And if what we want is to be shown as the previous image (my favorite), we will make sure that we have Zenity installed by typing the command:
sudo apt-get install zenity
And in the document we will write the following:
#!/usr/bin/env bash text="$(xsel -o)" translate="$(wget -U "Mozilla/5.0" -qO - "http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=es&dt=t&q=$(echo $text | sed "s/[\"'<>]//g")" | sed "s/,,,0]],,.*//g" | awk -F'"' '{print $2, $6}')" echo -e "Original text:" "$text"'\n' > /tmp/notitrans echo "Translation:" "$translate" >> /tmp/notitrans zenity --text-info --title="Translation" --filename=/tmp/notitrans
Have you tried any of these scripts? What do you think?
You could have changed the catches, that sings a little
How great ... how fast ...
At least you have linked to the original xD
Excellent, and will it work with other translators like Yandex or Bing?
And if you add this line at the end
spd-say "$ translate"
he sings it to you with that romantic voice
Obviously it is necessary to have spd-say installed
We are here to learn:
—I didn't know about the xsel even though it was created in 2001 by Conrad Parker:
http p: / / linux.die .net / man / 1 / xsel <- (Remove the spaces and you will navigate).
This is how we can write "xsel -o" and it will print on the command line what we have selected and stored in the Ubuntu clipboard.
—What about notify-send because I didn't know about it either (last update dates from 2008)
but reading in:
ht tp: // manpages.ubu ntu.com/man pages / gutsy / man1 / notify-send.1.html
we can practice sending any message graphically to Ubuntu:
notify-send –icon = info "HELLO WORLD!"
We can send a title and message to the notification, try:
notify-send –icon = info Date "` date` "
{BEWARE of those single quotes inside the double ones, the ASCII code is 239}
—Not to mention the venerable wget, you will only remember how powerful it is in this web link:
ht tp: // blog.phen obarbital.info/ 2015/02 / did you know how to download a complete website wget /
–The command sed NEITHER knew it ("I just know that I don't know anything") but investigating we can see how useful it is, I really liked these examples (in English):
http: // ww w.folkstal k.com/2012 /01/sed-command-in-unix-examples.html
I've been "running" the examples and they work fine on my ubuntu.
HERE WE REACHED THE "ARRIVAL":
the translation doesn't work, so I "dissected" the component commands to see which one works fine and which one works badly.
That is why I asked the ducky who knows everything DUCKDUCKGO for help and I found:
ht tp: // w ww. webupd8.or g / 20 16/03 / translate-any-text-you-select-on-your.html
"The original script, which dates back to 2012, no longer works however, I've managed to fix it and decided to share it with you."
AND I THINK IT DOESN'T WORK BECAUSE:
"Google Translate API is a paid service. For website translations, we encourage you to use the Google Website Translator gadget."
https: // cloud. Google. com / translate / v2 / using_rest
So if you have a few dollars to spare, they charge for millions of letters:
https: // cloud. Google. com / translate / v2 / pricing
—THE IS MY THEORY OF WHY IT DOESN'T WORK if someone knows how to put it in demonstrative mode and for didactic purposes then "post" your answer, thanks for your attention!
IN CASE I went back to follow, step by step (and with a different IP address to connect to Google because it "briefly blocked me for unusual traffic" -unusual that we use GNU / Linux wget without a header ha ha ha) the steps described by the citizen Russian
http ps: // there will be ahabr.ru/ post / 137215 / and it doesn't work either, too bad. 8- (
Advantages and disadvantages
+ Возможность переводить фрагMENTы текста.
+ Требуется минимльное количество действий для получения перевода .
+ Podderzhka practical vs yazykov.
+ Отсутствие необходимости в установке отдельных словарей.
- Не работает без подключения к интернету.
- Нет возможности выделить текст перевода.
* Проверенно в Unity, GNOME2, Xfce4, KDE4.
"Pros and cons
+ Ability to translate text fragments.
+ Requires a minimum number of actions to obtain the translation.
+ Support for practically all languages.
+ No need to install specific dictionaries.
- It does not work without an Internet connection.
- Unable to assign translated text.
* Tested on Unity, Gnome2, Xfce4, KDE4. »
TRANSLATION FROM RUSSIAN TO SPANISH THANKS TO «Google Translate».
The author of the post in English answered me kindly and confirmed my suspicion: I abused the google service with my tests:
https://twitter.com/ks7000/status/708398151351341058
It is now, six days later that I tried again very carefully so as not to make mistakes and also tried translating from English to French by changing the «tl = fr» and here is the result:
https://twitter.com/ks7000/status/708397624890662915
Thank you very much, today I learned something new! * -)
damn the time I wanted to install the translator ... I had some problems and when I wanted to uninstall it via terminal I could no longer connect to the internet or wired or wifi, I think I uninstalled the network card driver or something like that. How do I install these things offline? regards
Excellent. You solved my life with this post. I wanted to have an easy-to-use translator without having to go into the browser or any program. Thanks a lot.
I have loved it and I find it very useful, but ... Does anyone know why I add an "en" at the end of all translations ??
Good!
On the line:
| sed «s / ,,, 0]] ,,. * // g» | awk -F '»' '{print $ 2, $ 6}')»
remove (, $ 6) so it looks like this:
| sed «s / ,,, 0]] ,,. * // g» | awk -F '»' '{print $ 2}')»
and ready !! without the "in" at the end.
Regards!!
Good.
Remove (, $ 6) and voila.
Greetings.
Thank you very much for the contribution and for the «Quita (, $ 6)» ;-P
Thank you very much for the contribution, it worked for me in the / usr / bin path
To remove the "en en" that appear at the end change the code as follows
#! / usr / bin / env bash
text = »$ (xsel -o)»
translate = »$ (wget -U« Mozilla / 5.0 »-qO -« http://translate.googleapis.com/trans$
translate2 = $ {translate% ??????}
echo "$ translate2" | xclip -selection clipboard
notify-send –icon = info "$ text" "$ translate2"
and ready
To remove the in in in
modify the last line for this
notify-send –icon = info "$ text" "$ {translate: 0: -17}"
Hi !
When I hit chmod + x ~ / notitrans it tells me that the file is not found
I've come this far ...
Hello, when I put a paragraph to translate it only translates the first sentence, as far as there is a point, it does not translate it completely and I have to be from point to point to translate the whole paragraph, how can I solve it?