Skip to content

Commit

Permalink
only translate when source language != target language
Browse files Browse the repository at this point in the history
  • Loading branch information
stoerr committed Mar 26, 2024
1 parent da00ccc commit 460eace
Showing 1 changed file with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ public Stats translateLiveCopy(@Nonnull Resource resource, @Nullable GPTConfigur
resource = resource.getChild("jcr:content");
}
Stats stats = new Stats();

String language = SelectorUtils.findLanguage(resource);
if (language == null) {
throw new IllegalArgumentException("No language found for " + resource.getPath());
}
String languageName = SelectorUtils.getLanguageName(language);
String sourceLanguage = determineSourceLanguage(resource);
if (StringUtils.equals(language, sourceLanguage)) {
LOG.info("Skipping translation because language and source language are {} for {}", language, resource.getPath());
return stats;
}

List<PropertyToTranslate> propertiesToTranslate = new ArrayList<>();
boolean changed = collectPropertiesToTranslate(resource, propertiesToTranslate, stats, translationParameters);

Expand All @@ -84,11 +96,7 @@ public Stats translateLiveCopy(@Nonnull Resource resource, @Nullable GPTConfigur
List<String> valuesToTranslate = propertiesToTranslate.stream()
.map(p -> p.sourceResource.getValueMap().get(p.propertyName, String.class))
.collect(Collectors.toList());
String language = SelectorUtils.findLanguage(resource);
if (language == null) {
throw new IllegalArgumentException("No language found for " + resource.getPath());
}
String languageName = SelectorUtils.getLanguageName(language);

List<String> translatedValues =
translationService.fragmentedTranslation(valuesToTranslate, languageName, configuration);

Expand Down Expand Up @@ -136,6 +144,18 @@ public Stats translateLiveCopy(@Nonnull Resource resource, @Nullable GPTConfigur
return stats;
}

private String determineSourceLanguage(Resource resource) throws WCMException {
LiveRelationship relationship = liveRelationshipManager.getLiveRelationship(resource, false);
if (relationship == null) {
throw new IllegalArgumentException("No live relationship for " + resource.getPath());
}
String sourceLanguage = SelectorUtils.findLanguage(resource.getResourceResolver().getResource(relationship.getSourcePath()));
if (sourceLanguage == null) {
throw new IllegalArgumentException("No source language found for " + resource.getPath());
}
return sourceLanguage;
}

protected void markAsAiTranslated(Resource resource, LiveRelationship liveRelationship, AutoTranslateService.TranslationParameters parameters) throws WCMException {
ModifiableValueMap valueMap = requireNonNull(resource.adaptTo(ModifiableValueMap.class));
AITranslatePropertyWrapper targetWrapper = new AITranslatePropertyWrapper(null, valueMap, null);
Expand Down

0 comments on commit 460eace

Please sign in to comment.