Skip to content

Commit

Permalink
Merge pull request #10 from chrisb09/master
Browse files Browse the repository at this point in the history
provide detected language information
  • Loading branch information
dynomake authored Aug 6, 2024
2 parents 7bbe430 + 4ded65a commit 60e6146
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repositories {
dependencies {
// other depends
implementation 'net.clojars.suuft:libretranslate-java:1.0.5'
implementation 'net.clojars.suuft:libretranslate-java:1.0.7'
}
```

Expand All @@ -38,7 +38,7 @@ Depend:
<dependency>
<groupId>net.clojars.suuft</groupId>
<artifactId>libretranslate-java</artifactId>
<version>1.0.5</version>
<version>1.0.7</version>
</dependency>
```
### `Usage:`
Expand Down
12 changes: 11 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'net.clojars.suuft'
version '1.0.5'
version '1.0.7'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
Expand Down Expand Up @@ -34,6 +34,16 @@ publishing {
publications {
gpr(MavenPublication) {
from(components.java)


pom {
licenses {
license {
name = 'MIT License'
url = 'https://github.com/dynomake/libretranslate-java/blob/master/LICENSE'
}
}
}
}
}
}
10 changes: 9 additions & 1 deletion src/main/java/net/suuft/libretranslate/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class Translator {
private String urlApi = "https://translate.fedilab.app/translate";

public String translate(@NonNull String from, @NonNull String to, @NonNull String request) {
return translateDetect(from, to, request).getTranslatedText();
}

public TranslateResponse translateDetect(@NonNull String from, @NonNull String to, @NonNull String request) {
try {

URL url = new URL(urlApi);
Expand All @@ -42,7 +46,7 @@ public String translate(@NonNull String from, @NonNull String to, @NonNull Strin
Scanner s = new Scanner(responseStream).useDelimiter("\\A");
String response = s.hasNext() ? s.next() : "";

return JsonUtil.from(response, TranslateResponse.class).getTranslatedText();
return JsonUtil.from(response, TranslateResponse.class);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
Expand All @@ -54,6 +58,10 @@ public String translate(@NonNull Language from, @NonNull Language to, @NonNull S
return translate(from.getCode(), to.getCode(), request);
}

public TranslateResponse translateDetect(@NonNull Language to, @NonNull String request) {
return translateDetect("auto", to.getCode(), request);
}

public String translate(@NonNull Language to, @NonNull String request) {
if (to == Language.NONE) return request;
return translate("auto", to.getCode(), request);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/net/suuft/libretranslate/type/TranslateResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@
@FieldDefaults(level = AccessLevel.PRIVATE)
public class TranslateResponse {
String translatedText;

DetectedLanguage detectedLanguage;

@Getter
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public static class DetectedLanguage {
int confidence;
String language;
}
}

0 comments on commit 60e6146

Please sign in to comment.