Display an error message when the Web request to the Translator service fails.

This commit is contained in:
Kay Unkroth 2022-04-20 15:09:43 -07:00
parent 0953dd1d16
commit eef37a69af
2 changed files with 31 additions and 12 deletions

View File

@ -32,6 +32,8 @@
<system:String x:Key="TranslatorLocationPrompt">This is typically the Global region, but could also be a single Azure region.</system:String>
<system:String x:Key="NothingToTranslate">Please select at least one language before you translate the metadata.</system:String>
<system:String x:Key="UnableToTranslate">Microsoft Translator was unable to translate the metadata strings into {0}. Please try again later or remove this language from the translation.</system:String>
<system:String x:Key="UnableToAccessTranslatorEndpoint">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.</system:String>
<system:String x:Key="WebRequestError">Web Request Error</system:String>
<system:String x:Key="ExportHeading">Export .csv files</system:String>
<system:String x:Key="ExportDescription">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.</system:String>
<system:String x:Key="ExportFolderDescription">Select the folder where you want to store your .csv files. Note that Metadata Translator might overwrite any existing files in this folder.</system:String>

View File

@ -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<string> TranslateBatch(List<object> 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<TranslationResult> parsedResults = new JavaScriptSerializer().Deserialize<List<TranslationResult>>(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<TranslationResult> parsedResults = new JavaScriptSerializer().Deserialize<List<TranslationResult>>(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);
}
}
}
}