/// <summary> /// Main activity method /// </summary> /// <param name="context"></param> protected override void Execute(CodeActivityContext context) { var textToTranslate = Text.Get(context); var targetLanguageCode = TargetLanguageCode.Get(context); var apiKey = ApiKey.Get(context); if (apiKey != null) { // Sets to the user's apiKey, if supplied; if not, defaults to a free key MicrosoftTranslationClient.ApiKey = apiKey; } try { var translatedText = MicrosoftTranslationClient.TranslateText(textToTranslate, targetLanguageCode); var detectedSourceLanguage = MicrosoftTranslationClient.Detect(textToTranslate); TranslatedText.Set(context, translatedText); DetectedSourceLanguage.Set(context, detectedSourceLanguage); } catch (System.Exception ex) { throw new System.Exception($"Actual Error: {ex.Message}\n{MicrosoftTranslationClient.InvalidApiKeyResolution}"); } }
/// <summary> /// Main activity method /// </summary> /// <param name="context"></param> protected override void Execute(CodeActivityContext context) { var inputFileSelection = Document.Get(context); string[] files = inputFileSelection.Split("|".ToCharArray()); List <string> filesForTranslation = new List <string>(); List <string> translatedFiles = new List <string>(); if (files.Length == 1) { if (!File.Exists(files[0])) { // Check if only one file is selected and if it exists throw new System.IO.FileNotFoundException("The document specified is not found. Make sure the file exists or full path is typed correctly."); } else { filesForTranslation.Add(files[0]); } } else { foreach (var selectedFile in files) { if (File.Exists(selectedFile)) { filesForTranslation.Add(selectedFile); } } if (filesForTranslation.Count == 0) { // All files specified are not found. throw new System.IO.FileNotFoundException("All files specified are not found. Make sure the file exists or full path is typed correctly."); } } var targetLanguageCode = TargetLanguageCode.Get(context); var apiKey = ApiKey.Get(context); if (apiKey != null && apiKey != "") { // Sets to the user's apiKey, if supplied; if not, defaults to a free key MicrosoftTranslationClient.ApiKey = apiKey; } try { translatedFiles = DocumentTranslationClient.TranslateDocument(String.Join(",", filesForTranslation), targetLanguageCode); TranslatedDoc.Set(context, translatedFiles); } catch (System.UnauthorizedAccessException uex) { throw new System.UnauthorizedAccessException($"FULL OUTPUT: {uex.Message}\n{MicrosoftTranslationClient.InvalidApiKeyResolution}"); } catch (System.Exception ex) { throw new System.Exception(ex.Message); } }