public static void AddQuery(string text, string LanguageCodeFrom, string LanguageCodeTo, TranslationDictionary dict) { if (string.IsNullOrEmpty(text)) { return; } if (!dict.ContainsKey(text)) { var query = new TranslationQuery() { OrigText = text, LanguageCode = LanguageCodeFrom, TargetLanguagesCode = new string[] { LanguageCodeTo } }; query.Text = text; ParseNonTranslatableElements(ref query); dict[text] = query; } else { var query = dict [text]; if (System.Array.IndexOf(query.TargetLanguagesCode, LanguageCodeTo) < 0) { query.TargetLanguagesCode = query.TargetLanguagesCode.Concat(new string[] { LanguageCodeTo }).Distinct().ToArray(); } dict [text] = query; } }
static void ParseNonTranslatableElements(ref TranslationQuery query) { //\[i2nt].*\[\/i2nt] var matches = Regex.Matches(query.Text, @"\{\[(.*?)]}|\[(.*?)]|\<(.*?)>"); if (matches == null || matches.Count == 0) { return; } string finalText = query.Text; List <string> finalTags = new List <string>(); for (int i = 0, imax = matches.Count; i < imax; ++i) { var tag = I2Utils.GetCaptureMatch(matches[i]); int iClosingTag = FindClosingTag(tag, matches, i); // find [/tag] or </tag> if (iClosingTag < 0) { // Its not a tag, its a parameter var fulltag = matches[i].ToString(); if (fulltag.StartsWith("{[") && fulltag.EndsWith("]}")) { finalText = finalText.Replace(fulltag, GetGoogleNoTranslateTag(finalTags.Count) + " "); // 0x2600 is the start of the UNICODE Miscellaneous Symbols table, so they are not going to be translated by google //finalText = finalText.Replace(fulltag, /*"{[" + finalTags.Count + "]}"*/ ((char)(0x2600 + finalTags.Count)).ToString()); // 0x2600 is the start of the UNICODE Miscellaneous Symbols table, so they are not going to be translated by google finalTags.Add(fulltag); } continue; } if (tag == "i2nt") { var tag1 = query.Text.Substring(matches[i].Index, (matches[iClosingTag].Index - matches[i].Index) + matches[iClosingTag].Length); finalText = finalText.Replace(tag1, GetGoogleNoTranslateTag(finalTags.Count) + " "); //finalText = finalText.Replace(tag1, /*"{[" + finalTags.Count + "]}"*/ ((char)(0x2600 + finalTags.Count)).ToString()); finalTags.Add(tag1); } else { var tag1 = matches[i].ToString(); finalText = finalText.Replace(tag1, GetGoogleNoTranslateTag(finalTags.Count) + " "); //finalText = finalText.Replace(tag1, /*"{[" + finalTags.Count + "]}"*/ ((char)(0x2600 + finalTags.Count)).ToString()); finalTags.Add(tag1); var tag2 = matches[iClosingTag].ToString(); finalText = finalText.Replace(tag2, GetGoogleNoTranslateTag(finalTags.Count) + " "); //finalText = finalText.Replace(tag2, /*"{[" + finalTags.Count + "]}"*/ ((char)(0x2600 + finalTags.Count)).ToString()); finalTags.Add(tag2); } } query.Text = finalText; query.Tags = finalTags.ToArray(); }