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
static String
MARKER_DEBUG_ADDITIONAL_INSTRUCTIONS
protected static Pattern
PATTERN_IGNORED_SUBNODE_NAMES
protected Map<String,LocalDateTime>
runningTranslations
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 boolean
configurationOrOverride(boolean defaultvalue, String override, String path)
Allows a boolean configuration to be overridden with an optional value from the context-aware configuration.protected static String
encodePropertyName(String prefix, String propertyName, String suffix)
Searches for propertiesprotected 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.Map<String,LocalDateTime>
getRunningTranslations()
protected boolean
isApplicable(AutoTranslateRuleConfig rule, String path, List<AutoPageTranslateServiceImpl.PropertyToTranslate> allTranslateableProperties)
Returns whether the rule is applicable.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 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.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 List<AutoPageTranslateServiceImpl.PropertyToTranslate>
reducePropertiesToTranslate(List<AutoPageTranslateServiceImpl.PropertyToTranslate> propertiesToTranslate, AutoTranslateCaConfig autoTranslateCaConfig)
Collects the values we need to translate.protected void
reenableInheritance(org.apache.sling.api.resource.Resource resource, String key, com.day.cq.wcm.msm.api.LiveRelationship relationship)
protected String
remapPaths(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.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
-
MARKER_DEBUG_ADDITIONAL_INSTRUCTIONS
public static final String MARKER_DEBUG_ADDITIONAL_INSTRUCTIONS
- See Also:
- Constant Field Values
-
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
-
runningTranslations
protected final Map<String,LocalDateTime> runningTranslations
-
-
Method Detail
-
getRunningTranslations
public Map<String,LocalDateTime> getRunningTranslations()
- Specified by:
getRunningTranslations
in 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: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
-
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 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.
-
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, 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)
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.
-
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.
-
-