private async void TranslateButton_Click(object sender, RoutedEventArgs e) { string fromLanguage = FromLanguageComboBox.SelectedValue.ToString(); string fromLanguageCode; if (fromLanguage == "Detect") { fromLanguageCode = new MSTranslate(TEXT_TRANSLATION_API_ENDPOINT, COGNITIVE_SERVICES_KEY).DetectLanguage(TextToTranslate.Text); if (!languageCodes.Contains(fromLanguageCode)) { MessageBox.Show("The source language could not be detected automatically " + "or is not supported for translation.", "Language detection failed", MessageBoxButton.OK, MessageBoxImage.Error); return; } } else { fromLanguageCode = languageCodesAndTitles[fromLanguage]; } string toLanguageCode = languageCodesAndTitles[ToLanguageComboBox.SelectedValue.ToString()]; string textToTranslate = TextToTranslate.Text.Trim(); string translation = await new MSTranslate(TEXT_TRANSLATION_API_ENDPOINT, COGNITIVE_SERVICES_KEY).go(textToTranslate, fromLanguageCode, toLanguageCode); TranslatedTextLabel.Content = translation; }
private void GetLanguagesForTranslate() { // Send request to get supported language codes var languages = new MSTranslate(TEXT_TRANSLATION_API_ENDPOINT, COGNITIVE_SERVICES_KEY).GetLanguagesForTranslate(); languageCodes = languages.Keys.ToArray(); foreach (var kv in languages) { languageCodesAndTitles.Add(kv.Value["name"], kv.Key); } }