コード例 #1
0
        public override void Invoke(IZappyExecutionContext context, ZappyTaskActionInvoker actionInvoker)
        {
            TranslationServiceFacade.AzureKey = TextTranslatorResorceKey;
            AccountViewModel.SaveAccountClick();
            var worker = new BackgroundWorker();

            DocumentTranslationManager.DoTranslation(
                FilePath,
                false,
                FromLanguage,
                ToLanguage,
                false);
        }
        /// <summary>
        ///     The execute.
        /// </summary>
        /// <returns>
        ///     The <see cref="bool" />.
        /// </returns>
        public override bool Execute()
        {
            int           documentcount = 0;
            List <String> listoffiles   = new List <string>();

            //Expand wildcard, if name specification contains *
            if (this.sourceDocuments.Values.ToArray().Any(file => file.ToString().Contains("*")))
            {
                foreach (string filename in this.sourceDocuments.Values.ToArray())
                {
                    int      lastBackslashPosition = filename.LastIndexOf('\\') + 1;
                    string   path         = filename.Substring(0, lastBackslashPosition);
                    string   filenameOnly = filename.Substring(lastBackslashPosition);
                    String[] filelist     = Directory.GetFiles(path, filenameOnly, SearchOption.AllDirectories);
                    listoffiles.AddRange(filelist);
                }
            }
            else  //no * in the file name
            {
                foreach (var file in this.sourceDocuments.ValueString.Split(','))
                {
                    listoffiles.Add(file);
                }
            }

            try
            {
                var model = new CommentTranslationModel
                {
                    SourceLanguage =
                        this.sourceLanguage.ValueString ?? "Auto-Detect",
                    TargetLanguage = this.targetLanguages.ValueString
                };

                foreach (var file in listoffiles)
                {
                    if (!File.Exists(file))
                    {
                        Logger.WriteLine(LogLevel.Error, String.Format("Specified document {0} does not exist. ", file));
                    }
                    foreach (var language in this.targetLanguages.Values)
                    {
                        try
                        {
                            this.Logger.WriteLine(
                                LogLevel.Msg,
                                string.Format(
                                    "Translating document {0} to language {1}.",
                                    file,
                                    language));
                            model.TargetPath = file;
                            var sourceLanguageExpanded = String.IsNullOrEmpty(this.sourceLanguage.ValueString) ||
                                                         this.sourceLanguage.ValueString.Equals("Auto-Detect")
                                                             ? "Auto-Detect"
                                                             : TranslationServiceFacade.AvailableLanguages[
                                this.sourceLanguage.ValueString];
                            string languagename = TranslationServiceFacade.LanguageCodeToLanguageName(language.ToString());

                            DocumentTranslationManager.DoTranslation(
                                file,
                                false,
                                sourceLanguageExpanded,
                                languagename);
                            this.Logger.WriteLine(
                                LogLevel.Msg,
                                string.Format(
                                    "-- Translated document name {0} to language {1}.",
                                    file,
                                    language));
                            documentcount++;
                        }
                        catch (Exception ex)
                        {
                            this.Logger.WriteLine(
                                LogLevel.Error,
                                string.Format(
                                    "Error while processing file: {0} to language {1} with error: {2}",
                                    model.TargetPath,
                                    language,
                                    ex.Message));
                            throw;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.Logger.WriteException(ex);
                Console.ReadLine();
                return(false);
            }

            this.Logger.WriteLine(LogLevel.Msg, string.Format("Documents translated successfully: {0}.", documentcount));
            return(true);
        }
コード例 #3
0
        /// <summary>
        /// Translates a document and provides links to the original as well as the translated document
        /// </summary>
        /// <param name="storageContainerName">Name of the storage container</param>
        /// <param name="storageFileName">Name of the storage file (original document for translation)</param>
        /// <param name="originalLanguage">The language of the original file</param>
        /// <param name="translationLanguage">The language for translating the document</param>
        /// <returns></returns>
        public DocumentLinks TranslateFile(string storageContainerName, string storageFileName, string originalLanguage, string translationLanguage)
        {
            var localFileName = storageFileName;

            try
            {
                TranslationServiceFacade.Initialize(ConfigurationManager.AppSettings["ApiKey"]);

                DocumentTranslationManager.DoTranslation(localFileName, false, originalLanguage, translationLanguage);
            }
            catch (Exception ex)
            {
                _loggingService.Error("Error in TranslationServiceFacade.Initialize or  DocumentTranslationManager.DoTranslation", ex);
                throw;
            }

            string originalFileUrl;
            string translatedFileUrl;
            string translatedDocumentName;

            try
            {
                var languageCode = TranslationServiceFacade.AvailableLanguages.Where(p => p.Value == translationLanguage).Select(p => p.Key).FirstOrDefault();

                var extension = Helper.GetExtension(storageFileName);

                translatedDocumentName = localFileName.Replace($".{extension}", $".{languageCode}.{extension}");

                // Move original file to SharePoint
                originalFileUrl = _sharePointManagementService.CopyFileToSharePoint(localFileName);

                // Move translated file to SharePoint
                translatedFileUrl = _sharePointManagementService.CopyFileToSharePoint(translatedDocumentName);
            }
            catch (Exception ex)
            {
                _loggingService.Error("Error in TranslationServiceFacade.AvailableLanguages.Wher or  Helper.GetExtension or _sharePointManagementService.CopyFileToSharePoint", ex);
                throw;
            }

            try
            {
                // Delete original file
                if (System.IO.File.Exists(localFileName))
                {
                    System.IO.File.Delete(localFileName);
                }

                // Delete translated file
                if (System.IO.File.Exists(translatedDocumentName))
                {
                    System.IO.File.Delete(translatedDocumentName);
                }
            }
            catch (Exception ex)
            {
                _loggingService.Error("Error in System.IO.File.Exists or System.IO.File.Delete", ex);
                throw;
            }

            return(new DocumentLinks
            {
                OriginalDocument = originalFileUrl,
                TranslatedDocument = translatedFileUrl
            });
        }