Skip to content

Commit 42d616a

Browse files
committed
Delayed error throwing to avoid losing successfully translated text
1 parent c56e306 commit 42d616a

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/main/java/com/airsaid/localization/task/TranslateTask.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.airsaid.localization.constant.Constants;
2121
import com.airsaid.localization.services.AndroidValuesService;
22+
import com.airsaid.localization.translate.TranslationException;
2223
import com.airsaid.localization.translate.lang.Lang;
2324
import com.airsaid.localization.translate.lang.Languages;
2425
import com.airsaid.localization.translate.services.TranslatorService;
@@ -66,6 +67,7 @@ public class TranslateTask extends Task.Backgroundable {
6667
private final AndroidValuesService mValueService;
6768

6869
private OnTranslateListener mOnTranslateListener;
70+
private TranslationException mTranslationError;
6971

7072
public interface OnTranslateListener {
7173
void onTranslateSuccess();
@@ -125,6 +127,12 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
125127
File valueFile = mValueService.getValueFile(resourceDir, toLanguage, valueFileName);
126128
writeTranslatedValues(progressIndicator, valueFile, translatedValues);
127129
}
130+
// If an exception occurs during the translation of the language,
131+
// the translation of the subsequent languages is terminated.
132+
// This prevents the loss of successfully translated strings in that language.
133+
if (mTranslationError != null) {
134+
throw mTranslationError;
135+
}
128136
}
129137
}
130138

@@ -193,8 +201,14 @@ private void doTranslate(@NotNull ProgressIndicator progressIndicator,
193201
if (TextUtil.isEmptyOrSpacesLineBreak(text)) {
194202
continue;
195203
}
196-
String translatedText = mTranslatorService.doTranslate(Languages.AUTO, toLanguage, text);
197-
ApplicationManager.getApplication().runReadAction(() -> xmlText.setValue(translatedText));
204+
try {
205+
String translatedText = mTranslatorService.doTranslate(Languages.AUTO, toLanguage, text);
206+
ApplicationManager.getApplication().runReadAction(() -> xmlText.setValue(translatedText));
207+
} catch (TranslationException e) {
208+
LOG.warn(e);
209+
// Just catch the error and wait for that file to be translated and released.
210+
mTranslationError = e;
211+
}
198212
} else if (child instanceof XmlTag) {
199213
doTranslate(progressIndicator, toLanguage, (XmlTag) child);
200214
}

0 commit comments

Comments
 (0)