/// <summary>
    /// Submits the node for translation. Does not check the permissions, you need to check it before calling this method.
    /// </summary>
    public string SubmitToTranslation()
    {
        string err = ValidateData();

        if (!string.IsNullOrEmpty(err))
        {
            return(err);
        }

        var settings = TranslationSettings ?? new TranslationSettings();

        if (settings.TargetLanguages.Count == 0)
        {
            settings.TargetLanguages.Add(currentCulture);
        }

        settings.TranslateWebpartProperties = SettingsKeyInfoProvider.GetBoolValue(SiteContext.CurrentSiteName + ".CMSTranslateWebpartProperties");
        settings.SourceLanguage             = FromLanguage;
        settings.Instructions           = Instructions;
        settings.Priority               = Priority;
        settings.ProcessBinary          = ProcessBinary;
        settings.TranslateAttachments   = ProcessBinary;
        settings.TranslationDeadline    = Deadline;
        settings.TranslationServiceName = SelectedService;

        var node = TreeProvider.GetOriginalNode(DocumentHelper.GetDocument(NodeID, settings.SourceLanguage, true, TreeProvider));

        TranslationSubmissionInfo submissionInfo;

        return(TranslationServiceHelper.SubmitToTranslation(settings, node, out submissionInfo));
    }
Пример #2
0
    /// <summary>
    /// Submits the node for translation. Does not check the permissions, you need to chek it before calling this method.
    /// </summary>
    public string SubmitToTranslation()
    {
        string err = ValidateData();

        if (!string.IsNullOrEmpty(err))
        {
            return(err);
        }

        TranslationSettings settings = TranslationSettings ?? new TranslationSettings();

        if (string.IsNullOrEmpty(settings.TargetLanguage))
        {
            settings.TargetLanguage = LocalizationContext.PreferredCultureCode;
        }
        settings.TranslateWebpartProperties = SettingsKeyInfoProvider.GetBoolValue(SiteContext.CurrentSiteName + ".CMSTranslateWebpartProperties");
        settings.SourceLanguage             = FromLanguage;
        settings.Instructions           = Instructions;
        settings.Priority               = Priority;
        settings.ProcessBinary          = ProcessBinary;
        settings.TranslateAttachments   = ProcessBinary;
        settings.TranslationDeadline    = Deadline;
        settings.TranslationServiceName = hdnSelectedName.Value;

        TreeProvider tree = TreeProvider ?? new TreeProvider();
        TreeNode     node = DocumentHelper.GetDocument(NodeID, settings.SourceLanguage, true, tree);

        TranslationSubmissionInfo submissionInfo;

        return(TranslationServiceHelper.SubmitToTranslation(settings, node, out submissionInfo));
    }
    /// <summary>
    /// Submits the node for translation. Does not check the permissions, you need to chek it before calling this method.
    /// </summary>
    public string SubmitToTranslation()
    {
        string err = ValidateData();

        if (!string.IsNullOrEmpty(err))
        {
            return(err);
        }

        TranslationSettings settings = TranslationSettings ?? new TranslationSettings();

        if (string.IsNullOrEmpty(settings.TargetLanguage))
        {
            settings.TargetLanguage = CMSContext.PreferredCultureCode;
        }
        settings.TranslateWebpartProperties = SettingsKeyProvider.GetBoolValue(CMSContext.CurrentSiteName + ".CMSTranslateWebpartProperties");
        settings.SourceLanguage             = FromLanguage;
        settings.Instructions           = Instructions;
        settings.Priority               = Priority;
        settings.ProcessBinary          = ProcessBinary;
        settings.TranslateAttachments   = ProcessBinary;
        settings.TranslationDeadline    = Deadline;
        settings.TranslationServiceName = hdnSelectedName.Value;
        settings.GenerateTargetTag      = ValidationHelper.GetBoolean(hdnGenerateTarget.Value, false);

        TreeProvider tree = TreeProvider;

        if (tree == null)
        {
            tree = new TreeProvider();
        }
        TreeNode node = DocumentHelper.GetDocument(NodeID, settings.SourceLanguage, true, tree); //tree.SelectSingleNode(NodeID, settings.SourceLanguage, true, settings.TranslateDocCoupledData);

        TranslationSubmissionInfo submissionInfo = null;

        return(TranslationServiceHelper.SubmitToTranslation(settings, node, out submissionInfo));
    }
    /// <summary>
    /// Translates document(s).
    /// </summary>
    private void Translate(object parameter)
    {
        if (parameter == null || nodeIds.Count < 1)
        {
            return;
        }

        int refreshId = 0;

        TreeProvider tree = new TreeProvider(currentUser);

        tree.AllowAsyncActions = false;

        try
        {
            // Begin log
            AddLog(ResHelper.GetString("contentrequest.starttranslate", currentCulture));

            bool oneSubmission = chkSeparateSubmissions.Checked;

            // Prepare translation settings
            TranslationSettings settings = new TranslationSettings();
            settings.TargetLanguage             = targetCulture;
            settings.TranslateWebpartProperties = SettingsKeyProvider.GetBoolValue(CMSContext.CurrentSiteName + ".CMSTranslateWebpartProperties");
            settings.SourceLanguage             = translationElem.FromLanguage;
            settings.Instructions           = translationElem.Instructions;
            settings.Priority               = translationElem.Priority;
            settings.TranslateAttachments   = translationElem.ProcessBinary;
            settings.ProcessBinary          = translationElem.ProcessBinary;
            settings.TranslationDeadline    = translationElem.Deadline;
            settings.TranslationServiceName = translationElem.SelectedService;

            using (CMSTransactionScope tr = new CMSTransactionScope())
            {
                // Get the translation provider
                AbstractMachineTranslationService machineService = null;
                AbstractHumanTranslationService   humanService   = null;
                TranslationSubmissionInfo         submission     = null;
                TranslationServiceInfo            ti             = TranslationServiceInfoProvider.GetTranslationServiceInfo(translationElem.SelectedService);
                if (ti != null)
                {
                    if (oneSubmission)
                    {
                        if (ti.TranslationServiceIsMachine)
                        {
                            machineService = AbstractMachineTranslationService.GetTranslationService(ti, CurrentSiteName);
                        }
                        else
                        {
                            humanService = AbstractHumanTranslationService.GetTranslationService(ti, CurrentSiteName);

                            if (oneSubmission)
                            {
                                submission = TranslationServiceHelper.CreateSubmissionInfo(settings, ti, CMSContext.CurrentUser.UserID, CMSContext.CurrentSiteID, "Document submission " + DateTime.Now);
                            }
                        }
                    }

                    bool langSupported = true;
                    if (humanService != null)
                    {
                        if (!humanService.IsLanguageSupported(settings.TargetLanguage))
                        {
                            AddError(ResHelper.GetString("translationservice.targetlanguagenotsupported"));
                            langSupported = false;
                        }
                    }

                    if (langSupported)
                    {
                        if (!oneSubmission || (machineService != null) || (humanService != null))
                        {
                            // Prepare the where condition
                            string where = SqlHelperClass.GetWhereCondition("NodeID", (int[])nodeIds.ToArray(typeof(int)));
                            string columns = "NodeID, NodeAliasPath, DocumentCulture, NodeParentID";

                            string submissionFileName = "";
                            string submissionName     = "";
                            int    charCount          = 0;
                            int    wordCount          = 0;

                            int docCount = 0;

                            // Get the documents in target culture to be able to check if "Skip already translated" option is on
                            // Combine both, source and target culture (at least one hit has to be found - to find the source of translation)
                            where = SqlHelperClass.AddWhereCondition(where, "DocumentCulture = N'" + settings.SourceLanguage + "' OR DocumentCulture = N'" + settings.TargetLanguage + "'");

                            DataSet ds = tree.SelectNodes(CMSContext.CurrentSiteName, "/%", TreeProvider.ALL_CULTURES, true, null, where, "NodeAliasPath DESC", TreeProvider.ALL_LEVELS, false, 0, columns);
                            if (!DataHelper.DataSourceIsEmpty(ds))
                            {
                                List <int> processedNodes = new List <int>();

                                // Translate the documents
                                foreach (DataRow dr in ds.Tables[0].Rows)
                                {
                                    refreshId = ValidationHelper.GetInteger(dr["NodeParentID"], 0);
                                    int nodeId = ValidationHelper.GetInteger(dr["NodeID"], 0);

                                    if (!processedNodes.Contains(nodeId))
                                    {
                                        processedNodes.Add(nodeId);

                                        string aliasPath = ValidationHelper.GetString(dr["NodeAliasPath"], "");
                                        string culture   = ValidationHelper.GetString(dr["DocumentCulture"], "");

                                        if (chkSkipTranslated.Checked)
                                        {
                                            if (culture == settings.TargetLanguage)
                                            {
                                                // Document already exists in requested culture, skip it
                                                AddLog(string.Format(ResHelper.GetString("content.translatedalready"), HTMLHelper.HTMLEncode(aliasPath + " (" + culture + ")")));
                                                continue;
                                            }
                                        }

                                        AddLog(string.Format(ResHelper.GetString("content.translating"), HTMLHelper.HTMLEncode(aliasPath + " (" + culture + ")")));

                                        TreeNode node = DocumentHelper.GetDocument(nodeId, settings.SourceLanguage, true, null);

                                        // Save the first document as a base for submission name
                                        if (string.IsNullOrEmpty(submissionName))
                                        {
                                            submissionName = node.GetDocumentName();
                                        }
                                        if (string.IsNullOrEmpty(submissionFileName))
                                        {
                                            submissionFileName = node.NodeAlias;
                                        }

                                        docCount++;

                                        // Submit the document
                                        if (machineService != null)
                                        {
                                            TranslationServiceHelper.Translate(machineService, settings, node);
                                        }
                                        else
                                        {
                                            if (oneSubmission && (humanService != null))
                                            {
                                                TreeNode targetNode = TranslationServiceHelper.CreateTargetCultureNode(node, settings.TargetLanguage, true, false);
                                                TranslationSubmissionItemInfo submissionItem = TranslationServiceHelper.CreateSubmissionItemInfo(settings, submission, node, targetNode.DocumentID);

                                                charCount += submissionItem.SubmissionItemCharCount;
                                                wordCount += submissionItem.SubmissionItemWordCount;
                                            }
                                            else
                                            {
                                                TranslationServiceHelper.SubmitToTranslation(settings, node, out submission);
                                            }
                                        }
                                    }
                                }

                                if (docCount > 0)
                                {
                                    if (oneSubmission && (humanService != null))
                                    {
                                        AddLog(ResHelper.GetString("content.submitingtranslation"));

                                        // Set submission name
                                        int itemCount = processedNodes.Count;
                                        if (itemCount > 1)
                                        {
                                            submissionName += " " + string.Format(GetString("translationservices.submissionnamesuffix"), itemCount - 1);
                                        }
                                        submission.SubmissionName      = submissionName;
                                        submission.SubmissionCharCount = charCount;
                                        submission.SubmissionWordCount = wordCount;
                                        submission.SubmissionItemCount = itemCount;
                                        submission.SubmissionParameter = submissionFileName;

                                        string err = humanService.CreateSubmission(submission);
                                        if (!string.IsNullOrEmpty(err))
                                        {
                                            AddError(err);
                                        }

                                        // Save submission with ticket
                                        TranslationSubmissionInfoProvider.SetTranslationSubmissionInfo(submission);
                                    }
                                }
                                else
                                {
                                    TranslationSubmissionInfoProvider.DeleteTranslationSubmissionInfo(submission);
                                    AddError(ResHelper.GetString("TranslateDocument.DocumentsAlreadyTranslated", currentCulture));
                                }
                            }
                        }
                        else
                        {
                            AddError(ResHelper.GetString("TranslateDocument.TranslationServiceNotFound", currentCulture));
                        }
                    }
                }

                tr.Commit();
            }
        }
        catch (ThreadAbortException ex)
        {
            string state = ValidationHelper.GetString(ex.ExceptionState, string.Empty);
            if (state == CMSThread.ABORT_REASON_STOP)
            {
                // When canceled
                AddError(ResHelper.GetString("TranslateDocument.TranslationCanceled", currentCulture));
            }
            else
            {
                // Log error
                LogExceptionToEventLog(ex);
            }
        }
        catch (Exception ex)
        {
            // Log error
            LogExceptionToEventLog(ex);
        }
        finally
        {
            if (isModal)
            {
                ctlAsync.Parameter = "wopener.location.replace(wopener.location); CloseDialog();";
            }
            else
            {
                if (string.IsNullOrEmpty(CurrentError))
                {
                    // Refresh tree
                    ctlAsync.Parameter = "RefreshTree(" + refreshId + ", " + refreshId + "); \n" + "SelectNode(" + refreshId + ");";
                }
                else
                {
                    ctlAsync.Parameter = "RefreshTree(null, null);";
                }
            }
        }
    }