Skip to content

Commit 85cf502

Browse files
committed
Add translation interval time
1 parent 8e98697 commit 85cf502

File tree

5 files changed

+85
-4
lines changed

5 files changed

+85
-4
lines changed

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

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.airsaid.localization.config.SettingsComponent">
3-
<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">
3+
<grid id="27dc6" binding="contentJPanel" layout-manager="GridLayoutManager" row-count="4" 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="558" height="400"/>
6+
<xy x="20" y="20" width="560" height="401"/>
77
</constraints>
88
<properties/>
99
<border type="none"/>
@@ -149,9 +149,52 @@
149149
</component>
150150
</children>
151151
</grid>
152-
<vspacer id="54b46">
152+
<grid id="cad41" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
153+
<margin top="0" left="0" bottom="0" right="0"/>
154+
<constraints>
155+
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
156+
</constraints>
157+
<properties/>
158+
<border type="line" title="Others"/>
159+
<children>
160+
<component id="fce43" class="javax.swing.JLabel">
161+
<constraints>
162+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
163+
</constraints>
164+
<properties>
165+
<text value="Translation interval(second):"/>
166+
</properties>
167+
</component>
168+
<component id="8226e" class="com.intellij.openapi.ui.ComboBox" binding="translationIntervalComboBox">
169+
<constraints>
170+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
171+
</constraints>
172+
<properties>
173+
<model>
174+
<item value="0"/>
175+
<item value="1"/>
176+
<item value="2"/>
177+
<item value="3"/>
178+
<item value="4"/>
179+
<item value="5"/>
180+
<item value="6"/>
181+
<item value="7"/>
182+
<item value="8"/>
183+
<item value="9"/>
184+
<item value="10"/>
185+
</model>
186+
</properties>
187+
</component>
188+
<hspacer id="15755">
189+
<constraints>
190+
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
191+
</constraints>
192+
</hspacer>
193+
</children>
194+
</grid>
195+
<vspacer id="1a75f">
153196
<constraints>
154-
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
197+
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
155198
</constraints>
156199
</vspacer>
157200
</children>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class SettingsComponent {
5959
private JBCheckBox enableCacheCheckBox;
6060
private ComboBox<String> maxCacheSizeComboBox;
6161
private JCheckBox useGoogleComCheckBox;
62+
private ComboBox<String> translationIntervalComboBox;
6263

6364
public SettingsComponent() {
6465
initTranslatorComponents();
@@ -204,4 +205,11 @@ public void setMaxCacheSize(int maxCacheSize) {
204205
maxCacheSizeComboBox.setSelectedItem(String.valueOf(maxCacheSize));
205206
}
206207

208+
public int getTranslationInterval() {
209+
return Integer.parseInt((String) Objects.requireNonNull(translationIntervalComboBox.getSelectedItem()));
210+
}
211+
212+
public void setTranslationInterval(int intervalTime) {
213+
translationIntervalComboBox.setItem(String.valueOf(intervalTime));
214+
}
207215
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ private void initComponents() {
6262
settingsComponent.setSelectedTranslator(translators.get(settingsState.getSelectedTranslator().getKey()));
6363
settingsComponent.setEnableCache(settingsState.isEnableCache());
6464
settingsComponent.setMaxCacheSize(settingsState.getMaxCacheSize());
65+
settingsComponent.setTranslationInterval(settingsState.getTranslationInterval());
6566
}
6667

6768
@Override
@@ -74,6 +75,7 @@ public boolean isModified() {
7475
isChanged |= settingsState.getAppKey(selectedTranslator.getKey()).equals(selectedTranslator.getAppKey());
7576
isChanged |= settingsState.isEnableCache() == settingsComponent.isEnableCache();
7677
isChanged |= settingsState.getMaxCacheSize() == settingsComponent.getMaxCacheSize();
78+
isChanged |= settingsState.getTranslationInterval() == settingsComponent.getTranslationInterval();
7779
LOG.info("isModified: " + isChanged);
7880
return isChanged;
7981
}
@@ -95,12 +97,14 @@ public void apply() {
9597
}
9698
settingsState.setEnableCache(settingsComponent.isEnableCache());
9799
settingsState.setMaxCacheSize(settingsComponent.getMaxCacheSize());
100+
settingsState.setTranslationInterval(settingsComponent.getTranslationInterval());
98101

99102
TranslatorService translatorService = TranslatorService.getInstance();
100103
translatorService.setSelectedTranslator(selectedTranslator);
101104
translatorService.setUseGoogleComHost(settingsComponent.isUseGoogleCom());
102105
translatorService.setEnableCache(settingsComponent.isEnableCache());
103106
translatorService.setMaxCacheSize(settingsComponent.getMaxCacheSize());
107+
translatorService.setTranslationInterval(settingsComponent.getTranslationInterval());
104108
}
105109

106110
@Override
@@ -114,6 +118,7 @@ public void reset() {
114118
settingsComponent.setAppKey(settingsState.getAppKey(selectedTranslator.getKey()));
115119
settingsComponent.setEnableCache(settingsState.isEnableCache());
116120
settingsComponent.setMaxCacheSize(settingsState.getMaxCacheSize());
121+
settingsComponent.setTranslationInterval(settingsState.getTranslationInterval());
117122
}
118123

119124
@Override

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public void initSetting() {
7070
translatorService.setSelectedTranslator(getSelectedTranslator());
7171
translatorService.setEnableCache(isEnableCache());
7272
translatorService.setMaxCacheSize(getMaxCacheSize());
73+
translatorService.setTranslationInterval(getTranslationInterval());
7374
}
7475
}
7576

@@ -129,6 +130,14 @@ public void setMaxCacheSize(int maxCacheSize) {
129130
state.maxCacheSize = maxCacheSize;
130131
}
131132

133+
public int getTranslationInterval() {
134+
return state.translationInterval;
135+
}
136+
137+
public void setTranslationInterval(int intervalTime) {
138+
state.translationInterval = intervalTime;
139+
}
140+
132141
@Override
133142
public @Nullable SettingsState.State getState() {
134143
return state;
@@ -145,5 +154,6 @@ static class State {
145154
public boolean isEnableCache = true;
146155
public int maxCacheSize = 100;
147156
public boolean isUseGoogleCom;
157+
public int translationInterval = 2; // 2 second
148158
}
149159
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public final class TranslatorService {
4747
private final TranslationCacheService cacheService;
4848
private final Map<String, AbstractTranslator> translators;
4949
private boolean isEnableCache = true;
50+
private int intervalTime;
5051

5152
public TranslatorService() {
5253
translators = new LinkedHashMap<>();
@@ -114,6 +115,7 @@ public String doTranslate(@NotNull Lang fromLang, @NotNull Lang toLang, @NotNull
114115
String result = selectedTranslator.doTranslate(fromLang, toLang, text);
115116
LOG.info(String.format("doTranslate result: %s", result));
116117
cacheService.put(getCacheKey(fromLang, toLang, text), result);
118+
delay(intervalTime);
117119
return result;
118120
}
119121

@@ -133,8 +135,21 @@ public void setMaxCacheSize(int maxCacheSize) {
133135
cacheService.setMaxCacheSize(maxCacheSize);
134136
}
135137

138+
public void setTranslationInterval(int intervalTime) {
139+
this.intervalTime = intervalTime;
140+
}
141+
136142
private String getCacheKey(@NotNull Lang fromLang, @NotNull Lang toLang, @NotNull String text) {
137143
return fromLang.getCode() + "_" + toLang.getCode() + "_" + text;
138144
}
139145

146+
private void delay(int second) {
147+
if (second <= 0) return;
148+
try {
149+
LOG.info(String.format("doTranslate delay time: %d second.", second));
150+
Thread.sleep(second * 1000L);
151+
} catch (InterruptedException e) {
152+
e.printStackTrace();
153+
}
154+
}
140155
}

0 commit comments

Comments
 (0)