Skip to content

Commit 316e69e

Browse files
committed
Add Microsoft Translator
1 parent e1c7d0d commit 316e69e

File tree

9 files changed

+363
-23
lines changed

9 files changed

+363
-23
lines changed

src/main/java/com/airsaid/localization/config/SettingsComponent.form

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
<grid id="27dc6" binding="contentJPanel" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="0" left="0" bottom="0" right="0"/>
55
<constraints>
6-
<xy x="20" y="20" width="500" height="400"/>
6+
<xy x="20" y="20" width="539" height="400"/>
77
</constraints>
88
<properties/>
99
<border type="none"/>
1010
<children>
11-
<grid id="c2e63" layout-manager="GridLayoutManager" row-count="3" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
11+
<grid id="c2e63" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
1212
<margin top="0" left="0" bottom="0" right="0"/>
1313
<constraints>
1414
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -67,14 +67,6 @@
6767
</constraints>
6868
<properties/>
6969
</component>
70-
<component id="4ef73" class="com.airsaid.localization.ui.FixedLinkLabel" binding="applyLink">
71-
<constraints>
72-
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
73-
</constraints>
74-
<properties>
75-
<text value="Apply..."/>
76-
</properties>
77-
</component>
7870
<component id="63820" class="javax.swing.JButton" binding="supportLanguagesButton" default-binding="true">
7971
<constraints>
8072
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
@@ -88,6 +80,14 @@
8880
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
8981
</constraints>
9082
</hspacer>
83+
<component id="4ef73" class="com.airsaid.localization.ui.FixedLinkLabel" binding="applyLink">
84+
<constraints>
85+
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
86+
</constraints>
87+
<properties>
88+
<text value="Apply..."/>
89+
</properties>
90+
</component>
9191
</children>
9292
</grid>
9393
<grid id="b866e" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">

src/main/java/com/airsaid/localization/config/SettingsComponent.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,17 @@ public void setTranslators(@NotNull Map<String, AbstractTranslator> translators)
107107
public void setSelectedTranslator(@NotNull AbstractTranslator selected) {
108108
LOG.info("setSelectedTranslator: " + selected);
109109
translatorsComboBox.setSelectedItem(selected);
110-
if (isSelectedDefaultTranslator(selected)) {
111-
appIdLabel.setVisible(false);
112-
appKeyLabel.setVisible(false);
113-
appIdField.setVisible(false);
114-
appKeyField.setVisible(false);
115-
} else {
116-
appIdLabel.setVisible(true);
117-
appKeyLabel.setVisible(true);
118-
appIdField.setVisible(true);
119-
appKeyField.setVisible(true);
120-
appIdField.setText(selected.getAppId());
121-
appKeyField.setText(selected.getAppKey());
122-
}
110+
111+
boolean isNeedAppId = selected.isNeedAppId();
112+
appIdLabel.setVisible(isNeedAppId);
113+
appIdField.setVisible(isNeedAppId);
114+
if (isNeedAppId) appIdField.setText(selected.getAppId());
115+
116+
boolean isNeedAppKey = selected.isNeedAppKey();
117+
appKeyLabel.setVisible(isNeedAppKey);
118+
appKeyField.setVisible(isNeedAppKey);
119+
if (isNeedAppKey) appKeyField.setText(selected.getAppKey());
120+
123121
String applyAppIdUrl = selected.getApplyAppIdUrl();
124122
if (!StringUtil.isEmpty(applyAppIdUrl)) {
125123
applyLink.setVisible(true);

src/main/java/com/airsaid/localization/translate/TranslatorConfigurable.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ public interface TranslatorConfigurable {
2424
@NotNull
2525
List<Lang> getSupportedLanguages();
2626

27+
boolean isNeedAppId();
28+
2729
@Nullable
2830
String getAppId();
2931

32+
boolean isNeedAppKey();
33+
3034
@Nullable
3135
String getAppKey();
3236

src/main/java/com/airsaid/localization/translate/impl/google/GoogleTranslator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ public class GoogleTranslator extends AbstractTranslator {
4242
return PluginIcons.GOOGLE_ICON;
4343
}
4444

45+
@Override
46+
public boolean isNeedAppId() {
47+
return false;
48+
}
49+
50+
@Override
51+
public boolean isNeedAppKey() {
52+
return false;
53+
}
54+
4555
@Override
4656
@NotNull
4757
public List<Lang> getSupportedLanguages() {
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.airsaid.localization.translate.impl.microsoft;
2+
3+
import com.airsaid.localization.translate.TranslationResult;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import java.util.List;
7+
import java.util.Objects;
8+
9+
/**
10+
* @author airsaid
11+
*/
12+
public class MicrosoftTranslationResult implements TranslationResult {
13+
14+
private List<Translation> translations;
15+
16+
@Override
17+
public @NotNull String getTranslationResult() {
18+
if (translations != null && !translations.isEmpty()) {
19+
String result = translations.get(0).getText();
20+
return result != null ? result : "";
21+
}
22+
return "";
23+
}
24+
25+
public List<Translation> getTranslations() {
26+
return translations;
27+
}
28+
29+
public void setTranslations(List<Translation> translations) {
30+
this.translations = translations;
31+
}
32+
33+
@Override
34+
public boolean equals(Object o) {
35+
if (this == o) return true;
36+
if (o == null || getClass() != o.getClass()) return false;
37+
MicrosoftTranslationResult that = (MicrosoftTranslationResult) o;
38+
return Objects.equals(translations, that.translations);
39+
}
40+
41+
@Override
42+
public int hashCode() {
43+
return Objects.hash(translations);
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return "MicrosoftTranslationResult{" +
49+
"translations=" + translations +
50+
'}';
51+
}
52+
53+
public static class Translation {
54+
private String text;
55+
private String to;
56+
57+
public String getText() {
58+
return text;
59+
}
60+
61+
public void setText(String text) {
62+
this.text = text;
63+
}
64+
65+
public String getTo() {
66+
return to;
67+
}
68+
69+
public void setTo(String to) {
70+
this.to = to;
71+
}
72+
73+
@Override
74+
public boolean equals(Object o) {
75+
if (this == o) return true;
76+
if (o == null || getClass() != o.getClass()) return false;
77+
Translation that = (Translation) o;
78+
return Objects.equals(text, that.text) && Objects.equals(to, that.to);
79+
}
80+
81+
@Override
82+
public int hashCode() {
83+
return Objects.hash(text, to);
84+
}
85+
86+
@Override
87+
public String toString() {
88+
return "Translation{" +
89+
"text='" + text + '\'' +
90+
", to='" + to + '\'' +
91+
'}';
92+
}
93+
}
94+
95+
public static class Error {
96+
private String code;
97+
private String message;
98+
99+
public String getCode() {
100+
return code;
101+
}
102+
103+
public void setCode(String code) {
104+
this.code = code;
105+
}
106+
107+
public String getMessage() {
108+
return message;
109+
}
110+
111+
public void setMessage(String message) {
112+
this.message = message;
113+
}
114+
115+
@Override
116+
public boolean equals(Object o) {
117+
if (this == o) return true;
118+
if (o == null || getClass() != o.getClass()) return false;
119+
Error error = (Error) o;
120+
return Objects.equals(code, error.code) && Objects.equals(message, error.message);
121+
}
122+
123+
@Override
124+
public int hashCode() {
125+
return Objects.hash(code, message);
126+
}
127+
128+
@Override
129+
public String toString() {
130+
return "Error{" +
131+
"code='" + code + '\'' +
132+
", message='" + message + '\'' +
133+
'}';
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)