From eef37a69af853aee72e9ed95fa1b65d263f0fa90 Mon Sep 17 00:00:00 2001 From: Kay Unkroth Date: Wed, 20 Apr 2022 15:09:43 -0700 Subject: [PATCH] Display an error message when the Web request to the Translator service fails. --- .../Resources/StringDictionary.xaml | 2 + .../Translations/TranslatorService.cs | 41 +++++++++++++------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/MetadataTranslator/Metadata Translator/Resources/StringDictionary.xaml b/MetadataTranslator/Metadata Translator/Resources/StringDictionary.xaml index 5fb7b1b..387e3ad 100644 --- a/MetadataTranslator/Metadata Translator/Resources/StringDictionary.xaml +++ b/MetadataTranslator/Metadata Translator/Resources/StringDictionary.xaml @@ -32,6 +32,8 @@ This is typically the Global region, but could also be a single Azure region. Please select at least one language before you translate the metadata. Microsoft Translator was unable to translate the metadata strings into {0}. Please try again later or remove this language from the translation. + Microsoft Translator was unable to translate the metadata strings because of a Web request error. Make sure the Microsoft Translator configuration settings are correct and check your network connection. + Web Request Error Export .csv files You can export the metadata captions, descriptions, and display folder strings into .csv files based on the locale identifiers (LCIDs) of the selected languages. Metadata Translator creates a separate .csv file for each LCID. Select the folder where you want to store your .csv files. Note that Metadata Translator might overwrite any existing files in this folder. diff --git a/MetadataTranslator/Metadata Translator/Translations/TranslatorService.cs b/MetadataTranslator/Metadata Translator/Translations/TranslatorService.cs index a109c0c..478e403 100644 --- a/MetadataTranslator/Metadata Translator/Translations/TranslatorService.cs +++ b/MetadataTranslator/Metadata Translator/Translations/TranslatorService.cs @@ -7,6 +7,7 @@ using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Web.Script.Serialization; +using System.Windows; namespace Metadata_Translator { @@ -111,15 +112,18 @@ namespace Metadata_Translator /// Translate the batch and assign the translated strings to the target languages. /// var translatedStrings = TranslateBatch(translationBatch, translationId); - for (int i = 0; i < maxBatchSize; i++) + if (translatedStrings.Count > 0) { - foreach (Language language in targetLanguages) + for (int i = 0; i < maxBatchSize; i++) { - dataRows[batchStart + i].SetValue(language.LanguageTag, translatedStrings[i], replaceExistingTranslations); + foreach (Language language in targetLanguages) + { + dataRows[batchStart + i].SetValue(language.LanguageTag, translatedStrings[i], replaceExistingTranslations); + } } - } - Translate(dataRows, targetLanguages, translationId, replaceExistingTranslations, ++iterationId); + Translate(dataRows, targetLanguages, translationId, replaceExistingTranslations, ++iterationId); + } } private List TranslateBatch(List sourceObjects, string targetLanguage) @@ -143,16 +147,29 @@ namespace Metadata_Translator HttpResponseMessage response = client.SendAsync(request).Result; string result = response.Content.ReadAsStringAsync().Result; - /// Parse the results and add the strings to the translated phrases if there was no error, - /// i.e. the target language was returned together with the translated string, which is - /// not the case if the service gives back an error message. + /// Display an error message if the Web request was not successful. /// - List parsedResults = new JavaScriptSerializer().Deserialize>(result); - if (parsedResults != null) + if (!response.IsSuccessStatusCode) { - for (int n = 0; n < parsedResults.Count; n++) + MessageBox.Show( + Application.Current.FindResource("UnableToAccessTranslatorEndpoint") as string, + Application.Current.FindResource("WebRequestError") as string, + MessageBoxButton.OK); + } + else + { + + /// Parse the results and add the strings to the translated phrases if there was no error, + /// i.e. the target language was returned together with the translated string, which is + /// not the case if the service gives back an error message. + /// + List parsedResults = new JavaScriptSerializer().Deserialize>(result); + if (parsedResults != null) { - translatedPhrases.Add((string.IsNullOrEmpty(parsedResults[n].translations[0].to))? "" : parsedResults[n].translations[0].text); + for (int n = 0; n < parsedResults.Count; n++) + { + translatedPhrases.Add((string.IsNullOrEmpty(parsedResults[n].translations[0].to)) ? "" : parsedResults[n].translations[0].text); + } } } }