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 class
AutoPageTranslateServiceImpl.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 AutoTranslateConfigService
autoTranslateConfigService
protected com.composum.ai.backend.slingbase.AIConfigurationService
configurationService
protected com.day.cq.wcm.msm.api.LiveRelationshipManager
liveRelationshipManager
protected static org.slf4j.Logger
LOG
protected static Pattern
PATTERN_IGNORED_SUBNODE_NAMES
protected com.composum.ai.backend.base.service.chat.GPTTranslationService
translationService
-
Constructor Summary
Constructors Constructor Description AutoPageTranslateServiceImpl()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
cancelInheritance(org.apache.sling.api.resource.Resource resource, org.apache.sling.api.resource.Resource resourceToTranslate, AutoPageTranslateServiceImpl.PropertyToTranslate propertyToTranslate)
protected boolean
collectPropertiesToTranslate(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 String
collectTranslationRules(String path, List<AutoPageTranslateServiceImpl.PropertyToTranslate> allTranslateableProperties, List<AutoTranslateRuleConfig> rules)
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.protected static String
encodePropertyName(String prefix, String propertyName, String suffix)
Searches for propertiesprotected boolean
isApplicable(AutoTranslateRuleConfig rule, String path, List<AutoPageTranslateServiceImpl.PropertyToTranslate> allTranslateableProperties)
protected static boolean
isCancelled(org.apache.sling.api.resource.Resource resource, String key, com.day.cq.wcm.msm.api.LiveRelationship relationship)
protected 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)
protected boolean
migratePathsToLanguageCopy(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 void
reenableInheritance(org.apache.sling.api.resource.Resource resource, String key, com.day.cq.wcm.msm.api.LiveRelationship relationship)
void
rollback(org.apache.sling.api.resource.Resource resource)
Rolls everything back in the resource - mostly for testing purposes.AutoPageTranslateService.Stats
translateLiveCopy(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
-
translationService
protected com.composum.ai.backend.base.service.chat.GPTTranslationService translationService
-
autoTranslateConfigService
protected volatile AutoTranslateConfigService autoTranslateConfigService
-
configurationService
protected com.composum.ai.backend.slingbase.AIConfigurationService configurationService
-
liveRelationshipManager
protected com.day.cq.wcm.msm.api.LiveRelationshipManager liveRelationshipManager
-
-
Method Detail
-
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:AutoPageTranslateService
Implements the actual translation for one page or asset.- Specified by:
translateLiveCopy
in interfaceAutoPageTranslateService
- Throws:
com.day.cq.wcm.api.WCMException
org.apache.sling.api.resource.PersistenceException
-
markAsAiTranslated
protected 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
-
migratePathsToLanguageCopy
protected boolean migratePathsToLanguageCopy(org.apache.sling.api.resource.Resource resource, String language, AutoPageTranslateService.Stats stats) throws com.day.cq.wcm.api.WCMException
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.- 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.PersistenceException
Description copied from interface:AutoPageTranslateService
Rolls everything back in the resource - mostly for testing purposes.- Specified by:
rollback
in interfaceAutoPageTranslateService
- Throws:
com.day.cq.wcm.api.WCMException
org.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)
-
collectTranslationRules
protected String collectTranslationRules(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)
-
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.
-
-