Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Last active August 22, 2018 19:14
Show Gist options
  • Save KevinJump/38cb7b65122d6bf2d8ed4ab6e9900035 to your computer and use it in GitHub Desktop.
Save KevinJump/38cb7b65122d6bf2d8ed4ab6e9900035 to your computer and use it in GitHub Desktop.
Umbraco Translation Manager - remove everything but vorto elements from a translation job, just before it is submitted (created)
/*
n.b this code isn't really tested - but in principle will work - it may need tweaking around
the edges.
At the moment there are no TranslationNode creation events - so you have to attach this to the
job submitting event which happens just before a job is created - in practice if you are
creating jobs directly from the send to translate dialog you won't notice the diffrence.
*/
//
// within something like ApplicationStarted attache to the submitting event, this is fired just before a job is submitted
//
// TranslationJobService.Submitting += TranslationJobService_Submitting;
//
private void TranslationJobService_Submitting(TranslationEventArgs e)
{
foreach (var node in e.Job.Nodes)
{
var tabs = new List<TranslationPropertyGroup>();
foreach (var tab in node.Groups)
{
var newTab = tab;
var properties = new List<TranslationProperty>();
foreach (var property in tab.Properties)
{
var sourceValues = GetVorto(property.Source);
var targetValues = GetVorto(property.Target);
if (sourceValues != null)
{
property.Source = sourceValues;
property.Target = targetValues;
properties.Add(property);
TranslationManagerContext.Current.NodeService.SaveProperty(property);
}
else
{
// this is dirty the save below should really update the properties
// but it doesn't remove the ones that have been removed here.
TranslationManagerContext.Current.NodeService.DeleteProperty(property.Id);
}
}
if (properties.Count > 0)
{
tab.Properties = properties;
tabs.Add(tab);
}
}
if (tabs.Count > 0)
{
node.Groups = tabs;
}
}
}
//
// recurse into a translation value and remove anything that isn't a vorto
// this will allow you to have vorto values inside other things.
//
private TranslationValue GetVorto(TranslationValue value)
{
if (value.EditorAlias.InvariantEquals("Our.Umbraco.Vorto")) return value;
if (value.HasChildValues())
{
var newInner = new Dictionary<string, TranslationValue>();
foreach (var innerValue in value.InnerValues)
{
var inner = GetVorto(innerValue.Value);
if (inner != null)
newInner.Add(innerValue.Key, inner);
}
if (newInner.Any())
{
value.InnerValues = newInner;
return value;
}
return null;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment