|
| 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.ali; |
| 19 | + |
| 20 | +import com.airsaid.localization.translate.AbstractTranslator; |
| 21 | +import com.airsaid.localization.translate.TranslationException; |
| 22 | +import com.airsaid.localization.translate.lang.Lang; |
| 23 | +import com.airsaid.localization.translate.lang.Languages; |
| 24 | +import com.aliyun.alimt20181012.Client; |
| 25 | +import com.aliyun.alimt20181012.models.TranslateGeneralRequest; |
| 26 | +import com.aliyun.alimt20181012.models.TranslateGeneralResponse; |
| 27 | +import com.aliyun.alimt20181012.models.TranslateGeneralResponseBody; |
| 28 | +import com.aliyun.teaopenapi.models.Config; |
| 29 | +import com.aliyun.teautil.models.RuntimeOptions; |
| 30 | +import icons.PluginIcons; |
| 31 | +import org.jetbrains.annotations.NotNull; |
| 32 | +import org.jetbrains.annotations.Nullable; |
| 33 | + |
| 34 | +import javax.swing.*; |
| 35 | +import java.util.LinkedList; |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +/** |
| 39 | + * @author airsaid |
| 40 | + */ |
| 41 | +public class AliTranslator extends AbstractTranslator { |
| 42 | + private static final String KEY = "Ali"; |
| 43 | + private static final String ENDPOINT = "mt.aliyuncs.com"; |
| 44 | + private static final String APPLY_APP_ID_URL = "https://www.aliyun.com/product/ai/base_alimt"; |
| 45 | + |
| 46 | + private final Config config = new Config(); |
| 47 | + private List<Lang> supportedLanguages; |
| 48 | + private Client client; |
| 49 | + |
| 50 | + @Override |
| 51 | + public @NotNull String getKey() { |
| 52 | + return KEY; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public @NotNull String getName() { |
| 57 | + return "Ali"; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public @Nullable Icon getIcon() { |
| 62 | + return PluginIcons.ALI_ICON; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public @NotNull List<Lang> getSupportedLanguages() { |
| 67 | + if (supportedLanguages == null) { |
| 68 | + supportedLanguages = new LinkedList<>(); |
| 69 | + final List<Lang> languages = Languages.getLanguages(); |
| 70 | + for (int i = 1; i < languages.size(); i++) { |
| 71 | + Lang lang = languages.get(i); |
| 72 | + if (lang.equals(Languages.UKRAINIAN) || lang.equals(Languages.DARI)) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + if (lang.equals(Languages.CHINESE_SIMPLIFIED)) { |
| 76 | + lang = lang.setTranslationCode("zh"); |
| 77 | + } else if (lang.equals(Languages.CHINESE_TRADITIONAL)) { |
| 78 | + lang = lang.setTranslationCode("zh-tw"); |
| 79 | + } else if (lang.equals(Languages.INDONESIAN)) { |
| 80 | + lang = lang.setTranslationCode("id"); |
| 81 | + } else if (lang.equals(Languages.CROATIAN)) { |
| 82 | + lang = lang.setTranslationCode("hbs"); |
| 83 | + } else if (lang.equals(Languages.HEBREW)) { |
| 84 | + lang = lang.setTranslationCode("he"); |
| 85 | + } |
| 86 | + supportedLanguages.add(lang); |
| 87 | + } |
| 88 | + } |
| 89 | + return supportedLanguages; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String getAppIdDisplay() { |
| 94 | + return "AccessKey ID"; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public String getAppKeyDisplay() { |
| 99 | + return "AccessKey Secret"; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public @Nullable String getApplyAppIdUrl() { |
| 104 | + return APPLY_APP_ID_URL; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String doTranslate(@NotNull Lang fromLang, @NotNull Lang toLang, @NotNull String text) throws TranslationException { |
| 109 | + checkSupportedLanguages(fromLang, toLang, text); |
| 110 | + |
| 111 | + config.setAccessKeyId(getAppId()).setAccessKeySecret(getAppKey()).setEndpoint(ENDPOINT); |
| 112 | + if (client == null) { |
| 113 | + try { |
| 114 | + client = new Client(config); |
| 115 | + } catch (Exception e) { |
| 116 | + throw new TranslationException(fromLang, toLang, text, e); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + TranslateGeneralRequest request = new TranslateGeneralRequest().setFormatType("text").setSourceLanguage(fromLang.getTranslationCode()).setTargetLanguage(toLang.getTranslationCode()).setSourceText(text).setScene("general"); |
| 121 | + RuntimeOptions runtime = new RuntimeOptions(); |
| 122 | + TranslateGeneralResponse response; |
| 123 | + try { |
| 124 | + response = client.translateGeneralWithOptions(request, runtime); |
| 125 | + } catch (Exception e) { |
| 126 | + throw new TranslationException(fromLang, toLang, text, e); |
| 127 | + } |
| 128 | + final TranslateGeneralResponseBody body = response.body; |
| 129 | + if (body.getCode() == 200) { |
| 130 | + return body.getData().translated; |
| 131 | + } else { |
| 132 | + throw new TranslationException(fromLang, toLang, text, body.getMessage() + "(" + body.getCode() + ")"); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments