Created
January 4, 2018 11:36
-
-
Save KevinJump/a7754bb20f93b1e9934a8cbfb1bb9203 to your computer and use it in GitHub Desktop.
Value Mapper for Custom JSON snippets inside Umbraco Grid Values - used by Translation Manager to import / export the text values
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 System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Jumoo.TranslatePlus.Core.Models; | |
using Jumoo.TranslatePlus.Core.Providers; | |
using Jumoo.TranslatePlus.Core.ValueMappers; | |
using Newtonsoft.Json; | |
using Umbraco.Core; | |
using Umbraco.Core.Logging; | |
namespace Jumoo.TranslatePlus.Core.ValueMappers | |
{ | |
public class CustomGridJsonValueMapper : BaseValueMapper, IValueMapper | |
{ | |
public string Name => "Custom Grid Json Value Mapper"; | |
public override string[] Editors | |
{ | |
get | |
{ | |
return Configs.Select(x => x.EdtiorAlias).ToArray(); | |
} | |
} | |
public List<CustomGridConfig> Configs { get; set; } | |
public CustomGridJsonValueMapper() | |
{ | |
LoadConfigs(); | |
} | |
public TranslationValue GetSourceValue(string displayName, string propertyTypeAlias, object value, CultureInfoView culture) | |
{ | |
_logger.Debug<CustomGridJsonValueMapper>("{0}: {1}", () => propertyTypeAlias, () => value); | |
var attempt = value.TryConvertTo<string>(); | |
if (!attempt) return null; | |
var config = Configs.FirstOrDefault(x => x.EdtiorAlias.InvariantEquals(propertyTypeAlias)); | |
var jsonDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(attempt.Result); | |
var translationValue = new TranslationValue(displayName, propertyTypeAlias); | |
foreach(var property in config.Properties) | |
{ | |
if (jsonDictionary.ContainsKey(property.Key)) | |
{ | |
var itemDisplayName = $"{displayName} {property.Key}"; | |
var innerValue = ValueMapperFactory.GetMapperSource(property.Value, itemDisplayName, jsonDictionary[property.Key], culture); | |
if (innerValue != null) | |
translationValue.InnerValues.Add(property.Key, innerValue); | |
} | |
} | |
return translationValue; | |
} | |
public object GetTargetValue(string propertyTypeAlias, object sourceValue, TranslationValue values, CultureInfoView sourceCulture, CultureInfoView targetCulture) | |
{ | |
_logger.Debug<CustomGridJsonValueMapper>("{0}: {1}", () => propertyTypeAlias, () => sourceValue); | |
var attempt = sourceValue.TryConvertTo<string>(); | |
if (!attempt) return sourceValue; | |
var config = Configs.FirstOrDefault(x => x.EdtiorAlias.InvariantEquals(propertyTypeAlias)); | |
if (values.HasChildValues()) | |
{ | |
var jsonDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(attempt.Result); | |
foreach(var innerValue in values.InnerValues) | |
{ | |
var innerEditorAlias = innerValue.Value.EditorAlias; | |
var innerSource = ""; | |
if (jsonDictionary.ContainsKey(innerValue.Key)) | |
{ | |
innerSource = jsonDictionary[innerValue.Key]; | |
} | |
var value = (string)ValueMapperFactory.GetMapperTarget(innerEditorAlias, innerSource, innerValue.Value, sourceCulture, targetCulture); | |
if (jsonDictionary.ContainsKey(innerValue.Key)) | |
{ | |
jsonDictionary[innerValue.Key] = value; | |
} | |
else | |
{ | |
jsonDictionary.Add(innerValue.Key, value); | |
} | |
} | |
_logger.Debug<MacroValueMapper>("Mapped: {0}", () => JsonConvert.SerializeObject(jsonDictionary, Formatting.Indented)); | |
return JsonConvert.SerializeObject(jsonDictionary, Formatting.Indented); | |
} | |
return sourceValue; | |
} | |
private IEnumerable<CustomGridConfig> LoadConfigs() | |
{ | |
LogHelper.Debug<CustomGridConfig>("Loading Custom Grid Config Mappers"); | |
Configs = new List<CustomGridConfig>(); | |
var customConfigs = TranslateConfigHelper.GetCoreSection("mappers/grid/custom"); | |
if (customConfigs != null && customConfigs.HasElements) | |
{ | |
foreach (var configNode in customConfigs.Elements("config")) | |
{ | |
if (configNode.Attribute("alias") == null) continue; | |
if (configNode.Element("properties") == null) continue; | |
var customConfig = new CustomGridConfig | |
{ | |
EdtiorAlias = $"Umbraco.Grid.{configNode.Attribute("alias").Value}" | |
}; | |
LogHelper.Debug<CustomGridConfig>("Custom Grid: {0}", ()=> customConfig.EdtiorAlias); | |
var propertiesElement = configNode.Element("properties"); | |
foreach (var property in propertiesElement.Elements("property")) | |
{ | |
if (property.Attribute("name") != null && property.Attribute("alias") != null) | |
{ | |
var name = property.Attribute("name").Value; | |
var alias = property.Attribute("alias").Value; | |
customConfig.Properties.Add(name, alias); | |
LogHelper.Debug<CustomGridConfig>("Editor Property: {0} {1}", () => name, () => alias); | |
} | |
} | |
Configs.Add(customConfig); | |
} | |
} | |
return Configs; | |
} | |
} | |
public class CustomGridConfig | |
{ | |
public CustomGridConfig() | |
{ | |
Properties = new Dictionary<string, string>(); | |
} | |
public string EdtiorAlias { get; set; } | |
public Dictionary<string, string> Properties { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment