|
19 | 19 |
|
20 | 20 | import com.airsaid.localization.constant.Constants; |
21 | 21 | import com.airsaid.localization.services.AndroidValuesService; |
| 22 | +import com.airsaid.localization.translate.TranslationException; |
22 | 23 | import com.airsaid.localization.translate.lang.Lang; |
23 | 24 | import com.airsaid.localization.translate.lang.Languages; |
24 | 25 | import com.airsaid.localization.translate.services.TranslatorService; |
@@ -66,6 +67,7 @@ public class TranslateTask extends Task.Backgroundable { |
66 | 67 | private final AndroidValuesService mValueService; |
67 | 68 |
|
68 | 69 | private OnTranslateListener mOnTranslateListener; |
| 70 | + private TranslationException mTranslationError; |
69 | 71 |
|
70 | 72 | public interface OnTranslateListener { |
71 | 73 | void onTranslateSuccess(); |
@@ -125,6 +127,12 @@ public void run(@NotNull ProgressIndicator progressIndicator) { |
125 | 127 | File valueFile = mValueService.getValueFile(resourceDir, toLanguage, valueFileName); |
126 | 128 | writeTranslatedValues(progressIndicator, valueFile, translatedValues); |
127 | 129 | } |
| 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 | + } |
128 | 136 | } |
129 | 137 | } |
130 | 138 |
|
@@ -193,8 +201,14 @@ private void doTranslate(@NotNull ProgressIndicator progressIndicator, |
193 | 201 | if (TextUtil.isEmptyOrSpacesLineBreak(text)) { |
194 | 202 | continue; |
195 | 203 | } |
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 | + } |
198 | 212 | } else if (child instanceof XmlTag) { |
199 | 213 | doTranslate(progressIndicator, toLanguage, (XmlTag) child); |
200 | 214 | } |
|
0 commit comments