/// <summary> /// Translates given string to specified language. /// </summary> /// <param name="ToTranslate">string</param> /// <param name="Language">string</param> /// <returns>translated string</returns> public string Translate(string ToTranslate, string Language) { //split input on whitespace var Words = ToTranslate.Split(' '); var NewWordList = new ArrayList(); foreach (var Word in Words) { //check for digits and symbols var HasDigitsOrSymbols = Word.Any(char.IsDigit) || Word.Any(char.IsSymbol); //check if string is all punctuation var IsAllPunctuation = IsAllPunct(Word); //translate only if string is free from digits or symbols and is not all punctuation if (!HasDigitsOrSymbols && !IsAllPunctuation) { //remove leading and trailing punctuation var TrimmedWord = TrimPunctuation(Word); var TranslatedWord = ""; //do not translate if word contains internal punctuation, except for hyphenated words and contractions if (!TrimmedWord.Any(char.IsPunctuation) || Regex.IsMatch(TrimmedWord, ContractionRegexPattern)) { if (Word.Length > 0) { if (Language.Equals(Language1)) { //new object instance var PigLatinTranslator = new PigLatinTranslator(); //translate to PigLatin TranslatedWord = PigLatinTranslator.ConvertTo(TrimmedWord); } else if (Language.Equals(Language2)) { //new object instance var PigGreekTranslator = new PigGreekTranslator(); //translate to PigGreek TranslatedWord = PigGreekTranslator.ConvertTo(TrimmedWord); } else { /*doNothing();*/ } } } else { //no translation performed TranslatedWord = TrimmedWord; } //add punctuation back to word var PunctuatedWord = this.LeadingPunctuation + TranslatedWord + this.TrailingPunctuation; //add word to arraylist NewWordList.Add(PunctuatedWord); } else { //add unaltered word to arraylist NewWordList.Add(Word); } } return(string.Join(" ", NewWordList.ToArray())); }