Instantly share code, notes, and snippets.
Created
March 17, 2023 15:46
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save KevinJump/2684b0d40cef1bd5c727ca818c6e27ca to your computer and use it in GitHub Desktop.
UmbNav - ValueMapper for Umbraco 8.x - Translation manager v8.7.x
This file contains hidden or 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 System.Linq; | |
using Jumoo.TranslationManager.Core; | |
using Jumoo.TranslationManager.Core.Models; | |
using Jumoo.TranslationManager.Core.ValueMappers; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using Umbraco.Core; | |
using Umbraco.Core.Logging; | |
using Umbraco.Core.Services; | |
namespace TMMultiSetLanguages.App_Code | |
{ | |
public class UmbNavValueMapper : BaseValueMapper, IValueMapper, IPropertyValueMapper | |
{ | |
public UmbNavValueMapper( | |
IContentService contentService, | |
IDataTypeService dataTypeService, | |
IContentTypeService contentTypeService, | |
ILogger logger) : base(contentService, dataTypeService, contentTypeService, logger) | |
{ } | |
public string Name => "AaronSadler.UmbNav"; | |
public override string[] Editors => new[] { "AaronSadler.UmbNav" }; | |
public TranslationValue GetSourceValue(string displayName, string propertyTypeAlias, object value, CultureInfoView culture) | |
=> GetSourceValue(displayName, propertyTypeAlias, "", value, culture); | |
public TranslationValue GetSourceValue(string displayName, string propertyTypeAlias, string path, object value, CultureInfoView culture) | |
{ | |
var array = GetValueAsArray(value); | |
if (array == null || array.Count == 0) return null; | |
var translationValue = new TranslationValue($"{displayName}", propertyTypeAlias, $"{path}/UmbNav"); | |
for (int i = 0; i < array.Count; i++) | |
{ | |
var entry = GetSourceEntry($"{displayName} [{i}]", propertyTypeAlias, path, array[i]); | |
if (entry != null) | |
{ | |
translationValue.InnerValues.Add($"{i}", entry); | |
} | |
} | |
return translationValue; | |
} | |
public TranslationValue GetSourceEntry(string displayName, string propertyTypeAlias, string path, JToken entry) | |
{ | |
if (entry == null) return null; | |
var translationValue = new TranslationValue(displayName, propertyTypeAlias, path); | |
// link name | |
var linkNameValue = new TranslationValue($"{displayName} Link", Constants.PropertyEditors.Aliases.TextBox, path + "/Name") | |
{ | |
Value = entry["name"] == null ? "" : entry.Value<string>("name") | |
}; | |
translationValue.InnerValues.Add("name", linkNameValue); | |
var titleValue = new TranslationValue($"{displayName} Title", Constants.PropertyEditors.Aliases.TextBox, path + "/Title") | |
{ | |
Value = entry["title"] == null ? "" : entry.Value<string>("title") | |
}; | |
translationValue.InnerValues.Add("title", titleValue); | |
if (entry["children"] != null) | |
{ | |
var children = entry.Value<JArray>("children"); | |
for (int c = 0; c < children.Count; c++) | |
{ | |
var childValue = GetSourceEntry($"{displayName} child [{c}]", propertyTypeAlias, path + "/child", children[c]); | |
if (childValue != null) | |
{ | |
translationValue.InnerValues.Add($"Child-{c}", childValue); | |
} | |
} | |
} | |
return translationValue; | |
} | |
public object GetTargetValue(string propertyTypeAlias, object sourceValue, TranslationValue values, CultureInfoView sourceCulture, CultureInfoView targetCulture) | |
{ | |
var array = GetValueAsArray(sourceValue); | |
for (int i = 0; i < array.Count; i++) | |
{ | |
var translatedItem = values.GetInnerValue($"{i}"); | |
if (translatedItem != null) | |
{ | |
var result = GetTargetEntry(propertyTypeAlias, array[i], translatedItem); | |
if (result != null) | |
{ | |
array[i] = result; | |
} | |
} | |
} | |
return JsonConvert.SerializeObject(array); | |
} | |
public JToken GetTargetEntry(string propertyTypeAlias, JToken entry, TranslationValue values) | |
{ | |
var linkValue = values.GetInnerValue("name"); | |
if (linkValue != null) | |
{ | |
entry["name"] = linkValue.Value; | |
} | |
var titleValue = values.GetInnerValue("title"); | |
if (titleValue != null) | |
{ | |
entry["title"] = titleValue.Value; | |
} | |
var children = entry.Value<JArray>("children"); | |
for (int c = 0; c < children.Count; c++) | |
{ | |
var childValue = values.GetInnerValue($"Child-{c}"); | |
if (childValue != null) | |
{ | |
var result = GetTargetEntry(propertyTypeAlias, children[c], childValue); | |
children[c] = result as JObject; | |
} | |
} | |
return entry; | |
} | |
/// <summary> | |
/// Helper to convert the source into a JArray | |
/// </summary> | |
/// <returns>JArray or null if cannot convert.</returns> | |
private JArray GetValueAsArray(object value) | |
{ | |
if (value == null) | |
return null; | |
var attempt = value.TryConvertTo<string>(); | |
if (!attempt.Success) return null; | |
var stringValue = attempt.Result; | |
if (stringValue.IsNullOrWhiteSpace()) | |
return null; | |
try | |
{ | |
var array = JsonConvert.DeserializeObject<JArray>(stringValue); | |
if (array == null || !array.Any()) | |
return null; | |
return array; | |
} | |
catch | |
{ | |
return null; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment