private int getDestinationTableColumnIndex(
            IInheritedSettings settings,
            DataTable dataTable,
            string languageCode)
        {
            var columnIndex = 0;

            foreach (DataColumn column in dataTable.Columns)
            {
                if (columnIndex > 1) // Column 0=FileGroup checksum, column 1=Tag name.
                {
                    var lc =
                        ExcelExportController.IsFileName(column.ColumnName)
                            ? new LanguageCodeDetection(
                            _information.Project)
                        .DetectLanguageCodeFromFileName(
                            settings,
                            column.ColumnName)
                            : column.ColumnName;

                    if (string.Compare(lc, languageCode, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        return(column.Ordinal);
                    }
                }

                columnIndex++;
            }

            // --
            // 2011-01-24, Uwe Keim: No direct (like "en-US") found, try fuzzy (like "en").

            columnIndex = 0;
            foreach (DataColumn column in dataTable.Columns)
            {
                if (columnIndex > 1) // Column 0=FileGroup checksum, column 1=Tag name.
                {
                    var lc =
                        ExcelExportController.IsFileName(column.ColumnName)
                            ? new LanguageCodeDetection(
                            _information.Project)
                        .DetectLanguageCodeFromFileName(
                            settings,
                            column.ColumnName)
                            : column.ColumnName;

                    if (lc.StartsWith(languageCode, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(column.Ordinal);
                    }
                }

                columnIndex++;
            }

            // --

            return(-1);
        }
        private void doTakeSnapshot(
            IGridEditableData fileGroup,
            IEnumerable <string> languageCodes,
            BackgroundWorker bw)
        {
            //var fgi = fileGroup.GetFileByLanguageCode(Project, languageCode);

            var table = new DataProcessing(fileGroup).GetDataTableFromResxFiles(Project, true);

            var lcs = new List <string>(languageCodes);

            for (var i = 0; i < lcs.Count; i++)
            {
                lcs[i] = lcs[i].ToLowerInvariant();
            }

            var rowIndex = 0;

            foreach (DataRow row in table.Rows)
            {
                if (rowIndex % 25 == 0)
                {
                    if (bw.CancellationPending)
                    {
                        throw new OperationCanceledException();
                    }
                }

                // Column 0=FileGroup checksum, column 1=Tag name.
                var baseKey = MakeBaseKey(row[0], row[1]);

                for (var sourceColumnIndex = 2;
                     sourceColumnIndex < table.Columns.Count - 1;                    // Subtract 1, because last column is ALWAYS the comment.
                     ++sourceColumnIndex)
                {
                    var languageValue = row[sourceColumnIndex] as string;
                    var languageCode  =
                        ExcelExportController.IsFileName(table.Columns[sourceColumnIndex].ColumnName)
                                                        ? new LanguageCodeDetection(Project)
                        .DetectLanguageCodeFromFileName(
                            fileGroup.ParentSettings,
                            table.Columns[sourceColumnIndex].ColumnName)
                                                        : table.Columns[sourceColumnIndex].ColumnName;
                    languageCode = languageCode.ToLowerInvariant();

                    if (                    /*lcs.Contains(languageCode.Substring(0, 2))||*/
                        lcs.Contains(languageCode))
                    {
                        var key = MakeFullKey(baseKey, languageCode);
                        PutSettingValue(key, languageValue);
                    }
                }

                rowIndex++;
            }
        }
        private void doTakeSnapshot(
            InMemoryTranslationSnapshot imss,
            Project project,
            IGridEditableData fileGroup,
            IEnumerable <string> languageCodes,
            BackgroundWorker bw)
        {
            //var fgi = fileGroup.GetFileByLanguageCode(Project, languageCode);

            var table = new DataProcessing(fileGroup).GetDataTableFromResxFiles(project, true);

            var lcs = new List <string>(languageCodes);

            for (var i = 0; i < lcs.Count; i++)
            {
                lcs[i] = lcs[i].ToLowerInvariant();
            }

            var rowIndex = 0;

            foreach (DataRow row in table.Rows)
            {
                if (rowIndex % 25 == 0 && (bw?.CancellationPending ?? false))
                {
                    throw new OperationCanceledException();
                }

                var dic = new Dictionary <string, string>();

                for (var sourceColumnIndex = 2;
                     sourceColumnIndex <
                     table.Columns.Count - 1; // Subtract 1, because last column is ALWAYS the comment.
                     ++sourceColumnIndex)
                {
                    var languageValue = row[sourceColumnIndex] as string;
                    var languageCode  =
                        ExcelExportController.IsFileName(table.Columns[sourceColumnIndex].ColumnName)
                            ? new LanguageCodeDetection(project)
                        .DetectLanguageCodeFromFileName(
                            fileGroup.ParentSettings,
                            table.Columns[sourceColumnIndex].ColumnName)
                            : table.Columns[sourceColumnIndex].ColumnName;
                    languageCode = languageCode.ToLowerInvariant();

                    if (lcs.Contains(languageCode))
                    {
                        dic[languageCode] = languageValue;
                    }
                }

                imss.AddBatchTranslation(dic);

                rowIndex++;
            }
        }
Пример #4
0
        private void buttonTranslate_Click(object sender, EventArgs e)
        {
            var success                 = false;
            var cancelled               = false;
            var table                   = _fileGroupControl.GetDataSource();
            var translationCount        = 0;
            var translationSuccessCount = 0;
            var translationErrorCount   = 0;
            var internallyExistingTranslationTranslatedSuccessCount = 0;

            var continueOnErrors  = _project == null || _project.TranslationContinueOnErrors;
            var delayMilliseconds = _project?.TranslationDelayMilliseconds ?? 500;

            var gridEditableData = _fileGroupControl.GridEditableData;

            var prefixSuccess =
                prefixCheckBox.Checked
                    ? prefixTextBox.Text.Trim() + @" "
                    : string.Empty;
            var useExistingTranslations     = useExistingTranslationsCheckBox.Checked;
            var useExistingTranslationsOnly = useOnlyExistingTranslationsCheckEdit.Checked;

            var prefixError = FileGroup.DefaultTranslationErrorPrefix.Trim() + @" ";

            var refLanguageCode =
                ((TranslationLanguageInfo)referenceLanguageGroupBox.SelectedItem).LanguageCode;
            var toTranslateLanguageCodes = new List <string>();

            // ReSharper disable LoopCanBeConvertedToQuery
            foreach (CheckedListBoxItem item in languagesToTranslateCheckListBox.CheckedItems)
            // ReSharper restore LoopCanBeConvertedToQuery
            {
                var pair = (TranslationLanguageInfo)item.Value;

                toTranslateLanguageCodes.Add(pair.LanguageCode.ToLowerInvariant());
            }

            // --
            // 2017-04-15.

            var prevPause = MainForm.Current.ProjectFilesControl.WantPauseNodeStateUpdate;

            MainForm.Current.ProjectFilesControl.WantPauseNodeStateUpdate = true;

            _fileGroupControl.IsTranslating = true;
            try
            {
                using (
                    new BackgroundWorkerLongProgressGui(
                        delegate(
                            object s,
                            DoWorkEventArgs a)
                {
                    var bw = (BackgroundWorker)s;

                    var imtc = new InMemoryTranslationSnapshotController();
                    var imss = useExistingTranslations
                                ? imtc.CreateSnapshot(_project,
                                                      toTranslateLanguageCodes.Concat(new[] { refLanguageCode }).ToArray(),
                                                      bw)
                                : null;

                    // Column 0=FileGroup checksum, column 1=Tag name.
                    var refValueIndex = 2;

#pragma warning disable 618, 612
                    var prev = DevExpress.Data.CurrencyDataController.DisableThreadingProblemsDetection;
                    DevExpress.Data.CurrencyDataController.DisableThreadingProblemsDetection = true;
#pragma warning restore 618, 612

                    try
                    {
                        foreach (DataColumn column in table.Columns)
                        {
                            // Column 0=FileGroup checksum, column 1=Tag name.
                            if (column.Ordinal > 1)
                            {
                                var raw =
                                    ExcelExportController.IsFileName(column.ColumnName)
                                                ? new LanguageCodeDetection(_project)
                                    .DetectLanguageCodeFromFileName(
                                        gridEditableData.ParentSettings,
                                        column.ColumnName)
                                                : column.ColumnName;

                                if (refLanguageCode == raw)
                                {
                                    refValueIndex = column.Ordinal;
                                    break;
                                }
                            }
                        }

                        // --

                        var ti = TranslationHelper.GetTranslationEngine(_project);

                        TranslationHelper.GetTranslationAppID(
                            MainForm.Current.ProjectFilesControl.Project ?? Project.Empty,
                            out var appID);

                        foreach (DataColumn column in table.Columns)
                        {
                            // Column 0=FileGroup checksum, column 1=Tag name.
                            if (column.Ordinal > 1 && column.Ordinal != refValueIndex)
                            {
                                var raw =
                                    ExcelExportController.IsFileName(column.ColumnName)
                                                ? new LanguageCodeDetection(_project)
                                    .DetectLanguageCodeFromFileName(
                                        gridEditableData.ParentSettings,
                                        column.ColumnName)
                                                : column.ColumnName;

                                if (toTranslateLanguageCodes.Contains(raw.ToLowerInvariant()))
                                {
                                    if (ti.SupportsArrayTranslation)
                                    {
                                        translationSuccessCount =
                                            translateArray(
                                                appID,
                                                ti,
                                                table,
                                                refLanguageCode,
                                                column,
                                                refValueIndex,
                                                raw,
                                                bw,
                                                delayMilliseconds,
                                                prefixSuccess,
                                                useExistingTranslations,
                                                useExistingTranslationsOnly,
                                                imss,
                                                translationSuccessCount,
                                                ref translationErrorCount,
                                                continueOnErrors,
                                                prefixError,
                                                ref translationCount,
                                                ref internallyExistingTranslationTranslatedSuccessCount);
                                    }
                                    else
                                    {
                                        translationSuccessCount =
                                            translateSingle(
                                                appID,
                                                ti,
                                                table,
                                                refLanguageCode,
                                                column,
                                                refValueIndex,
                                                raw,
                                                bw,
                                                delayMilliseconds,
                                                prefixSuccess,
                                                useExistingTranslations,
                                                useExistingTranslationsOnly,
                                                imss,
                                                translationSuccessCount,
                                                ref translationErrorCount,
                                                continueOnErrors,
                                                prefixError,
                                                ref translationCount,
                                                ref internallyExistingTranslationTranslatedSuccessCount);
                                    }
                                }
                            }
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        // Do nothing.
                    }
                    finally
                    {
#pragma warning disable 618, 612
                        DevExpress.Data.CurrencyDataController.DisableThreadingProblemsDetection = prev;
#pragma warning restore 618, 612

                        MainForm.Current.ProjectFilesControl.WantPauseNodeStateUpdate = prevPause;
                    }
                },
                        delegate(
                            object s,
                            RunWorkerCompletedEventArgs a)
                {
                    success = !a.Cancelled && a.Error == null;
                    cancelled = a.Cancelled;
                },
                        BackgroundWorkerLongProgressGui.CancellationMode.Cancelable
                        ))
                {
                }
            }
            finally
            {
                _fileGroupControl.IsTranslating = false;
            }

            if (translationCount > 0)
            {
                _fileGroupControl.MarkGridContentAsModified();
                _fileGroupControl.MarkAsModified();
                _fileGroupControl.UpdateUI();
            }

            var message = string.Format(
                Resources.SR_AutoTranslateForm_buttonTranslateClick_TranslatingTranslatedTexts,
                translationCount,
                translationSuccessCount,
                translationErrorCount);

            if (useExistingTranslations)
            {
                message += @" " + string.Format(
                    Resources.SR_AutoTranslateForm_buttonTranslateClick_TranslatingTranslatedTextsExisting,
                    internallyExistingTranslationTranslatedSuccessCount);
            }

            XtraMessageBox.Show(
                this,
                message,
                @"Zeta Resource Editor",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button1);

            if (success || cancelled)
            {
                DialogResult = DialogResult.OK;
                Close();
            }
        }