Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jesse Andrews 191 posts 716 karma points c-trib
    Jun 27, 2019 @ 00:41
    Jesse Andrews
    0

    Are there any existing helpers that aid with serialization/deserialization of translation jobs?

    I'm not stuck on this, but I was wondering whether there are any existing helpers that can make my current IJobSerializer implementation cleaner. Below is the code I'm currently using.

    public class StrakerSerializer : IJobSerializer<XmlDocument> {
        public string Name => "Straker Serializer";
    
        public string Format => "xml";
        private ILogger _logger;
    
        public StrakerSerializer(ILogger logger) {
            _logger = logger;
        }
    
        public TranslationAttempt<TranslationJob> Deserialize(XmlDocument source, TranslationJob job, bool valuesInSource, bool matchLanguages) {
            var entries = new Dictionary<string, string>();
            foreach (XmlElement node in source.SelectNodes("data")) {
                entries.Add(node.GetAttribute("name"), node.SelectSingleNode("value").InnerXml);
            }
            foreach (var node in job.Nodes) {
                node.Status = NodeStatus.Reviewing;
                foreach (var group in node.Groups) {
                    foreach (var property in group.Properties) {
                        var key = property.NodeKey.ToString();
                        if (entries.ContainsKey(key)) {
                            property.Target.Value = entries[key];
                        }
                    }
    
                }
            }
            return TranslationAttempt<TranslationJob>.Succeed(job);
        }
    
        public TranslationAttempt<XmlDocument> Serialize(TranslationJob job, string sourceLang, string targetLang, bool splitHtml) {
            var doc = new XmlDocument();
            var xmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(xmlDeclaration);
            var root = doc.CreateElement("root");
            doc.AppendChild(root);
            try {
                foreach (var node in job.Nodes) {
                    foreach (var group in node.Groups) {
                        foreach (var property in group.Properties) {
                            var item = doc.CreateElement("data");
                            if (!string.IsNullOrEmpty(property.Source.Value)) {
                                item.SetAttribute("name", property.NodeKey.ToString());
                                item.InnerXml = "<value>" + property.Source.Value + "</value>";
                                root.AppendChild(item);
                            }
                            if (property.Source.InnerValues.Count > 0) {
                                foreach (var pair in property.Source.InnerValues) {
                                    GetValue(pair.Value, doc, root);
                                }
                            }
                        }
                    }
                }
    
                return TranslationAttempt<XmlDocument>.Succeed(doc);
            }
            catch (Exception e) {
                return TranslationAttempt<XmlDocument>.Fail($"Failed to serialize job {job.Id} for straker.", e);
            }
        }
    
        private void GetValue(TranslationValue value, XmlDocument doc, XmlElement root) {
            if (!string.IsNullOrEmpty(value.Value)) {
                var item = doc.CreateElement("data");
                item.SetAttribute("name", value.DisplayName.ToString());
                item.InnerXml = "<value>" + value.Value + "</value>";
                root.AppendChild(item);
            }
            if (value.InnerValues.Count > 0) {
                foreach (var pair in value.InnerValues) {
                    GetValue(pair.Value, doc, root);
                }
            }
        }
    }
    

    It's still a work in progress, as I'm pretty sure this doesn't ensure unique names when serialized (see structure I'm converting to here). I may just end up using the Xliff Serializer and chop up what it returns to get what I need, but I wanted to check whether there is a cleaner way to handle this.

    Also, where is deserialization supposed to be triggered?

  • Kevin Jump 2309 posts 14673 karma points MVP 7x c-trib
    Jun 27, 2019 @ 09:00
    Kevin Jump
    100

    Hi

    We don't have any specific helpers for just XML, there are things to serialise in and out of XLIFF formats.

    And they look similar to what you have (with a bit of XLIFF library stuff thrown in).

    The tricky bit is the recursion as each value can have child values, so you need to make sure you do that both in and out (looks like you have missed this at the Deserialize end?) and when setting the low-level target value, you should also set the Translated boolean value to True as this is used in the checks (as noted in another response).

  • Jesse Andrews 191 posts 716 karma points c-trib
    Jun 28, 2019 @ 00:34
    Jesse Andrews
    0

    Yeah I hadn't updated up the deserialize method yet to match the recursion in the serialization, as I didn't have the logic that triggers deserialization wired up yesterday. I was able to get this working today.

Please Sign in or register to post replies

Write your reply to:

Draft