|
| 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 | +} |
0 commit comments