diff --git a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AITranslatePropertyWrapper.java b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AITranslatePropertyWrapper.java index 681721fe3..ef9a9ec72 100644 --- a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AITranslatePropertyWrapper.java +++ b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AITranslatePropertyWrapper.java @@ -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] . @@ -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), diff --git a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoPageTranslateServiceImpl.java b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoPageTranslateServiceImpl.java index 74ed495c7..a6203789a 100644 --- a/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoPageTranslateServiceImpl.java +++ b/aem/core/src/main/java/com/composum/ai/aem/core/impl/autotranslate/AutoPageTranslateServiceImpl.java @@ -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 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() @@ -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); @@ -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 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) { @@ -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 @@ -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; } } diff --git a/aem/core/src/test/java/com/composum/ai/aem/core/impl/autotranslate/AutoPageTranslateServiceImplTest.java b/aem/core/src/test/java/com/composum/ai/aem/core/impl/autotranslate/AutoPageTranslateServiceImplTest.java index 93f1eba12..e4426b44b 100644 --- a/aem/core/src/test/java/com/composum/ai/aem/core/impl/autotranslate/AutoPageTranslateServiceImplTest.java +++ b/aem/core/src/test/java/com/composum/ai/aem/core/impl/autotranslate/AutoPageTranslateServiceImplTest.java @@ -74,7 +74,7 @@ public void testCollectPropertiesToTranslate() throws WCMException { List 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());