Class AutoPageTranslateServiceImpl
- java.lang.Object
- 
- com.composum.ai.aem.core.impl.autotranslate.AutoPageTranslateServiceImpl
 
- 
- All Implemented Interfaces:
- AutoPageTranslateService
 
 public class AutoPageTranslateServiceImpl extends Object implements AutoPageTranslateService Translated would normally be properties that "obviously" contain text, like jcr:title, jcr:description, text, title etc. (Let's search the wcm core components documentation for that), and properties that heuristically "look like text", that is, contain multiple whitespace sequences. Since that's bound to fail sometimes, we later need a rule configuration mechanism in the OSGI configuration that defines positive / negative exceptions, but that's not in scope for now. We save property values : the property value before the translation is saved with prefix `ai_` and suffix `_original` for the property name, and the property value after the translation is saved with prefix `ai_` and suffix `_translated` for the property name. 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description protected static classAutoPageTranslateServiceImpl.AutoTranslateRuleConfigContentRuleSimple implementation of this for content translation rules.protected static classAutoPageTranslateServiceImpl.PropertyToTranslate- 
Nested classes/interfaces inherited from interface com.composum.ai.aem.core.impl.autotranslate.AutoPageTranslateServiceAutoPageTranslateService.Stats
 
- 
 - 
Field SummaryFields Modifier and Type Field Description protected AutoTranslateConfigServiceautoTranslateConfigServiceprotected com.composum.ai.backend.slingbase.AIConfigurationServiceconfigurationServiceprotected StringDEFAULT_TRANSLATION_RULE_PATTERNprotected com.day.cq.wcm.msm.api.LiveRelationshipManagerliveRelationshipManagerprotected static org.slf4j.LoggerLOGstatic StringMARKER_DEBUG_ADDITIONAL_INSTRUCTIONSIf this is embedded into the additional instructions the translation will be aborted and the additional instructions will be wrapped into an exception which is caught and the instructions printed.protected static PatternPATTERN_IGNORED_SUBNODE_NAMESprotected Map<String,LocalDateTime>runningTranslationsprotected com.composum.ai.backend.base.service.chat.GPTTranslationServicetranslationService
 - 
Constructor SummaryConstructors Constructor Description AutoPageTranslateServiceImpl()
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcancelInheritance(org.apache.sling.api.resource.Resource resource, org.apache.sling.api.resource.Resource resourceToTranslate, AutoPageTranslateServiceImpl.PropertyToTranslate propertyToTranslate)protected voidcleanupObsoleteKeys(AutoPageTranslateServiceImpl.PropertyToTranslate propertyToTranslate, org.apache.sling.api.resource.ModifiableValueMap valueMap, com.day.cq.wcm.msm.api.LiveRelationship liveRelationship, AITranslatePropertyWrapper targetWrapper)The translator and so forth was previously saved on each property - remove that and just save it on the page.protected StringcollectApplicableTranslationRules(String path, List<AutoPageTranslateServiceImpl.PropertyToTranslate> allTranslateableProperties, List<AutoTranslateRuleConfig> rules)protected booleancollectPropertiesToTranslate(org.apache.sling.api.resource.Resource resource, List<AutoPageTranslateServiceImpl.PropertyToTranslate> propertiesToTranslate, AutoPageTranslateService.Stats stats, AutoTranslateService.TranslationParameters translationParameters, boolean force)Searches for properties we have to translate.protected List<AutoTranslateRuleConfig>collectTranslationTables(AutoTranslateCaConfig autoTranslateCaConfig, org.apache.sling.api.resource.Resource resource)protected static PatterncompileContentPattern(String contentMatch)The content match can be a word or phrase that must be present in the content of the page for the rule to match.protected booleanconfigurationOrOverride(boolean defaultvalue, String override, String path)Allows a boolean configuration to be overridden with an optional value from the context-aware configuration.protected com.composum.ai.backend.base.service.chat.GPTConfigurationdetermineConfiguration(org.apache.sling.api.resource.Resource resource, AutoTranslateCaConfig autoTranslateCaConfig, AutoTranslateService.TranslationParameters translationParameters, AutoPageTranslateService.Stats stats)protected static voidexpandSelection(boolean[] includeIndizes, int selectRange)Also include 2 items before those already set, and 2 items after those already set, to have some context.protected Map<String,String>getRawRules(AutoTranslateTranslationTableConfig tableConfig, org.apache.sling.api.resource.Resource resource)Map<String,LocalDateTime>getRunningTranslations()protected booleanisApplicable(AutoTranslateRuleConfig rule, String path, List<AutoPageTranslateServiceImpl.PropertyToTranslate> allTranslateableProperties)Returns whether the rule is applicable.protected static booleanisCancelled(org.apache.sling.api.resource.Resource resource, String key, com.day.cq.wcm.msm.api.LiveRelationship relationship)protected voidmarkAsAiTranslated(org.apache.sling.api.resource.Resource resource, com.day.cq.wcm.msm.api.LiveRelationship liveRelationship, AutoTranslateService.TranslationParameters parameters, com.composum.ai.backend.base.service.chat.GPTConfiguration configuration)protected com.composum.ai.backend.base.service.chat.GPTConfigurationmaybeIncludeAlreadyTranslatedTextAsExample(List<AutoPageTranslateServiceImpl.PropertyToTranslate> propertiesToTranslate, AutoTranslateCaConfig autoTranslateCaConfig, com.composum.ai.backend.base.service.chat.GPTConfiguration configuration)If configured, we include the already translated parts of the page as example.protected booleanmigratePathsToLanguageCopy(org.apache.sling.api.resource.Resource resource, String language, AutoPageTranslateService.Stats stats)Traverses the resource tree looking for paths pointing to /content/dam/ and /content/experience-fragments/ and changes them if there is an unique language copy in our language.protected List<AutoPageTranslateServiceImpl.PropertyToTranslate>reducePropertiesToTranslate(List<AutoPageTranslateServiceImpl.PropertyToTranslate> propertiesToTranslate, AutoTranslateCaConfig autoTranslateCaConfig)Collects the values we need to translate.protected voidreenableInheritance(org.apache.sling.api.resource.Resource resource, String key, com.day.cq.wcm.msm.api.LiveRelationship relationship)protected StringremapPaths(String translatedValue, String blueprintPath, String livecopyPath)We find all href="path" patternsprotected List<String>remapPaths(List<String> translatedValues, String blueprintPath, String livecopyPath)Checks whether there are href="path" in the translatedValues where path is within blueprintPath and replaces those with the according path in the live copy.voidrollback(org.apache.sling.api.resource.Resource resource)Rolls everything back in the resource - mostly for testing purposes.AutoPageTranslateService.StatstranslateLiveCopy(org.apache.sling.api.resource.Resource resource, AutoTranslateService.TranslationParameters translationParameters)Implements the actual translation for one page or asset.
 
- 
- 
- 
Field Detail- 
LOGprotected static final org.slf4j.Logger LOG 
 - 
PATTERN_IGNORED_SUBNODE_NAMESprotected static final Pattern PATTERN_IGNORED_SUBNODE_NAMES 
 - 
MARKER_DEBUG_ADDITIONAL_INSTRUCTIONSpublic static final String MARKER_DEBUG_ADDITIONAL_INSTRUCTIONS If this is embedded into the additional instructions the translation will be aborted and the additional instructions will be wrapped into an exception which is caught and the instructions printed.- See Also:
- Constant Field Values
 
 - 
DEFAULT_TRANSLATION_RULE_PATTERNprotected final String DEFAULT_TRANSLATION_RULE_PATTERN - See Also:
- Constant Field Values
 
 - 
translationServiceprotected com.composum.ai.backend.base.service.chat.GPTTranslationService translationService 
 - 
autoTranslateConfigServiceprotected volatile AutoTranslateConfigService autoTranslateConfigService 
 - 
configurationServiceprotected com.composum.ai.backend.slingbase.AIConfigurationService configurationService 
 - 
liveRelationshipManagerprotected com.day.cq.wcm.msm.api.LiveRelationshipManager liveRelationshipManager 
 - 
runningTranslationsprotected final Map<String,LocalDateTime> runningTranslations 
 
- 
 - 
Method Detail- 
getRunningTranslationspublic Map<String,LocalDateTime> getRunningTranslations() - Specified by:
- getRunningTranslationsin interface- AutoPageTranslateService
 
 - 
translateLiveCopypublic AutoPageTranslateService.Stats translateLiveCopy(@Nonnull org.apache.sling.api.resource.Resource resource, @Nonnull AutoTranslateService.TranslationParameters translationParameters) throws com.day.cq.wcm.api.WCMException, org.apache.sling.api.resource.PersistenceException Description copied from interface:AutoPageTranslateServiceImplements the actual translation for one page or asset.- Specified by:
- translateLiveCopyin interface- AutoPageTranslateService
- Throws:
- com.day.cq.wcm.api.WCMException
- org.apache.sling.api.resource.PersistenceException
 
 - 
cleanupObsoleteKeysprotected void cleanupObsoleteKeys(AutoPageTranslateServiceImpl.PropertyToTranslate propertyToTranslate, org.apache.sling.api.resource.ModifiableValueMap valueMap, com.day.cq.wcm.msm.api.LiveRelationship liveRelationship, AITranslatePropertyWrapper targetWrapper) throws com.day.cq.wcm.api.WCMException The translator and so forth was previously saved on each property - remove that and just save it on the page.- Throws:
- com.day.cq.wcm.api.WCMException
 
 - 
determineConfigurationprotected com.composum.ai.backend.base.service.chat.GPTConfiguration determineConfiguration(@Nonnull org.apache.sling.api.resource.Resource resource, AutoTranslateCaConfig autoTranslateCaConfig, @Nonnull AutoTranslateService.TranslationParameters translationParameters, AutoPageTranslateService.Stats stats) throws com.day.cq.wcm.api.WCMException - Throws:
- com.day.cq.wcm.api.WCMException
 
 - 
reducePropertiesToTranslateprotected List<AutoPageTranslateServiceImpl.PropertyToTranslate> reducePropertiesToTranslate(List<AutoPageTranslateServiceImpl.PropertyToTranslate> propertiesToTranslate, AutoTranslateCaConfig autoTranslateCaConfig) Collects the values we need to translate. If configured, we also insert texts that are already translated since they might guide the translation process.
 - 
expandSelectionprotected static void expandSelection(boolean[] includeIndizes, int selectRange)Also include 2 items before those already set, and 2 items after those already set, to have some context.
 - 
maybeIncludeAlreadyTranslatedTextAsExampleprotected com.composum.ai.backend.base.service.chat.GPTConfiguration maybeIncludeAlreadyTranslatedTextAsExample(List<AutoPageTranslateServiceImpl.PropertyToTranslate> propertiesToTranslate, AutoTranslateCaConfig autoTranslateCaConfig, com.composum.ai.backend.base.service.chat.GPTConfiguration configuration) If configured, we include the already translated parts of the page as example.
 - 
configurationOrOverrideprotected boolean configurationOrOverride(boolean defaultvalue, String override, String path)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.
 - 
remapPathsprotected List<String> remapPaths(List<String> translatedValues, String blueprintPath, String livecopyPath) Checks whether there are href="path" in the translatedValues where path is within blueprintPath and replaces those with the according path in the live copy.
 - 
remapPathsprotected String remapPaths(String translatedValue, String blueprintPath, String livecopyPath) We find all href="path" patterns- See Also:
- remapPaths(List, String, String)
 
 - 
markAsAiTranslatedprotected void markAsAiTranslated(org.apache.sling.api.resource.Resource resource, com.day.cq.wcm.msm.api.LiveRelationship liveRelationship, AutoTranslateService.TranslationParameters parameters, com.composum.ai.backend.base.service.chat.GPTConfiguration configuration) throws com.day.cq.wcm.api.WCMException- Throws:
- com.day.cq.wcm.api.WCMException
 
 - 
migratePathsToLanguageCopyprotected boolean migratePathsToLanguageCopy(org.apache.sling.api.resource.Resource resource, String language, AutoPageTranslateService.Stats stats) throws com.day.cq.wcm.api.WCMExceptionTraverses the resource tree looking for paths pointing to /content/dam/ and /content/experience-fragments/ and changes them if there is an unique language copy in our language.- Returns:
- true if something was changed.
- Throws:
- com.day.cq.wcm.api.WCMException
 
 - 
cancelInheritanceprotected void cancelInheritance(org.apache.sling.api.resource.Resource resource, org.apache.sling.api.resource.Resource resourceToTranslate, AutoPageTranslateServiceImpl.PropertyToTranslate propertyToTranslate) throws com.day.cq.wcm.api.WCMException- Throws:
- com.day.cq.wcm.api.WCMException
 
 - 
reenableInheritanceprotected void reenableInheritance(org.apache.sling.api.resource.Resource resource, String key, com.day.cq.wcm.msm.api.LiveRelationship relationship) throws com.day.cq.wcm.api.WCMException- Throws:
- com.day.cq.wcm.api.WCMException
 
 - 
rollbackpublic void rollback(org.apache.sling.api.resource.Resource resource) throws com.day.cq.wcm.api.WCMException, org.apache.sling.api.resource.PersistenceExceptionDescription copied from interface:AutoPageTranslateServiceRolls everything back in the resource - mostly for testing purposes.- Specified by:
- rollbackin interface- AutoPageTranslateService
- Throws:
- com.day.cq.wcm.api.WCMException
- org.apache.sling.api.resource.PersistenceException
 
 - 
collectPropertiesToTranslateprotected boolean collectPropertiesToTranslate(@Nonnull org.apache.sling.api.resource.Resource resource, @Nonnull List<AutoPageTranslateServiceImpl.PropertyToTranslate> propertiesToTranslate, @Nonnull AutoPageTranslateService.Stats stats, @Nonnull AutoTranslateService.TranslationParameters translationParameters, boolean force) throws com.day.cq.wcm.api.WCMException Searches for properties we have to translate.- Parameters:
- propertiesToTranslate- list to add the properties to translate to - output parameter
- force- all properties have to be retranslated
- Returns:
- true if something was changed already
- Throws:
- com.day.cq.wcm.api.WCMException
 
 - 
isCancelledprotected static boolean isCancelled(org.apache.sling.api.resource.Resource resource, String key, com.day.cq.wcm.msm.api.LiveRelationship relationship)
 - 
collectApplicableTranslationRulesprotected String collectApplicableTranslationRules(String path, List<AutoPageTranslateServiceImpl.PropertyToTranslate> allTranslateableProperties, @Nullable List<AutoTranslateRuleConfig> rules) 
 - 
isApplicableprotected boolean isApplicable(@Nonnull AutoTranslateRuleConfig rule, @Nonnull String path, @Nonnull List<AutoPageTranslateServiceImpl.PropertyToTranslate> allTranslateableProperties) Returns whether the rule is applicable. It is not applicable if there are no translateable properties, anyway, if there is aAutoTranslateRuleConfig.pathRegex()that doesn't match the path, or if there is aAutoTranslateRuleConfig.contentPattern()that doesn't match the content.
 - 
collectTranslationTablesprotected List<AutoTranslateRuleConfig> collectTranslationTables(AutoTranslateCaConfig autoTranslateCaConfig, @Nonnull org.apache.sling.api.resource.Resource resource) 
 - 
getRawRulesprotected Map<String,String> getRawRules(AutoTranslateTranslationTableConfig tableConfig, org.apache.sling.api.resource.Resource resource) throws IOException - Throws:
- IOException
 
 - 
compileContentPattern@Nonnull protected static Pattern compileContentPattern(String contentMatch) The content match can be a word or phrase that must be present in the content of the page for the rule to match. For example, 'Product' will match all pages that contain the word 'Product', case-insensitive. Spaces will also match any whitespace. If it contains any of the regex meta characters []()*+ it'll be treated as a regex.
 
- 
 
-