Created
October 20, 2022 12:01
-
-
Save KevinJump/e983c8a4fa0b4377aa7ab357a47c42c2 to your computer and use it in GitHub Desktop.
Translation Manager v8 - Copy the node name between source and target languages when a translation is approaved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Jumoo.TranslationManager.Core.Services; | |
using Umbraco.Core; | |
using Umbraco.Core.Composing; | |
using Umbraco.Core.Services; | |
namespace 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 PublishCheckComposer : IUserComposer | |
{ | |
public void Compose(Composition composition) | |
{ | |
composition.Components().Append<TranslationApprovalComponent>(); | |
} | |
} | |
public class TranslationApprovalComponent : IComponent | |
{ | |
public readonly IContentService contentService; | |
public TranslationApprovalComponent(IContentService contentService) | |
{ | |
this.contentService = contentService; | |
} | |
public void Initialize() | |
{ | |
TranslationNodeService.Approved += TranslationNodeService_Approved; | |
} | |
/// <summary> | |
/// fired when a node is approved. this happens. after all the translated content | |
/// has been moved into the target node but before any publish events are fired | |
/// </summary> | |
/// <param name="e"></param> | |
private void TranslationNodeService_Approved(TranslationNodeContentEventArgs e) | |
{ | |
var targetContentItem = e.Target; | |
var translationNode = e.Node; | |
// this code only works on loop back (e.g variants) sets | |
if (e.Node.MasterNodeId != e.Node.TargetNodeId || !targetContentItem.ContentType.VariesByCulture()) return; | |
// optional, only set the node name if the culture doesn't have this set.? | |
// var targetNodeName = targetContentItem.GetCultureName(e.Node.Culture.Name); | |
// if (!string.IsNullOrWhiteSpace(targetNodeName)) return; | |
var sourceNode = contentService.GetById(translationNode.MasterNodeId); | |
targetContentItem.SetCultureName(sourceNode.Name, e.Node.Culture.Name); | |
} | |
public void Terminate() | |
{ | |
// teardown - nothing to do in v8 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment