diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..ba1ec5c --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..dfefbdd --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +LibreTranslate Java Restful Client +--- +At some point, we had to translate the Java text, but we didn't find comfortable and understandable libraries, so I wrote my decision. +### `Add as depend:` + +| **Gradle:** + +```groovy +repositories { + // other repositories + maven { + name = "clojars.org" + url = uri("ttp://clojars.org/repo") + } +} + +dependencies { + // other depends + implementation 'net.clojars.suuft:libretranslate-java:1.0.0' +} +``` + +| **Maven:** + +Repository: + +```xml + + clojars.org + http://clojars.org/repo + +``` + +Depend: + +```xml + + + net.clojars.suuft + libretranslate-java + 1.0.0 + +``` \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..1a0799e --- /dev/null +++ b/build.gradle @@ -0,0 +1,39 @@ +plugins { + id 'java' + id 'maven-publish' +} + +group 'net.clojars.suuft' +version '1.0.0' +tasks.withType(JavaCompile) { + options.encoding = 'UTF-8' +} + +repositories { + mavenCentral() +} + +dependencies { + implementation 'com.google.code.gson:gson:2.8.7' + compileOnly 'org.projectlombok:lombok:1.18.24' + annotationProcessor 'org.projectlombok:lombok:1.18.24' +} + +publishing { + repositories { + maven { + name = "clojars" + url = uri("https://clojars.org/repo") + credentials { + username = clojarsUserName + password = clojarsDeployToken + } + } + + } + publications { + gpr(MavenPublication) { + from(components.java) + } + } +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..41d9927 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..41dfb87 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..5279814 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'libretranslate-java' + diff --git a/src/main/java/net/suuft/libretranslate/Translator.java b/src/main/java/net/suuft/libretranslate/Translator.java new file mode 100644 index 0000000..ac6f0f4 --- /dev/null +++ b/src/main/java/net/suuft/libretranslate/Translator.java @@ -0,0 +1,54 @@ +package net.suuft.libretranslate; + +import lombok.NonNull; +import lombok.experimental.UtilityClass; +import net.suuft.libretranslate.type.TranslateRequest; +import net.suuft.libretranslate.type.TranslateResponse; +import net.suuft.libretranslate.util.JsonUtil; + +import javax.net.ssl.HttpsURLConnection; +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.*; +import java.nio.charset.StandardCharsets; +import java.util.Scanner; + +@UtilityClass +public class Translator { + + + public String get(@NonNull String from, @NonNull String to, @NonNull String request) { + try { + + URL url = new URL("https://translate.fedilab.app/translate"); + HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); + httpConn.setRequestMethod("POST"); + + httpConn.setRequestProperty("accept", "application/json"); + httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + + httpConn.setDoOutput(true); + + OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream()); + + writer.write("q=" + URLEncoder.encode(request, "UTF-8") + "&source=" + from + "&target=" + to + "&format=text&api_key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); + writer.flush(); + writer.close(); + httpConn.getOutputStream().close(); + + if (!(httpConn.getResponseCode() / 100 == 2)) return "Falled translate!"; + + InputStream responseStream = httpConn.getInputStream(); + Scanner s = new Scanner(responseStream).useDelimiter("\\A"); + String response = s.hasNext() ? s.next() : ""; + + return JsonUtil.from(response, TranslateResponse.class).getTranslatedText(); + } catch (Exception e) { + e.printStackTrace(); + return "Falled translate!"; + } + } + +} diff --git a/src/main/java/net/suuft/libretranslate/type/TranslateRequest.java b/src/main/java/net/suuft/libretranslate/type/TranslateRequest.java new file mode 100644 index 0000000..4e7cfac --- /dev/null +++ b/src/main/java/net/suuft/libretranslate/type/TranslateRequest.java @@ -0,0 +1,16 @@ +package net.suuft.libretranslate.type; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.experimental.FieldDefaults; + +@AllArgsConstructor +@FieldDefaults(level = AccessLevel.PRIVATE) +public class TranslateRequest { + + String q; + String source; + String target; + String format; //"text", +// String api_key; +} diff --git a/src/main/java/net/suuft/libretranslate/type/TranslateResponse.java b/src/main/java/net/suuft/libretranslate/type/TranslateResponse.java new file mode 100644 index 0000000..af5cc50 --- /dev/null +++ b/src/main/java/net/suuft/libretranslate/type/TranslateResponse.java @@ -0,0 +1,13 @@ +package net.suuft.libretranslate.type; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.experimental.FieldDefaults; + +@Getter +@AllArgsConstructor +@FieldDefaults(level = AccessLevel.PRIVATE) +public class TranslateResponse { + String translatedText; +} diff --git a/src/main/java/net/suuft/libretranslate/util/JsonUtil.java b/src/main/java/net/suuft/libretranslate/util/JsonUtil.java new file mode 100644 index 0000000..3711f31 --- /dev/null +++ b/src/main/java/net/suuft/libretranslate/util/JsonUtil.java @@ -0,0 +1,20 @@ +package net.suuft.libretranslate.util; + +import com.google.gson.Gson; +import lombok.NonNull; +import lombok.experimental.UtilityClass; + + +@UtilityClass +public class JsonUtil { + + private final Gson gson = new Gson(); + + public String to(@NonNull Object object) { + return gson.toJson(object); + } + + public T from(@NonNull String s, Class tClass) { + return gson.fromJson(s, tClass); + } +}