Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Created May 6, 2020 08:40
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 KevinJump/825147da8eb3e634e003c1ca9788aa74 to your computer and use it in GitHub Desktop.
Save KevinJump/825147da8eb3e634e003c1ca9788aa74 to your computer and use it in GitHub Desktop.
TranslationManager : Copy the NodeName on TranslationJob Approval
using System;
using Jumoo.TranslationManager.Core.Services;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Services;
namespace Jumoo.Translation.ExampleCode
{
//
// Example Code : CopyNodeName on Approval
//
// This code shows how to hook into the TranslationManager Events
// to copy the node name value between content when a node has
// been approved
//
public class CopyNodeNameComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Components().Append<CopyNodeNameComponent>();
}
}
public class CopyNodeNameComponent : IComponent
{
public readonly IContentService contentService;
public void Initialize()
{
TranslationJobService.PartialApproved += CopyNodeNameOnApproval;
}
/// <summary>
/// Fired whenever a job is approved by a user
/// </summary>
/// <remarks>
/// This even will be fired once the translated text has been
/// saved back into umbraco, so you can connect to the ContentItems
/// and they will be in their translated state.
/// </remarks>
private void CopyNodeNameOnApproval(TranslationPartialEventArgs e)
{
foreach(var node in e.Nodes)
{
var master = contentService.GetById(node.MasterNodeId);
if (node.MasterNodeId == node.TargetNodeId)
{
// same master and node, so we are copying the node name from one language
// to another in the same bit of cotent.
var nodeName = master.GetCultureName(e.Job.SourceCulture.Name);
master.SetCultureName(nodeName, e.Job.TargetCulture.Name);
if (e.Publish)
{
contentService.SaveAndPublish(master, culture: e.Job.TargetCulture.Name);
}
else
{
contentService.Save(master);
}
}
else
{
// the master and target are diffrent nodes, so copy between
var target = contentService.GetById(node.TargetNodeId);
target.Name = master.Name;
if (e.Publish) // the job was an approve and publish
{
contentService.SaveAndPublish(target);
}
else
{
contentService.Save(target);
}
}
}
}
public void Terminate()
{
// No Terminate action
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment