/// <summary> /// Main execution method for activity /// </summary> /// <param name="Text"></param> /// <param name="TargetLanguageCode"></param> /// <param name="apiKey"></param> /// <returns></returns> protected Dictionary <string, string> Execute(string Text, string TargetLanguageCode, string apiKey = "") { var output = new Dictionary <string, string>(); var textToTranslate = Text; var targetLanguageCode = TargetLanguageCode; if (apiKey != "") { // 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); output = new Dictionary <string, string> { { "Translated Text", translatedText }, { "Detected Language", detectedSourceLanguage } }; return(output); } catch (System.Exception ex) { throw new System.Exception(MicrosoftTranslationClient.InvalidApiKeyResolution); } }
/// <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}"); } }