/// <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}");
            }
        }
Example #2
0
        public static TargetLanguageCode AddFile(string analyzeFilePath)
        {
            var languageCode       = Helper.GetTargetLanguageCode(analyzeFilePath);
            var targetLanguageCode = _languageCodes.FirstOrDefault(x => x.LanguageCode.Equals(languageCode));

            if (targetLanguageCode == null)
            {
                targetLanguageCode = new TargetLanguageCode
                {
                    LanguageCode = languageCode,
                    AnalyzeFiles = new List <AnalyzeFile>()
                };

                _languageCodes.Add(targetLanguageCode);
            }

            var fileName = string.Format("{0} {1}", Path.GetFileNameWithoutExtension(analyzeFilePath),
                                         targetLanguageCode.AnalyzeFiles.Count + 1);

            targetLanguageCode.AnalyzeFiles.Add(new AnalyzeFile
            {
                Name = fileName,
                Path = analyzeFilePath
            });

            return(targetLanguageCode);
        }
        /// <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);
            }
        }
Example #4
0
        public static TargetLanguageCode AddFile(string analyzeFilePath)
        {
            var languageCode       = Helper.GetTargetLanguageCode(Helper.GetFileName(analyzeFilePath));
            var targetLanguageCode = _languageCodes.FirstOrDefault(x => x.LanguageCode.Equals(languageCode));

            if (targetLanguageCode == null)
            {
                targetLanguageCode = new TargetLanguageCode
                {
                    LanguageCode = languageCode,
                    AnalyzeFiles = new List <AnalyzeFile>()
                };

                _languageCodes.Add(targetLanguageCode);
            }

            targetLanguageCode.AnalyzeFiles.Add(new AnalyzeFile
            {
                Name = Helper.GetXMLFileName(analyzeFilePath) + " " + (targetLanguageCode.AnalyzeFiles.Count + 1),
                Path = analyzeFilePath
            });

            return(targetLanguageCode);
        }
Example #5
0
 public TargetLanguageCodeViewModel(TargetLanguageCode targetLanguageCode, string iconUri) : base(null, true)
 {
     _targetLanguageCode = targetLanguageCode;
     _iconUri            = iconUri;
 }