Exemple #1
0
        public void Translate(string fromlanguage, string tolanguage)
        {
            Dictionary <int, MDUtterance> ToDict = new Dictionary <int, MDUtterance>();

//            Parallel.ForEach(MDContent, (mdutterance) =>
            foreach (var mdutterance in MDContent)
            {
                MDUtterance toutterance = new MDUtterance();
                toutterance = mdutterance.Value;
                if (mdutterance.Value.lineType != LineType.code)
                {
                    toutterance.text = Tagged2Markdown(TranslationServices.Core.TranslationServiceFacade.TranslateString(Markdown2Tagged(mdutterance.Value.text), fromlanguage, tolanguage, 0));
                }
                ToDict.Add(mdutterance.Key, toutterance);
            }
            ;
            _langcode = TranslationServices.Core.TranslationServiceFacade.LanguageNameToLanguageCode(tolanguage);
            MDContent = ToDict;        //Replace the master collection with the translated version.
        }
Exemple #2
0
        /// <summary>
        /// Load the MD file into the internal document format, a dictionary.
        /// Key of the dictionary is the utterance number.
        /// </summary>
        /// <param name="mddocument">A string containing the MD document</param>
        /// <param name="langcode">The language code of the document</param>
        /// <returns>Count of utterances</returns>
        public int LoadMD(string mddocument, string langcode)
        {
            _langcode = langcode;
            string[] md                = mddocument.Split('\n');
            int      lineindex         = 0;
            int      paragraphindex    = 0;
            bool     insidecodesection = false;

            while (lineindex < md.Length)
            {
                string trimline = md[lineindex].Trim();
                paragraphindex++;
                if (paragraphindex > 0)
                {
                    MDUtterance mdutterance = new MDUtterance
                    {
                        text      = string.Empty,
                        spanlines = 0,
                        lineType  = LineType.translatable
                    };

                    //Check for empty string
                    if (trimline.Length == 0)
                    {
                        mdutterance.lineType = LineType.empty;
                        MDContent.Add(paragraphindex, mdutterance);
                        lineindex++;
                        continue;
                    }
                    //Mark lines enclosed with "'''" as untranslatable code.
                    if (insidecodesection)
                    {
                        mdutterance.lineType = LineType.code;
                    }
                    if (trimline.StartsWith(@"'''") || trimline.StartsWith(@"```"))
                    {
                        mdutterance.lineType = LineType.code;
                        if (insidecodesection)
                        {
                            insidecodesection = false;
                        }
                        else
                        {
                            insidecodesection = true;
                        }
                    }

                    //Check for Blockquote. Remove the > character and treat as continuous utterance. Mark utterance as linetype.blockquote
                    if (trimline.StartsWith(@">"))
                    {
                        trimline             = trimline.Substring(1).Trim();
                        mdutterance.lineType = LineType.blockquote;
                    }

                    //Check for Lists
                    if (trimline.StartsWith(@"*"))
                    {
                        mdutterance.lineType = LineType.list;
                    }

                    //Check for heading. If heading, leave as single paragraph, do not combine lines. Else combine lines until an empty line.
                    if (trimline.Length > 0)
                    {
                        if (trimline.StartsWith("#") || insidecodesection || trimline.StartsWith(@"'''") || trimline.StartsWith(@"```") || trimline.StartsWith(@"*") || trimline.StartsWith(@"-"))
                        {
                            //create a single-line utterance
                            mdutterance.text = trimline;
                        }
                        else
                        {
                            //combine multiple lines to a single utterance
                            while (md[lineindex].Trim().Length > 0)
                            {
                                //break on ''' (start of untranslatable code section)
                                if (md[lineindex].TrimStart().StartsWith(@"'''") || md[lineindex].TrimStart().StartsWith(@"```"))
                                {
                                    MDContent.Add(paragraphindex, mdutterance);
                                    insidecodesection     = true;
                                    mdutterance.text      = md[lineindex];
                                    mdutterance.lineType  = LineType.code;
                                    mdutterance.spanlines = 1;
                                    paragraphindex++;
                                    break;
                                }

                                //check for blockquote
                                if (md[lineindex].TrimStart().StartsWith(@">"))
                                {
                                    mdutterance.lineType = LineType.blockquote;
                                    mdutterance.text    += md[lineindex].Trim().Substring(1);
                                }
                                else
                                {
                                    mdutterance.text += md[lineindex].Trim();
                                }
                                //add a space between the lines for languages that use space
                                if (!nonspacelanguages.Contains(langcode.ToLowerInvariant()))
                                {
                                    mdutterance.text += " ";
                                }
                                lineindex++;

                                if (lineindex == md.Length)
                                {
                                    break;
                                }
                                mdutterance.spanlines++;
                            }
                        }
                        mdutterance.text = mdutterance.text.Replace("\r\n", string.Empty);
                    }
                    Debug.WriteLine(mdutterance.lineType.ToString() + ": " + mdutterance.text);
                    MDContent.Add(paragraphindex, mdutterance);
                    lineindex++;
                }
            }
            return(MDContent.Count);
        }