Skip to content

Instantly share code, notes, and snippets.

@chrismrgn
Last active December 16, 2015 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismrgn/2e2b6290997dc2d1fdbc to your computer and use it in GitHub Desktop.
Save chrismrgn/2e2b6290997dc2d1fdbc to your computer and use it in GitHub Desktop.
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