|
| 1 | +package com.airsaid.localization.translate.impl.googleapi; |
| 2 | + |
| 3 | +import com.airsaid.localization.translate.AbstractTranslator; |
| 4 | +import com.airsaid.localization.translate.TranslationException; |
| 5 | +import com.airsaid.localization.translate.lang.Lang; |
| 6 | +import com.airsaid.localization.translate.lang.Languages; |
| 7 | +import com.airsaid.localization.translate.util.GsonUtil; |
| 8 | +import com.intellij.openapi.util.Pair; |
| 9 | +import com.intellij.util.io.RequestBuilder; |
| 10 | +import icons.PluginIcons; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | +import org.jetbrains.annotations.Nullable; |
| 13 | + |
| 14 | +import javax.swing.*; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author airsaid |
| 20 | + */ |
| 21 | +public class GoogleApiTranslator extends AbstractTranslator { |
| 22 | + private static final String KEY = "GoogleApi"; |
| 23 | + private static final String HOST_URL = "https://translation.googleapis.com"; |
| 24 | + private static final String TRANSLATE_URL = HOST_URL.concat("/language/translate/v2"); |
| 25 | + private static final String APPLY_APP_ID_URL = "https://cloud.google.com/translate"; |
| 26 | + |
| 27 | + private List<Lang> supportedLanguages; |
| 28 | + |
| 29 | + @Override |
| 30 | + public @NotNull String getKey() { |
| 31 | + return KEY; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public @NotNull String getName() { |
| 36 | + return "Google (API)"; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public @Nullable Icon getIcon() { |
| 41 | + return PluginIcons.GOOGLE_ICON; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public @NotNull List<Lang> getSupportedLanguages() { |
| 46 | + if (supportedLanguages == null) { |
| 47 | + List<Lang> languages = Languages.getLanguages(); |
| 48 | + supportedLanguages = new ArrayList<>(104); |
| 49 | + for (int i = 1; i <= 104; i++) { |
| 50 | + supportedLanguages.add(languages.get(i)); |
| 51 | + } |
| 52 | + } |
| 53 | + return supportedLanguages; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public @NotNull String getRequestUrl(@NotNull Lang fromLang, @NotNull Lang toLang, @NotNull String text) { |
| 58 | + return TRANSLATE_URL; |
| 59 | + } |
| 60 | + |
| 61 | + @Nullable |
| 62 | + @Override |
| 63 | + public String getApplyAppIdUrl() { |
| 64 | + return APPLY_APP_ID_URL; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public boolean isNeedAppId() { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public @NotNull List<Pair<String, String>> getRequestParams(@NotNull Lang fromLang, @NotNull Lang toLang, @NotNull String text) { |
| 74 | + List<Pair<String, String>> params = new ArrayList<>(); |
| 75 | + params.add(Pair.create("q", text)); |
| 76 | + params.add(Pair.create("target", toLang.getCode())); |
| 77 | + params.add(Pair.create("key", getAppKey())); |
| 78 | + params.add(Pair.create("format", "text")); |
| 79 | + return params; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void configureRequestBuilder(@NotNull RequestBuilder requestBuilder) { |
| 84 | + requestBuilder.tuner(connection -> connection.setRequestProperty("Referer", HOST_URL)); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public @NotNull String parsingResult(@NotNull Lang fromLang, @NotNull Lang toLang, @NotNull String text, @NotNull String resultText) { |
| 89 | + LOG.info("parsingResult: " + resultText); |
| 90 | + GoogleApiTranslationResult result = GsonUtil.getInstance().getGson().fromJson(resultText, GoogleApiTranslationResult.class); |
| 91 | + if (result.isSuccess()) { |
| 92 | + return result.getTranslationResult(); |
| 93 | + } else { |
| 94 | + String message; |
| 95 | + if (result.error != null) { |
| 96 | + message = result.error.message.concat("(").concat(String.valueOf(result.error.code)).concat(")"); |
| 97 | + } else { |
| 98 | + message = "Unknown error"; |
| 99 | + } |
| 100 | + throw new TranslationException(fromLang, toLang, text, message); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments