Skip to content

Commit

Permalink
re-translate when additional instructions change
Browse files Browse the repository at this point in the history
  • Loading branch information
stoerr committed Mar 26, 2024
1 parent 460eace commit 0c2995d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

class AITranslatePropertyWrapper {

/**
* PageContent only property: saves the additional instructions the page was translated with.
*/
public static final String PROPERTY_AI_ADDINSTRUCTIONS = "ai_additionalInstructions";

/**
* Saves the date when a resource was automatically translated.
* Find translated resources with /content//*[@ai_translated] .
Expand Down Expand Up @@ -148,6 +153,9 @@ public String[] allLcKeys() {
}

public String[] allAiKeys() {
if (propertyName.startsWith(AI_PREFIX) || propertyName.startsWith(LC_PREFIX)) {
throw new IllegalArgumentException("Property name must not start with " + AI_PREFIX + " or " + LC_PREFIX + ": " + propertyName);
}
return new String[]{
AutoPageTranslateServiceImpl.encodePropertyName(AI_PREFIX, propertyName, AI_ORIGINAL_SUFFIX),
AutoPageTranslateServiceImpl.encodePropertyName(AI_PREFIX, propertyName, AI_TRANSLATED_SUFFIX),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ public Stats translateLiveCopy(@Nonnull Resource resource, @Nullable GPTConfigur
return stats;
}

String additionalInstructions = configuration != null ? configuration.getAdditionalInstructions() : null;
String pageAdditionalInstructions = resource.getValueMap().get(AITranslatePropertyWrapper.PROPERTY_AI_ADDINSTRUCTIONS, String.class);
boolean additionalInstructionsChanged = !StringUtils.equals(additionalInstructions, pageAdditionalInstructions);

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

LOG.debug("Set of property names to translate in {} : {}", resource.getPath(),
propertiesToTranslate.stream()
Expand Down Expand Up @@ -133,6 +137,12 @@ public Stats translateLiveCopy(@Nonnull Resource resource, @Nullable GPTConfigur
}
}

if (additionalInstructionsChanged) {
ModifiableValueMap mvm = requireNonNull(resource.adaptTo(ModifiableValueMap.class));
mvm.put(AITranslatePropertyWrapper.PROPERTY_AI_ADDINSTRUCTIONS, additionalInstructions);
changed = true;
}

changed |= migratePathsToLanguageCopy(resource, language, stats);
if (changed) {
markAsAiTranslated(resource, liveRelationshipManager.getLiveRelationship(resource, false), translationParameters);
Expand Down Expand Up @@ -308,11 +318,12 @@ public void rollback(Resource resource) throws WCMException {
/**
* Searches for properties we have to translate.
*
* @param force all properties have to be retranslated
* @return true if something was changed already
*/
protected boolean collectPropertiesToTranslate(
@Nonnull Resource resource, @Nonnull List<PropertyToTranslate> propertiesToTranslate, @Nonnull Stats stats,
@Nonnull AutoTranslateService.TranslationParameters translationParameters) throws WCMException {
@Nonnull AutoTranslateService.TranslationParameters translationParameters, boolean force) throws WCMException {
boolean changed = false;
LiveRelationship relationship = liveRelationshipManager.getLiveRelationship(resource, false);
if (relationship == null) {
Expand Down Expand Up @@ -349,14 +360,14 @@ protected boolean collectPropertiesToTranslate(
continue; // don't touch cancelled properties
}

if (targetWrapper.isOriginalAsWhenLastTranslating()) {
if (targetWrapper.isOriginalAsWhenLastTranslating() && !force) {
// shortcut: we have a recent translation already
targetWrapper.setCurrentValue(targetWrapper.getTranslatedCopy());
changed = changed || !StringUtils.equals(targetWrapper.getTranslatedCopy(), targetWrapper.getOriginalCopy());
continue;
}

if (isCancelled && targetWrapper.hasSavedTranslation()
if (isCancelled && targetWrapper.hasSavedTranslation() && translationParameters.translateWhenChanged
&& !StringUtils.equals(targetWrapper.getTranslatedCopy(), targetWrapper.getCurrentValue())
&& !StringUtils.equals(targetWrapper.getOriginal(), targetWrapper.getCurrentValue())) {
// = translateWhenChanged override; save manual change. We also exclude the phase during rollout
Expand All @@ -379,7 +390,7 @@ protected boolean collectPropertiesToTranslate(
}
for (Resource child : resource.getChildren()) {
if (!PATTERN_IGNORED_SUBNODE_NAMES.matcher(child.getName()).matches()) {
boolean childChanged = collectPropertiesToTranslate(child, propertiesToTranslate, stats, translationParameters);
boolean childChanged = collectPropertiesToTranslate(child, propertiesToTranslate, stats, translationParameters, force);
changed |= childChanged;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void testCollectPropertiesToTranslate() throws WCMException {
List<AutoPageTranslateServiceImpl.PropertyToTranslate> props = new java.util.ArrayList<>();
AutoPageTranslateServiceImpl.Stats stats = new AutoPageTranslateServiceImpl.Stats();
AutoTranslateService.TranslationParameters parms = new AutoTranslateService.TranslationParameters();
service.collectPropertiesToTranslate(copy, props, stats, parms);
service.collectPropertiesToTranslate(copy, props, stats, parms, false);
assertEquals(2, props.size());
assertEquals("something", props.get(0).propertyName);
assertEquals("/content/de/jcr:content", props.get(0).targetResource.getPath());
Expand Down

0 comments on commit 0c2995d

Please sign in to comment.