Sitefinity 4.0: Automate Translations with Custom Workflows and Google Translate

January 21, 2011 Digital Experience

Recently one of our customers asked us for a solution to automatically translate news, blog posts and other content-based items in Sitefinity using Google Translate.

The main idea behind the solution depicted in this article is to automatically create and populate missing language versions using Google Translate once a content-based item is saved as a draft. The content editors can thereafter review and improve the newly generated translations.

Custom workflows provide the means to modify and extend the processes behind the lifecycle of content-based items (as well as pages, media items, etc.) with user-specific logic, which perfectly suits this task.

NOTE:

  • The following sample demonstrates the implementation of user-specific scenarios using custom workflows in Sitefinity 4.0 – a Professional or Enterprise license is required to use this sample.
  • Workflow (.xamlx) files are updated frequently; make sure to keep your modified custom workflow file up-to-date.
  • You will need the Telerik.Samples.GoogleTranslateWorkflow project files which you can find here.
  • You can get the latest default workflow files from your client account.


The following section offers step-by-step instructions on how to extend the workflow file; if you want to skip it you can use the CompleteGoogleTranslateWorkflow.xamlx file from the archive instead.

Create the .xaml workflow file

  1. Copy the unmodified AnyContentApprovalWorkflow.xamlx workflow file to the Workflows directory inside your Sitefinity project folder.
  2. Open the Sitefinity project file in Visual Studio.
  3. Add a reference to the Telerik.Samples.GoogleTranslateWorkflow project.
  4. Build the solution.
  5. Open the AnyContentApprovalWorkflow.xamlx file inside Visual Studio and locate the ContentApproval Flowchart.
  6. Double click the ContentApproval Flowchart and locate the ClearScheduledOperationsActivity.
  7. Create a new Flowchart and name it “Google Translate Flowchart”.
  8. Set the “Google Translate Flowchart” as the last step by dragging an arrow from ClearScheduledOperationsActivity and dropping it on the newly created Flowchart.
  9. Double click the “Google Translate Flowchart”.
  10. Open the Variables tab and create a new variable by clicking the bottom table row.
  11. Name the variable translateItem and set its type to IAnyTempDraftFacade
  12. Inside the Default field paste: App.Prepare().SetContentProvider(providerName).WorkWith().AnyContentItem(contextBag("ContentType"), itemId).GetTemp()
  13. To create custom translations when the item is saved as a draft add a FlowDecision element from the toolbox.
  14. Set operationName.Equals("SaveDraft") as the condition.
  15. Add a ForEach element to the Flowchart.
  16. Set it as the step which is executed if the FlowDecision returns true by dragging an arrow from the FlowDecision element.
  17. Set System.Globalization.CultureInfo as the TypeArgument
  18. Set New Telerik.Sitefinity.Abstractions.AppSettings().DefinedFrontendLanguages as Values.
  19. Double click the newly added ForEach element.
  20. Add the GoogleTranslateActivity from the toolbox.
    NOTE: If you cannot find the GoogleTranslateActivity in your toolbox make sure that you executed steps 3&4.
  21. Select the GoogleTranslateActivity element and set the following parameters
    1. OnbehalfOfIp as: contextBag("userHostAddress")
    2. TranslateTo as: item
    3. WorkflowItem as: translateItem.GetAs(Of IWorkflowItem)()
  22. In the breadcrumb on top click “Google Translate Flowchart” to get back to the “Google Translate Flowchart”.
  23. From the toolbox add an ExecuteCodeActivity and set its CodeExecutionResult property to translateItem.SaveChanges()
  24. Save the workflow file.
  25. Build the solution.

The Google Translate Flowchart in its complete state.

Register the workflow

The only thing left to do is to register the custom workflow:

In the backend, go to Administration > Settings > Advanced > Workflows.


The Settings view lists workflow associations by model type.

 

Change the workflow ServiceUrl to point at the GoogleTranslateAnyContentApproval.xamlx workflow, for each model you want to apply automatic translation to.

Final comments

The whole procedure described above could have been implemented entirely inside the GoogleTranslateActivity. However, having only the actual translation logic encapsulated as an activity allows it to be reused in completely different scenarios.

This is where the power of Sitefinity’s workflows comes into play, which allows you to model your own unique business processes using convenient enterprise-grade WF4 technology.

The Progress Team