private Dictionary <string, MtTag> GetSourceTagsDict()
        {
            dict = new Dictionary <string, MtTag>(); //try this
            //build dict
            for (var i = 0; i < sourceSegment.Elements.Count; i++)
            {
                var elType = sourceSegment.Elements[i].GetType();

                if (elType.ToString() == "Sdl.LanguagePlatform.Core.Tag") //if tag, add to dictionary
                {
                    var theTag    = new MtTag((Tag)sourceSegment.Elements[i].Duplicate());
                    var tagNumber = sourceSegment.Elements.IndexOf(theTag.SdlTag); //this is a number we will assign the tag
                    var tagText   = "<tg" + tagNumber + ">";                       //create our abbreviated tag to send to google
                    preparedSourceText += tagText;

                    //now we have to figure out whether this tag is preceded and/or followed by whitespace
                    if (i > 0 && !sourceSegment.Elements[i - 1].GetType().ToString().Equals("Sdl.LanguagePlatform.Core.Tag"))
                    {
                        var prevText = sourceSegment.Elements[i - 1].ToString();
                        if (!prevText.Trim().Equals(""))//and not just whitespace
                        {
                            //get number of trailing spaces for that segment
                            var whitespace = prevText.Length - prevText.TrimEnd().Length;
                            //add that trailing space to our tag as leading space
                            theTag.PadLeft = prevText.Substring(prevText.Length - whitespace);
                        }
                    }
                    if (i < sourceSegment.Elements.Count - 1 && !sourceSegment.Elements[i + 1].GetType().ToString().Equals("Sdl.LanguagePlatform.Core.Tag"))
                    {
                        //here we don't care whether it is only whitespace
                        //get number of leading spaces for that segment
                        var nextText   = sourceSegment.Elements[i + 1].ToString();
                        var whitespace = nextText.Length - nextText.TrimStart().Length;
                        //add that trailing space to our tag as leading space
                        theTag.PadRight = nextText.Substring(0, whitespace);
                    }
                    dict.Add(tagText, theTag); //add our new tag code to the dict with the corresponding tag
                }
                else
                {                                                                           //if not a tag
                    var str = HttpUtility.HtmlEncode(sourceSegment.Elements[i].ToString()); //HtmlEncode our plain text to be better processed by google and add to string
                    preparedSourceText += str;
                }
            }

            return(dict);
        }
Exemple #2
0
        /// <summary>
        /// Get the corresponding dictionary for the source tags
        /// </summary>
        /// <returns></returns>
        private Dictionary <string, MtTag> GetSourceTagsDict()
        {
            //build dict by adding the new tag which is used for translation process and the actual tag from segment that will be used to display the translation in editor
            dict = new Dictionary <string, MtTag>();
            try
            {
                for (var i = 0; i < _sourceSegment.Elements.Count; i++)
                {
                    var elType = _sourceSegment.Elements[i].GetType();

                    if (elType.ToString() == "Sdl.LanguagePlatform.Core.Tag")                     //if tag, add to dictionary
                    {
                        var theTag  = new MtTag((Tag)_sourceSegment.Elements[i].Duplicate());
                        var tagText = string.Empty;

                        var tagInfo = new TagInfo
                        {
                            TagType  = theTag.SdlTag.Type,
                            Index    = i,
                            IsClosed = false,
                            TagId    = theTag.SdlTag.TagID
                        };
                        if (!TagsInfo.Any(n => n.TagId.Equals(tagInfo.TagId)))
                        {
                            TagsInfo.Add(tagInfo);
                        }

                        var tag = GetCorrespondingTag(theTag.SdlTag.TagID);
                        if (theTag.SdlTag.Type == TagType.Start)
                        {
                            if (tag != null)
                            {
                                tagText = "<tg" + tag.TagId + ">";
                            }
                        }
                        if (theTag.SdlTag.Type == TagType.End)
                        {
                            if (tag != null)
                            {
                                tag.IsClosed = true;
                                tagText      = "</tg" + tag.TagId + ">";
                            }
                        }
                        if (theTag.SdlTag.Type.Equals(TagType.Standalone) ||
                            theTag.SdlTag.Type.Equals(TagType.TextPlaceholder) ||
                            theTag.SdlTag.Type.Equals(TagType.LockedContent))
                        {
                            if (tag != null)
                            {
                                tagText = "<tg" + tag.TagId + "/>";
                            }
                        }
                        PreparedSourceText += tagText;
                        //now we have to figure out whether this tag is preceded and/or followed by whitespace
                        if (i > 0 && !_sourceSegment.Elements[i - 1].GetType().ToString().Equals("Sdl.LanguagePlatform.Core.Tag"))
                        {
                            var prevText = _sourceSegment.Elements[i - 1].ToString();
                            if (!prevText.Trim().Equals(""))                            //and not just whitespace
                            {
                                //get number of trailing spaces for that segment
                                var whitespace = prevText.Length - prevText.TrimEnd().Length;
                                //add that trailing space to our tag as leading space
                                theTag.PadLeft = prevText.Substring(prevText.Length - whitespace);
                            }
                        }
                        if (i < _sourceSegment.Elements.Count - 1 && !_sourceSegment.Elements[i + 1].GetType().ToString().Equals("Sdl.LanguagePlatform.Core.Tag"))
                        {
                            //here we don't care whether it is only whitespace
                            //get number of leading spaces for that segment
                            var nextText   = _sourceSegment.Elements[i + 1].ToString();
                            var whitespace = nextText.Length - nextText.TrimStart().Length;

                            //add that trailing space to our tag as leading space
                            theTag.PadRight = nextText.Substring(0, whitespace);
                        }
                        dict.Add(tagText, theTag);                         //add our new tag code to the dict with the corresponding tag
                    }
                    else
                    {
                        var str = HttpUtility.HtmlEncode(_sourceSegment.Elements[i].ToString());                         //HtmlEncode our plain text to be better processed by google and add to string
                        //_preparedSourceText += str;
                        PreparedSourceText += _sourceSegment.Elements[i].ToString();
                    }
                }
                TagsInfo.Clear();
            }
            catch (Exception ex)
            {
                _logger.Error($"{_constants.GetSourceTagsDict} {ex.Message}\n { ex.StackTrace}");
            }
            return(dict);
        }
        private Dictionary <string, MtTag> GetSourceTagsDict()
        {
            dict = new Dictionary <string, MtTag>(); //try this
            //build dict
            for (var i = 0; i < sourceSegment.Elements.Count; i++)
            {
                var elType = sourceSegment.Elements[i].GetType();

                if (elType.ToString() == "Sdl.LanguagePlatform.Core.Tag") //if tag, add to dictionary
                {
                    var theTag  = new MtTag((Tag)sourceSegment.Elements[i].Duplicate());
                    var tagText = string.Empty;

                    var tagInfo = new TagInfo
                    {
                        TagType  = theTag.SdlTag.Type,
                        Index    = i,
                        IsClosed = false,
                        TagId    = theTag.SdlTag.TagID
                    };
                    if (!TagsInfo.Any(n => n.TagId.Equals(tagInfo.TagId)))
                    {
                        TagsInfo.Add(tagInfo);
                    }

                    var tag = GetCorrespondingTag(theTag.SdlTag.TagID);
                    if (theTag.SdlTag.Type == TagType.Start)
                    {
                        if (tag != null)
                        {
                            tagText = "<tg" + tag.TagId + ">";
                        }
                    }
                    if (theTag.SdlTag.Type == TagType.End)
                    {
                        if (tag != null)
                        {
                            tag.IsClosed = true;
                            tagText      = "</tg" + tag.Index + ">";
                        }
                    }
                    if (theTag.SdlTag.Type == TagType.Standalone || theTag.SdlTag.Type == TagType.TextPlaceholder)
                    {
                        if (tag != null)
                        {
                            tagText = "</tg" + tag.TagId + ">";
                        }
                    }

                    preparedSourceText += tagText;
                    //now we have to figure out whether this tag is preceded and/or followed by whitespace
                    if (i > 0 && !sourceSegment.Elements[i - 1].GetType().ToString().Equals("Sdl.LanguagePlatform.Core.Tag"))
                    {
                        var prevText = sourceSegment.Elements[i - 1].ToString();
                        if (!prevText.Trim().Equals(""))//and not just whitespace
                        {
                            //get number of trailing spaces for that segment
                            var whitespace = prevText.Length - prevText.TrimEnd().Length;
                            //add that trailing space to our tag as leading space
                            theTag.PadLeft = prevText.Substring(prevText.Length - whitespace);
                        }
                    }
                    if (i < sourceSegment.Elements.Count - 1 && !sourceSegment.Elements[i + 1].GetType().ToString().Equals("Sdl.LanguagePlatform.Core.Tag"))
                    {
                        //here we don't care whether it is only whitespace
                        //get number of leading spaces for that segment
                        var nextText   = sourceSegment.Elements[i + 1].ToString();
                        var whitespace = nextText.Length - nextText.TrimStart().Length;
                        //add that trailing space to our tag as leading space
                        theTag.PadRight = nextText.Substring(0, whitespace);
                    }
                    dict.Add(tagText, theTag); //add our new tag code to the dict with the corresponding tag
                }
                else
                {                                                                           //if not a tag
                    var str = HttpUtility.HtmlEncode(sourceSegment.Elements[i].ToString()); //HtmlEncode our plain text to be better processed by google and add to string
                    preparedSourceText += str;
                }
            }
            TagsInfo.Clear();
            return(dict);
        }