Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SDL Tridion Translation Manager plugin that creates a secondary job for a pages components, in the correct context publication
[TranslationManagerPlugIn]
class AddComponentsTranslationManagerPlugin
{
public AddComponentsTranslationManagerPlugin()
{
//Subscribe to Translation Manager events
TranslationJobManager.TranslationJobCreated += JobCreated;
TranslationJobManager.TranslationJobLoaded += JobCreated;
}
private void JobCreated(object sender, TranslationJobEventArgs e)
{
//Subscribe to job saving event
TranslationJob job = e.TranslationJob;
job.Saving += Job_Saving;
}
private void Job_Saving(object sender, EventArgs e)
{
var job = (TranslationJob)sender;
//foreach item in the job (handling only Page for now)
foreach (var item in job.AddedItems)
{
try
{
//initialize CoreService client
SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2013");
client.Impersonate("NT AUTHORITY\\SYSTEM");
//read current item via CoreService
var tridionItem = client.Read(item.TcmUri.ToString(), new ReadOptions { LoadFlags = LoadFlags.Expanded });
//if item is a Page
if (tridionItem is PageData)
{
//if "translate components" check box is sele
if (item.TranslationOptions == TranslationOptions.TranslateSubItems)
{
CreateTranslationJobWithPageComponents(job.Title, (PageData)tridionItem);
}
}
else //item is StructureGroup, Bundle, Component etc...
{
//considered out of scope
}
}
catch (Exception ex)
{
//refactor to add logging
job.Title = "ERROR: " + job.Title + " " + ex.Message;
}
//Delete original Page job, as in this case I do not want to Translate the Page at all
job.Delete();
}
}
private static void CreateTranslationJobWithPageComponents(string title, PageData page)
{
var manager = new TranslationJobManager("NT AUTHORITY\\SYSTEM");
//Additional code required here, if more complex translation business rules
//TODO: handle repeated saving, and do not create duplicate jobs
var componentJob = manager.CreateJob(title + " - Components", "COMPONENT PUBLICATION TCMID", TranslationJobType.PullJob);
TranslationConfiguration configuration = manager.GetTranslationConfiguration(new Tridion.TranslationManager.DomainModel.Api.TcmUri("COMPONENT PUBLICATION TCMID"));
foreach (TranslationConfiguration targetconfiguration in configuration.TargetConfigurations)
{
componentJob.TargetPublicationUris.Add(targetconfiguration.ConfiguredItemUri);
}
foreach (var cp in page.ComponentPresentations)
{
AddedItem newItem = new AddedItem(cp.Component.IdRef, TranslationOptions.TranslateSubItems);
componentJob.AddedItems.Add(newItem);
}
componentJob.Save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment