Skip to content

Commit

Permalink
make ca config override global config
Browse files Browse the repository at this point in the history
  • Loading branch information
stoerr committed Oct 5, 2024
1 parent e8a4e04 commit 9722767
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,9 @@ public Stats translateLiveCopy(@Nonnull Resource resource,
* If configured, we also insert texts that are already translated since they might guide the translation process.
*/
protected List<PropertyToTranslate> reducePropertiesToTranslate(List<PropertyToTranslate> propertiesToTranslate, AutoTranslateCaConfig autoTranslateCaConfig) {
boolean includeFullPageInRetranslation = autoTranslateConfigService.includeFullPageInRetranslation()
|| trueTristateCaConfig(autoTranslateCaConfig.includeFullPageInRetranslation());
boolean includeFullPageInRetranslation = configurationOrOverride(
autoTranslateConfigService.includeFullPageInRetranslation(),
autoTranslateCaConfig.includeFullPageInRetranslation());
boolean[] includeIndizes = new boolean[propertiesToTranslate.size()];
for (int i = 0; i < propertiesToTranslate.size(); i++) {
includeIndizes[i] = includeFullPageInRetranslation || !propertiesToTranslate.get(i).isAlreadyCorrectlyTranslated;
Expand Down Expand Up @@ -284,8 +285,8 @@ protected GPTConfiguration maybeIncludeAlreadyTranslatedTextAsExample(
List<PropertyToTranslate> propertiesToTranslate,
AutoTranslateCaConfig autoTranslateCaConfig, GPTConfiguration configuration) {
boolean includeExistingTranslationsInRetranslation =
autoTranslateConfigService.includeExistingTranslationsInRetranslation() ||
trueTristateCaConfig(autoTranslateCaConfig.includeExistingTranslationsInRetranslation());
configurationOrOverride(autoTranslateConfigService.includeExistingTranslationsInRetranslation(),
autoTranslateCaConfig.includeExistingTranslationsInRetranslation());

String alreadyTranslatedText = propertiesToTranslate.stream()
.filter(p -> p.isAlreadyCorrectlyTranslated)
Expand All @@ -305,10 +306,13 @@ protected GPTConfiguration maybeIncludeAlreadyTranslatedTextAsExample(
}

/**
* Is counted as true if there is a true value in the array.
* Allows a boolean configuration to be overridden with an optional value from the context-aware configuration. If the override array has several values we just take the first one.
*/
protected boolean trueTristateCaConfig(boolean[] value) {
return value != null && Arrays.asList(value).contains(true);
protected boolean configurationOrOverride(boolean defaultvalue, boolean[] override) {
if (override != null && override.length > 0) {
return override[0];
}
return defaultvalue;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import static com.composum.ai.aem.core.impl.autotranslate.AutoPageTranslateServiceImpl.compileContentPattern;
import static junit.framework.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -210,5 +211,25 @@ public void expandSelection_long() {
assertArrayEquals(new boolean[]{false, true, true, true, true, true, true, true, false}, includeIndizes);
}

@Test
public void testConfigurationOrOverride() {
AutoPageTranslateServiceImpl service = new AutoPageTranslateServiceImpl();

// Test when override is null
assertTrue(service.configurationOrOverride(true, null));
assertFalse(service.configurationOrOverride(false, null));

// Test when override is empty
assertTrue(service.configurationOrOverride(true, new boolean[]{}));
assertFalse(service.configurationOrOverride(false, new boolean[]{}));

// Test when override has one element
assertTrue(service.configurationOrOverride(false, new boolean[]{true}));
assertFalse(service.configurationOrOverride(true, new boolean[]{false}));

// Test when override has multiple elements: we take the first.
assertTrue(service.configurationOrOverride(false, new boolean[]{true, false}));
assertFalse(service.configurationOrOverride(true, new boolean[]{false, true}));
}

}

0 comments on commit 9722767

Please sign in to comment.