-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
suuft
committed
Dec 17, 2022
0 parents
commit 565d0c0
Showing
10 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
<repository> | ||
<id>clojars.org</id> | ||
<url>http://clojars.org/repo</url> | ||
</repository> | ||
``` | ||
|
||
Depend: | ||
|
||
```xml | ||
|
||
<dependency> | ||
<groupId>net.clojars.suuft</groupId> | ||
<artifactId>libretranslate-java</artifactId> | ||
<version>1.0.0</version> | ||
</dependency> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
rootProject.name = 'libretranslate-java' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!"; | ||
} | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/net/suuft/libretranslate/type/TranslateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/net/suuft/libretranslate/type/TranslateResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> T from(@NonNull String s, Class<T> tClass) { | ||
return gson.fromJson(s, tClass); | ||
} | ||
} |