/// <summary> /// Translates an array of strings from a source language to a target language. /// </summary> /// <param name="translateArraySourceTexts">The strings to translate.</param> /// <param name="sourceLanguage">The language code of the translation text. For example, "en" for English.</param> /// <param name="targetLanguage">The language code to translate the text into.</param> /// <returns>A task that represents the translation operation. /// The task result contains a list of the translated documents.</returns> public async Task <List <ITranslatedDocument> > TranslateArrayAsync(string[] translateArraySourceTexts, string sourceLanguage, string targetLanguage) { var translatedDocuments = new List <ITranslatedDocument>(); for (var srcTxtIndx = 0; srcTxtIndx < translateArraySourceTexts.Length; srcTxtIndx++) { var currentTranslatedDocument = new TranslatedDocument(translateArraySourceTexts[srcTxtIndx]); translatedDocuments.Add(currentTranslatedDocument); // Check for literal tag in input user message _preProcessor.PreprocessMessage(currentTranslatedDocument.GetSourceMessage(), out var processedText, out var literanlNoTranslateList); currentTranslatedDocument.SetSourceMessage(processedText); translateArraySourceTexts[srcTxtIndx] = processedText; currentTranslatedDocument.SetLiteranlNoTranslatePhrases(literanlNoTranslateList); } // list of translation request for the service var payload = translateArraySourceTexts.Select(s => new TranslatorRequestModel { Text = s }); using (var request = _requestBuilder.BuildTranslateRequest(sourceLanguage, targetLanguage, payload)) { var translatedResults = await _responseGenerator.GenerateTranslateResponseAsync(request).ConfigureAwait(false); var sentIndex = 0; foreach (var translatedValue in translatedResults) { var translation = translatedValue.Translations.First(); var currentTranslatedDocument = translatedDocuments[sentIndex]; currentTranslatedDocument.SetRawAlignment(translation.Alignment?.Projection ?? null); currentTranslatedDocument.SetTranslatedMessage(translation.Text); if (!string.IsNullOrEmpty(currentTranslatedDocument.GetRawAlignment())) { var alignments = currentTranslatedDocument.GetRawAlignment().Trim().Split(' '); currentTranslatedDocument.SetSourceTokens(PostProcessingUtilities.SplitSentence(currentTranslatedDocument.GetSourceMessage(), alignments)); currentTranslatedDocument.SetTranslatedTokens(PostProcessingUtilities.SplitSentence(translation.Text, alignments, false)); currentTranslatedDocument.SetIndexedAlignment(PostProcessingUtilities.WordAlignmentParse(alignments, currentTranslatedDocument.GetSourceTokens(), currentTranslatedDocument.GetTranslatedTokens())); currentTranslatedDocument.SetTranslatedMessage(PostProcessingUtilities.Join(" ", currentTranslatedDocument.GetTranslatedTokens())); } else { var translatedText = translation.Text; currentTranslatedDocument.SetTranslatedMessage(translatedText); currentTranslatedDocument.SetSourceTokens(new string[] { currentTranslatedDocument.GetSourceMessage() }); currentTranslatedDocument.SetTranslatedTokens(new string[] { currentTranslatedDocument.GetTranslatedMessage() }); currentTranslatedDocument.SetIndexedAlignment(new Dictionary <int, int>()); } sentIndex++; } // post process all translated documents PostProcesseDocuments(translatedDocuments, sourceLanguage); return(translatedDocuments); } }