Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
suuft committed Dec 17, 2022
0 parents commit 565d0c0
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions README.md
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>
```
39 changes: 39 additions & 0 deletions build.gradle
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 added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
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
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'libretranslate-java'

54 changes: 54 additions & 0 deletions src/main/java/net/suuft/libretranslate/Translator.java
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 src/main/java/net/suuft/libretranslate/type/TranslateRequest.java
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 src/main/java/net/suuft/libretranslate/type/TranslateResponse.java
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;
}
20 changes: 20 additions & 0 deletions src/main/java/net/suuft/libretranslate/util/JsonUtil.java
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);
}
}

0 comments on commit 565d0c0

Please sign in to comment.