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 Summary
Nested 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.AutoPageTranslateService
AutoPageTranslateService.Stats 
 - 
 
- 
Field Summary
Fields Modifier and Type Field Description protected AutoTranslateConfigServiceautoTranslateConfigServiceprotected 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 GPTTranslationServicetranslationService 
- 
Constructor Summary
Constructors Constructor Description AutoPageTranslateServiceImpl() 
- 
Method Summary
All 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 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, GPTConfiguration configuration)protected GPTConfigurationmaybeIncludeAlreadyTranslatedTextAsExample(List<AutoPageTranslateServiceImpl.PropertyToTranslate> propertiesToTranslate, AutoTranslateCaConfig autoTranslateCaConfig, 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
- 
LOG
protected static final org.slf4j.Logger LOG
 
- 
PATTERN_IGNORED_SUBNODE_NAMES
protected static final Pattern PATTERN_IGNORED_SUBNODE_NAMES
 
- 
MARKER_DEBUG_ADDITIONAL_INSTRUCTIONS
public 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_PATTERN
protected final String DEFAULT_TRANSLATION_RULE_PATTERN
- See Also:
 - Constant Field Values
 
 
- 
translationService
protected GPTTranslationService translationService
 
- 
autoTranslateConfigService
protected volatile AutoTranslateConfigService autoTranslateConfigService
 
- 
configurationService
protected AIConfigurationService configurationService
 
- 
liveRelationshipManager
protected com.day.cq.wcm.msm.api.LiveRelationshipManager liveRelationshipManager
 
- 
runningTranslations
protected final Map<String,LocalDateTime> runningTranslations
 
 - 
 
- 
Method Detail
- 
getRunningTranslations
public Map<String,LocalDateTime> getRunningTranslations()
- Specified by:
 getRunningTranslationsin interfaceAutoPageTranslateService
 
- 
translateLiveCopy
public 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 interfaceAutoPageTranslateService- Throws:
 com.day.cq.wcm.api.WCMExceptionorg.apache.sling.api.resource.PersistenceException
 
- 
cleanupObsoleteKeys
protected 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
 
- 
determineConfiguration
protected 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
 
- 
reducePropertiesToTranslate
protected 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. 
- 
expandSelection
protected 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. 
- 
maybeIncludeAlreadyTranslatedTextAsExample
protected GPTConfiguration maybeIncludeAlreadyTranslatedTextAsExample(List<AutoPageTranslateServiceImpl.PropertyToTranslate> propertiesToTranslate, AutoTranslateCaConfig autoTranslateCaConfig, GPTConfiguration configuration)
If configured, we include the already translated parts of the page as example. 
- 
configurationOrOverride
protected 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. 
- 
remapPaths
protected 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. 
- 
remapPaths
protected String remapPaths(String translatedValue, String blueprintPath, String livecopyPath)
We find all href="path" patterns- See Also:
 remapPaths(List, String, String)
 
- 
markAsAiTranslated
protected void markAsAiTranslated(org.apache.sling.api.resource.Resource resource, com.day.cq.wcm.msm.api.LiveRelationship liveRelationship, AutoTranslateService.TranslationParameters parameters, GPTConfiguration configuration) throws com.day.cq.wcm.api.WCMException- Throws:
 com.day.cq.wcm.api.WCMException
 
- 
migratePathsToLanguageCopy
protected 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
 
- 
cancelInheritance
protected 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
 
- 
reenableInheritance
protected 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
 
- 
rollback
public 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 interfaceAutoPageTranslateService- Throws:
 com.day.cq.wcm.api.WCMExceptionorg.apache.sling.api.resource.PersistenceException
 
- 
collectPropertiesToTranslate
protected 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 parameterforce- all properties have to be retranslated- Returns:
 - true if something was changed already
 - Throws:
 com.day.cq.wcm.api.WCMException
 
- 
isCancelled
protected static boolean isCancelled(org.apache.sling.api.resource.Resource resource, String key, com.day.cq.wcm.msm.api.LiveRelationship relationship) 
- 
collectApplicableTranslationRules
protected String collectApplicableTranslationRules(String path, List<AutoPageTranslateServiceImpl.PropertyToTranslate> allTranslateableProperties, @Nullable List<AutoTranslateRuleConfig> rules)
 
- 
isApplicable
protected 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. 
- 
collectTranslationTables
protected List<AutoTranslateRuleConfig> collectTranslationTables(AutoTranslateCaConfig autoTranslateCaConfig, @Nonnull org.apache.sling.api.resource.Resource resource)
 
- 
getRawRules
protected 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. 
 - 
 
 -