Skip to content

Commit 41ec4e3

Browse files
committed
feat: support of api-keys
1 parent 600b185 commit 41ec4e3

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repositories {
1616
1717
dependencies {
1818
// other depends
19-
implementation "space.dynomake:libretranslate-java:1.0.8"
19+
implementation "space.dynomake:libretranslate-java:1.0.9"
2020
}
2121
```
2222

@@ -37,7 +37,7 @@ Depend:
3737
<dependency>
3838
<groupId>space.dynomake</groupId>
3939
<artifactId>libretranslate-java</artifactId>
40-
<version>1.0.8</version>
40+
<version>1.0.9</version>
4141
</dependency>
4242
```
4343
### `Usage:`
@@ -48,4 +48,5 @@ System.out.println(Translator.translate(Language.RUSSIAN, Language.ENGLISH, "Д
4848
If you have LibreTranslate installed on your server, you can change the URL for requests. Example:
4949
```java
5050
Translator.setUrlApi("https://your_domain_with_libretranslate.com/translate");
51+
Translator.setApiKey("your_api_key_if_required");
5152
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'space.dynomake'
7-
version '1.0.8'
7+
version '1.0.9'
88
tasks.withType(JavaCompile) {
99
options.encoding = 'UTF-8'
1010
}

src/main/java/space/dynomake/libretranslate/Translator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public class Translator {
1717
@Setter
1818
private String urlApi = "https://translate.fedilab.app/translate";
1919

20+
@Setter
21+
private String apiKey = "unknown";
22+
2023
public String translate(@NonNull String from, @NonNull String to, @NonNull String request) {
2124
return translateDetect(from, to, request).getTranslatedText();
2225
}
@@ -35,7 +38,7 @@ public TranslateResponse translateDetect(@NonNull String from, @NonNull String t
3538

3639
OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
3740

38-
writer.write("q=" + URLEncoder.encode(request, "UTF-8") + "&source=" + from + "&target=" + to + "&format=text");
41+
writer.write("q=" + URLEncoder.encode(request, "UTF-8") + "&source=" + from + "&api_key=" + apiKey + "&target=" + to + "&format=text");
3942
writer.flush();
4043
writer.close();
4144
httpConn.getOutputStream().close();

0 commit comments

Comments
 (0)