Exemplo n.º 1
0
 private void MainForm_Load(object sender, EventArgs e)
 {
     try
     {
         _translationsettings = TranslationSetting.ReadFromConfig();
         foreach (var translationsetting in _translationsettings)
         {
             cmbLanguage.Items.Add(translationsetting.From + " -> " + translationsetting.To);
         }
         cmbLanguage.SelectedIndex = 0;
     }
     catch (Exception ex)
     {
         MessageBox.Show(string.Format("Error occured: {0}", ex.Message));
     }
 }
Exemplo n.º 2
0
        private static async Task <string> Translate(string text, TranslationSetting activeSetting)
        {
            var    authTokenSource = new AzureAuthToken(activeSetting.SubscriptionKey.Trim());
            string authToken;

            try
            {
                authToken = await authTokenSource.GetAccessTokenAsync();
            }
            catch (HttpRequestException)
            {
                if (authTokenSource.RequestStatusCode == HttpStatusCode.Unauthorized)
                {
                    throw new ApplicationException("Request to token service is not authorized (401). Check that the Azure subscription key is valid.");
                }
                if (authTokenSource.RequestStatusCode == HttpStatusCode.Forbidden)
                {
                    throw new ApplicationException("Request to token service is not authorized (403). For accounts in the free-tier, check that the account quota is not exceeded.");
                }
                throw;
            }

            return(TranslateApi.Translate(authToken, text, activeSetting.From, activeSetting.To, activeSetting.Category));
        }