Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multi thread #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 45 additions & 15 deletions src/main/java/com/airsaid/localization/task/TranslateTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Semaphore;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -103,6 +104,8 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
for (Lang toLanguage : mToLanguages) {
if (progressIndicator.isCanceled()) break;

Semaphore semaphore = new Semaphore(0);

progressIndicator.setText("Translation to " + toLanguage.getEnglishName() + "...");

VirtualFile resourceDir = mValueFile.getParent().getParent();
Expand All @@ -120,11 +123,13 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
},
Function.identity()
));
List<PsiElement> translatedValues = doTranslate(progressIndicator, toLanguage, toValuesMap, isOverwriteExistingString);
List<PsiElement> translatedValues = doTranslate(progressIndicator, toLanguage, toValuesMap, isOverwriteExistingString, semaphore);
wait(semaphore);
writeTranslatedValues(progressIndicator, new File(toValuePsiFile.getVirtualFile().getPath()), translatedValues);
} else {
List<PsiElement> translatedValues = doTranslate(progressIndicator, toLanguage, null, isOverwriteExistingString);
List<PsiElement> translatedValues = doTranslate(progressIndicator, toLanguage, null, isOverwriteExistingString, semaphore);
File valueFile = mValueService.getValueFile(resourceDir, toLanguage, valueFileName);
wait(semaphore);
writeTranslatedValues(progressIndicator, valueFile, translatedValues);
}
// If an exception occurs during the translation of the language,
Expand All @@ -139,7 +144,8 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
private List<PsiElement> doTranslate(@NotNull ProgressIndicator progressIndicator,
@NotNull Lang toLanguage,
@Nullable Map<String, PsiElement> toValues,
boolean isOverwrite) {
boolean isOverwrite,
Semaphore semaphore) {
LOG.info("doTranslate toLanguage: " + toLanguage.getEnglishName() + ", toValues: " + toValues + ", isOverwrite: " + isOverwrite);

List<PsiElement> translatedValues = new ArrayList<>();
Expand Down Expand Up @@ -167,14 +173,14 @@ private List<PsiElement> doTranslate(@NotNull ProgressIndicator progressIndicato
translatedValues.add(translateValue);
switch (translateValue.getName()) {
case NAME_TAG_STRING:
doTranslate(progressIndicator, toLanguage, translateValue);
doTranslate(progressIndicator, toLanguage, translateValue, semaphore);
break;
case NAME_TAG_STRING_ARRAY:
case NAME_TAG_PLURALS:
XmlTag[] subTags = ApplicationManager.getApplication()
.runReadAction((Computable<XmlTag[]>) translateValue::getSubTags);
for (XmlTag subTag : subTags) {
doTranslate(progressIndicator, toLanguage, subTag);
doTranslate(progressIndicator, toLanguage, subTag, semaphore);
}
break;
}
Expand All @@ -187,7 +193,8 @@ private List<PsiElement> doTranslate(@NotNull ProgressIndicator progressIndicato

private void doTranslate(@NotNull ProgressIndicator progressIndicator,
@NotNull Lang toLanguage,
@NotNull XmlTag xmlTag) {
@NotNull XmlTag xmlTag,
Semaphore semaphore) {
if (progressIndicator.isCanceled() || isXliffTag(xmlTag)) return;

XmlTagValue xmlTagValue = ApplicationManager.getApplication()
Expand All @@ -201,20 +208,43 @@ private void doTranslate(@NotNull ProgressIndicator progressIndicator,
if (TextUtil.isEmptyOrSpacesLineBreak(text)) {
continue;
}
try {
String translatedText = mTranslatorService.doTranslate(Languages.AUTO, toLanguage, text);
ApplicationManager.getApplication().runReadAction(() -> xmlText.setValue(translatedText));
} catch (TranslationException e) {
LOG.warn(e);
// Just catch the error and wait for that file to be translated and released.
mTranslationError = e;
}
ApplicationManager.getApplication().executeOnPooledThread(() -> {
countUp(semaphore);
try {
String translatedText = mTranslatorService.doTranslate(Languages.AUTO, toLanguage, text);
ApplicationManager.getApplication().runReadAction(() -> xmlText.setValue(translatedText));
} catch (TranslationException e) {
ApplicationManager.getApplication().invokeLater(() -> {
LOG.warn(e);
// Just catch the error and wait for that file to be translated and released.
mTranslationError = e;
});
} finally {
countDown(semaphore);
}
});
} else if (child instanceof XmlTag) {
doTranslate(progressIndicator, toLanguage, (XmlTag) child);
doTranslate(progressIndicator, toLanguage, (XmlTag) child, semaphore);
}
}
}

private void wait(Semaphore semaphore) {
while (semaphore.availablePermits() > 0) {
}
}

private void countUp(Semaphore semaphore) {
semaphore.release();
}

private void countDown(Semaphore semaphore) {
try {
semaphore.acquire();
} catch (InterruptedException ignored) {
}
}

private void writeTranslatedValues(@NotNull ProgressIndicator progressIndicator,
@NotNull File valueFile,
@NotNull List<PsiElement> translatedValues) {
Expand Down