📜 ⬆️ ⬇️

Yandex-Transfer in the terminal via Java

Using the translator's API, I wrote for myself a simple program to translate words and phrases, and to work from anywhere. I wanted to use curl, but it's unclear why I got an error that there is no such language.

Now, instead of boring, and sometimes slow, and sometimes such a bright white new tab with too much information, I write in the trans hello terminal and get a translation into Russian, and if trans hi - into English. Of course, you can enter phrases in quotes. It works quickly.

import javax.net.ssl.HttpsURLConnection; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLEncoder; public class YandexTranslate { private static int i = 0; public static void main(String[] args) throws IOException { System.out.println(translate("ru", args[0])); } private static String translate(String lang, String input) throws IOException { String urlStr = "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20150627T071448Z.117dacaac1e63b79.6b1b4bb84635161fcd400dace9fb2220d6f344ef"; URL urlObj = new URL(urlStr); HttpsURLConnection connection = (HttpsURLConnection)urlObj.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream()); dataOutputStream.writeBytes("text=" + URLEncoder.encode(input, "UTF-8") + "&lang=" + lang); InputStream response = connection.getInputStream(); String json = new java.util.Scanner(response).nextLine(); int start = json.indexOf("["); int end = json.indexOf("]"); String translated = json.substring(start + 2, end - 1); i++; if (translated.equals(input) && i < 2) { // if return equal of entered text - we need change direction of translation return translate("en", input); } else return translated; } } 

For the translation to work in any directory, you need to put a .class file and trans.sh in / usr / bin to call it:
 #!/bin/bash if [ $# -eq 0 ] then echo "Enter text for translate" exit 1; fi java -classpath /usr/bin/ YandexTranslate "$1" 

And make it executable: sudo chmod + x trans.sh

If instead of translation you begin to receive a message stating that the key is over, or if it’s not enough — you can get a new key here - paste it where necessary in the Java code, then compile ( javac YandexTranslate.java ) and copy it with the replacement to / usr / bin .
')
This code is also available on Github .

Also, Firefox users can use my extension translator - it is integrated into the context menu, fast-minimalistic and does not affect the DOM, after installation it works immediately and does not require a browser restart. At the moment, the only Yandex-translator for current versions of Firefox.

UPD: It’s better to do such things right on the bash, but it didn’t work right away, but after bockra showed my script, I finished writing it so that I don’t have to indicate the direction of the translation, and you can write several words without quotes. He could not achieve the translation of several words with an ampersand, but also a haller with him.
 #!/bin/bash # # simple console util for translation text to any language using Yandex Translate API # use: trans 'call me, baby # will return translation of a phrase to ru or en language # more example at http://api.yandex.ru/translate/ # input=${@//[&]/%26} link='https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20150627T071448Z.117dacaac1e63b79.6b1b4bb84635161fcd400dace9fb2220d6f344ef&lang=' function translate() { translated=$(curl -s "$link$key&lang=$1&text=$input" | awk -F'"' {' print $10 '}) } translate "ru" if [ "$translated" == "$input" ] then translate "en" fi echo $translated 

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


All Articles