Skip to main content

Building a custom translation completion action

Introduction

The TranslationCompletionAction abstract class provides a flexible solution for executing custom logic upon completion of translations within a content management system. Brightspot offers built-in concrete extensions such as Draft, Publish, and Workflow, and other extensions can be deployed to meet custom requirements.

Step 1: Extend the TranslationCompletionAction class

To implement a custom translation completion action, you need to extend the TranslationCompletionAction class. This abstract class provides the basic structure for creating custom completion actions.

@Recordable.DisplayName("Publish with Translation Author")
public class TranslationAuthorCompletionAction extends TranslationCompletionAction {

}
  • 1. Since TranslationCompletionAction extends Record you can use Brightspot data modeling annotations on this class. This line changes the display name of the completion action in the tool UI.

Step 2: Add configuration fields

You can add class-level fields to your extension of TranslationCompletionAction, which will be presented to admins during the configuration of the completion action setup. A real world example is the out of the box WorkflowCompletionAction. This class allows admins to configure which exact Workflow status is applied to an asset when it is returned from translation.

@Recordable.DisplayName("Publish with Translation Author")
public class TranslationAuthorCompletionAction extends TranslationCompletionAction {

@Note("Author selected here will be set as the content author for all translations")
private Author author;

}

Translation Completion Action

Step 3: Implement the completeTranslation method

The key method that needs to be implemented when extending the TranslationCompletionAction class is completeTranslation. This method takes two parameters: Recordable newContent and TranslationLog log. The newContent object represents the translated content, whereas the log object encapsulates information about the translation process, such as the user who performed the translation, the translation service used, and relevant metadata.

Your implementation of the completeTranslation method is responsible for saving the completed translation to the database. However, it is not responsible for saving the TranslationLog object.

To generate a new Draft object (in the CMS, this is referred to as a Revision), you can use the createDraft helper method provided by the TranslationCompletionAction class. createDraft requires a TranslationLog object, a Record object, and two State objects that represent the state of the content before and after translation.

Below is an example implementation of a TranslationCompletionAction in its entirety. It ensures that all translations have an admin-specified author applied as the content author.

@Recordable.DisplayName("Publish with Translation Author")
public class TranslationAuthorCompletionAction extends TranslationCompletionAction {

@Note("Author selected here will be set as the content author for all translations")
private Author author;

@Override
protected void completeTranslation(Recordable newContent, TranslationLog log) {
newContent.as(HasAuthorData.class).setAuthor(author);
Content.Static.publish(newContent, log.getSite(), newContent.as(Content.ObjectModification.class).getPublishUser());
}
}