private Dictionary <string, BeGlobalTag> GetSourceTagsDict()
        {
            _tagsDictionary = new Dictionary <string, BeGlobalTag>();            //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 BeGlobalTag((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 (theTag.SdlTag.Type == TagType.Start)
                    {
                        tagText = "<tg" + tagInfo.TagId + ">";
                    }
                    if (theTag.SdlTag.Type == TagType.End)
                    {
                        tagInfo.IsClosed = true;
                        tagText          = "</tg" + tagInfo.TagId + ">";
                    }
                    if (theTag.SdlTag.Type == TagType.Standalone || theTag.SdlTag.Type == TagType.TextPlaceholder)
                    {
                        tagText = "<tg" + tagInfo.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);
                    }
                    _tagsDictionary.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;
                    PreparedSourceText += _sourceSegment.Elements[i].ToString();
                }
            }
            TagsInfo.Clear();
            return(_tagsDictionary);
        }
Esempio n. 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);
        }
Esempio n. 3
0
        /// <summary>
        /// Get the corresponding dictionary for the source tags
        /// </summary>
        /// <returns></returns>
        private Dictionary <string, TartuNLPTag> 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, TartuNLPTag>();
            if (_formattingAndTagUsage)
            {
                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 TartuNLPTag((Tag)_sourceSegment.Elements[i].Duplicate());
                            var tagText = theTag.SdlTag.ToString();
                            PreparedSourceText += tagText;

                            if (!TagsInfo.Any(n => n.Equals(theTag.SdlTag.TagID)))
                            {
                                TagsInfo.Add(theTag.SdlTag.TagID);
                            }

                            //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
                        {
                            PreparedSourceText += _sourceSegment.Elements[i].ToString();
                        }
                    }
                    TagsInfo.Clear();
                }
                catch (Exception ex)
                {
                    throw new Exception($"GetSourceTagsDict method: {ex.Message}\n { ex.StackTrace}");
                }
            }
            return(_dict);
        }