Skip to content

Commit b406c75

Browse files
musagilAirsaid
authored andcommitted
Add DeepL Support
1 parent 6427764 commit b406c75

File tree

5 files changed

+333
-9
lines changed

5 files changed

+333
-9
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright 2021 Airsaid. https://github.com/airsaid
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.airsaid.localization.translate.impl.deepl;
19+
20+
import com.airsaid.localization.translate.TranslationResult;
21+
import org.jetbrains.annotations.NotNull;
22+
23+
import java.util.List;
24+
import java.util.Objects;
25+
26+
/**
27+
* @author musagil
28+
*/
29+
public class DeepLTranslationResult implements TranslationResult {
30+
31+
private List<Translation> translations;
32+
33+
@Override
34+
public @NotNull String getTranslationResult() {
35+
if (translations != null && !translations.isEmpty()) {
36+
String result = translations.get(0).getText();
37+
return result != null ? result : "";
38+
}
39+
return "";
40+
}
41+
42+
public List<Translation> getTranslations() {
43+
return translations;
44+
}
45+
46+
public void setTranslations(List<Translation> translations) {
47+
this.translations = translations;
48+
}
49+
50+
@Override
51+
public boolean equals(Object o) {
52+
if (this == o) return true;
53+
if (o == null || getClass() != o.getClass()) return false;
54+
DeepLTranslationResult that = (DeepLTranslationResult) o;
55+
return Objects.equals(translations, that.translations);
56+
}
57+
58+
@Override
59+
public int hashCode() {
60+
return Objects.hash(translations);
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return "DeepLTranslationResult{" +
66+
"translations=" + translations +
67+
'}';
68+
}
69+
70+
public static class Translation {
71+
private String text;
72+
private String to;
73+
74+
public String getText() {
75+
return text;
76+
}
77+
78+
public void setText(String text) {
79+
this.text = text;
80+
}
81+
82+
public String getTo() {
83+
return to;
84+
}
85+
86+
public void setTo(String to) {
87+
this.to = to;
88+
}
89+
90+
@Override
91+
public boolean equals(Object o) {
92+
if (this == o) return true;
93+
if (o == null || getClass() != o.getClass()) return false;
94+
Translation that = (Translation) o;
95+
return Objects.equals(text, that.text) && Objects.equals(to, that.to);
96+
}
97+
98+
@Override
99+
public int hashCode() {
100+
return Objects.hash(text, to);
101+
}
102+
103+
@Override
104+
public String toString() {
105+
return "Translation{" +
106+
"text='" + text + '\'' +
107+
", to='" + to + '\'' +
108+
'}';
109+
}
110+
}
111+
112+
public static class Error {
113+
private String code;
114+
private String message;
115+
116+
public String getCode() {
117+
return code;
118+
}
119+
120+
public void setCode(String code) {
121+
this.code = code;
122+
}
123+
124+
public String getMessage() {
125+
return message;
126+
}
127+
128+
public void setMessage(String message) {
129+
this.message = message;
130+
}
131+
132+
@Override
133+
public boolean equals(Object o) {
134+
if (this == o) return true;
135+
if (o == null || getClass() != o.getClass()) return false;
136+
Error error = (Error) o;
137+
return Objects.equals(code, error.code) && Objects.equals(message, error.message);
138+
}
139+
140+
@Override
141+
public int hashCode() {
142+
return Objects.hash(code, message);
143+
}
144+
145+
@Override
146+
public String toString() {
147+
return "Error{" +
148+
"code='" + code + '\'' +
149+
", message='" + message + '\'' +
150+
'}';
151+
}
152+
}
153+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright 2021 Airsaid. https://github.com/airsaid
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.airsaid.localization.translate.impl.deepl;
19+
20+
import com.airsaid.localization.translate.AbstractTranslator;
21+
import com.airsaid.localization.translate.lang.Lang;
22+
import com.airsaid.localization.translate.lang.Languages;
23+
import com.airsaid.localization.translate.util.GsonUtil;
24+
import com.airsaid.localization.translate.util.UrlBuilder;
25+
import com.intellij.openapi.diagnostic.Logger;
26+
import com.intellij.openapi.util.Pair;
27+
import com.intellij.util.io.RequestBuilder;
28+
import icons.PluginIcons;
29+
import org.jetbrains.annotations.NotNull;
30+
import org.jetbrains.annotations.Nullable;
31+
32+
import javax.swing.*;
33+
import java.util.ArrayList;
34+
import java.util.List;
35+
36+
/**
37+
* @author musagil
38+
*/
39+
public class DeepLTranslator extends AbstractTranslator {
40+
41+
private static final Logger LOG = Logger.getInstance(DeepLTranslator.class);
42+
43+
private static final String KEY = "DeepL";
44+
private static final String HOST_URL = "https://api-free.deepl.com/v2";
45+
private static final String TRANSLATE_URL = HOST_URL.concat("/translate");
46+
private static final String APPLY_APP_ID_URL = "https://www.deepl.com/pro-api?cta=header-pro-api/";
47+
48+
private List<Lang> supportedLanguages;
49+
50+
@Override
51+
public @NotNull String getKey() {
52+
return KEY;
53+
}
54+
55+
@Override
56+
public @NotNull String getName() {
57+
return "DeepL";
58+
}
59+
60+
@Override
61+
public @Nullable Icon getIcon() {
62+
return PluginIcons.DEEP_L_ICON;
63+
}
64+
65+
@Override
66+
public boolean isNeedAppId() {
67+
return false;
68+
}
69+
70+
@Override
71+
public @NotNull List<Lang> getSupportedLanguages() {
72+
if (supportedLanguages == null) {
73+
supportedLanguages = new ArrayList<>();
74+
supportedLanguages.add(Languages.BULGARIAN);
75+
supportedLanguages.add(Languages.CZECH);
76+
supportedLanguages.add(Languages.DANISH);
77+
supportedLanguages.add(Languages.GERMAN);
78+
supportedLanguages.add(Languages.GREEK);
79+
supportedLanguages.add(new Lang(118, "en-gb", "English (British)", "English (British)"));
80+
supportedLanguages.add(new Lang(119, "en-us", "English (American)", "English (American)"));
81+
supportedLanguages.add(Languages.SPANISH);
82+
supportedLanguages.add(Languages.ESTONIAN);
83+
supportedLanguages.add(Languages.FINNISH);
84+
supportedLanguages.add(Languages.FRENCH);
85+
supportedLanguages.add(Languages.HUNGARIAN);
86+
supportedLanguages.add(Languages.INDONESIAN);
87+
supportedLanguages.add(Languages.ITALIAN);
88+
supportedLanguages.add(Languages.JAPANESE);
89+
supportedLanguages.add(Languages.LITHUANIAN);
90+
supportedLanguages.add(Languages.LATVIAN);
91+
supportedLanguages.add(Languages.DUTCH);
92+
supportedLanguages.add(Languages.POLISH);
93+
supportedLanguages.add(new Lang(120, "pt-br", "Portuguese (Brazilian)", "Portuguese (Brazilian)"));
94+
supportedLanguages.add(new Lang(121, "pt-pt", "Portuguese (European)", "Portuguese (European)"));
95+
supportedLanguages.add(Languages.ROMANIAN);
96+
supportedLanguages.add(Languages.RUSSIAN);
97+
supportedLanguages.add(Languages.SLOVAK);
98+
supportedLanguages.add(Languages.SLOVENIAN);
99+
supportedLanguages.add(Languages.SWEDISH);
100+
supportedLanguages.add(Languages.TURKISH);
101+
supportedLanguages.add(Languages.UKRAINIAN);
102+
supportedLanguages.add(Languages.CHINESE_SIMPLIFIED);
103+
104+
}
105+
return supportedLanguages;
106+
}
107+
108+
@Override
109+
public String getAppKeyDisplay() {
110+
return "KEY";
111+
}
112+
113+
@Override
114+
public @Nullable String getApplyAppIdUrl() {
115+
return APPLY_APP_ID_URL;
116+
}
117+
118+
@Override
119+
public @NotNull String getRequestUrl(@NotNull Lang fromLang, @NotNull Lang toLang, @NotNull String text) {
120+
return new UrlBuilder(TRANSLATE_URL).build();
121+
}
122+
123+
@Override
124+
public @NotNull List<Pair<String, String>> getRequestParams(@NotNull Lang fromLang, @NotNull Lang toLang, @NotNull String text) {
125+
List<Pair<String, String>> params = new ArrayList<>();
126+
params.add(Pair.create("text", text));
127+
params.add(Pair.create("target_lang", toLang.getCode()));
128+
return params;
129+
}
130+
131+
@Override
132+
@NotNull
133+
public String getRequestBody(@NotNull Lang fromLang, @NotNull Lang toLang, @NotNull String text) {
134+
return "";
135+
}
136+
137+
@Override
138+
public void configureRequestBuilder(@NotNull RequestBuilder requestBuilder) {
139+
requestBuilder.tuner(connection -> {
140+
connection.setRequestProperty("Authorization", "DeepL-Auth-Key " + getAppKey());
141+
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
142+
});
143+
}
144+
145+
@Override
146+
public @NotNull String parsingResult(@NotNull Lang fromLang, @NotNull Lang toLang, @NotNull String text, @NotNull String resultText) {
147+
LOG.info("parsingResult: " + resultText);
148+
return GsonUtil.getInstance().getGson().fromJson(resultText, DeepLTranslationResult.class).getTranslationResult();
149+
}
150+
151+
}

src/main/java/com/airsaid/localization/translate/services/TranslatorService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.airsaid.localization.translate.AbstractTranslator;
2121
import com.airsaid.localization.translate.impl.ali.AliTranslator;
2222
import com.airsaid.localization.translate.impl.baidu.BaiduTranslator;
23+
import com.airsaid.localization.translate.impl.deepl.DeepLTranslator;
2324
import com.airsaid.localization.translate.impl.google.GoogleTranslator;
2425
import com.airsaid.localization.translate.impl.googleapi.GoogleApiTranslator;
2526
import com.airsaid.localization.translate.impl.microsoft.MicrosoftTranslator;
@@ -81,6 +82,9 @@ public TranslatorService() {
8182
AliTranslator aliTranslator = new AliTranslator();
8283
translators.put(aliTranslator.getKey(), aliTranslator);
8384

85+
DeepLTranslator deepLTranslator = new DeepLTranslator();
86+
translators.put(deepLTranslator.getKey(), microsoftTranslator);
87+
8488
cacheService = TranslationCacheService.getInstance();
8589

8690
translationInterceptors = new ArrayList<>();

src/main/java/icons/PluginIcons.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
* @author airsaid
2525
*/
2626
public interface PluginIcons {
27-
Icon TRANSLATE_ACTION_ICON = load("/icons/icon_translate.svg");
28-
Icon GOOGLE_ICON = load("/icons/icon_google.svg");
29-
Icon BAIDU_ICON = load("/icons/icon_baidu.svg");
30-
Icon YOUDAO_ICON = load("/icons/icon_youdao.svg");
31-
Icon MICROSOFT_ICON = load("/icons/icon_microsoft.svg");
32-
Icon ALI_ICON = load("/icons/icon_ali.svg");
27+
Icon TRANSLATE_ACTION_ICON = load("/icons/icon_translate.svg");
28+
Icon GOOGLE_ICON = load("/icons/icon_google.svg");
29+
Icon BAIDU_ICON = load("/icons/icon_baidu.svg");
30+
Icon YOUDAO_ICON = load("/icons/icon_youdao.svg");
31+
Icon MICROSOFT_ICON = load("/icons/icon_microsoft.svg");
32+
Icon ALI_ICON = load("/icons/icon_ali.svg");
33+
Icon DEEP_L_ICON = load("/icons/deepl_logo_blue.svg");
3334

34-
private static Icon load(String path) {
35-
return IconLoader.getIcon(path, PluginIcons.class);
36-
}
35+
private static Icon load(String path) {
36+
return IconLoader.getIcon(path, PluginIcons.class);
37+
}
3738
}
Lines changed: 15 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)