Example #1
0
        private void DataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var cultureReference   = this.ToolStripComboBoxCultureEntryReference.SelectedItem as Culture;
            var cultureTranslation = this.DataGridView.Rows [e.RowIndex].DataBoundItem as CultureTranslation;
            var cultureSelected    = ((this.ListViewCultures.SelectedItems.Count == 1) ? (this.ListViewCultures.SelectedItems [0].Tag as Culture) : ((Culture)null));

            if (e.ColumnIndex == this.DataGridView.Columns ["ColumnCultureTranslationCopyReference"].Index)
            {
                if ((cultureSelected != null) && (cultureReference != null) && (cultureSelected.Id == cultureReference.Id))
                {
                    cultureTranslation.Value = cultureTranslation.ValueReference;
                    this.DataGridView.Rows [e.RowIndex].Cells [e.ColumnIndex].Value = cultureTranslation.ValueReference;

                    this.DataGridView.Refresh();
                }
            }
            else if (e.ColumnIndex == this.DataGridView.Columns ["ColumnCultureTranslationCopyToAll"].Index)
            {
                if ((cultureSelected != null) && (cultureReference != null) && (cultureSelected.Id == cultureReference.Id))
                {
                    var cultureTranslations = this.ResourceContainer.CultureTranslations.Where(ct => ct.Translation.Id == cultureTranslation.Translation.Id);

                    foreach (var ct in cultureTranslations)
                    {
                        ct.Value = cultureTranslation.Value;
                    }

                    this.DataGridView.Refresh();
                }
            }
            else if (e.ColumnIndex == this.DataGridView.Columns ["ColumnCultureTranslationGetTranslationMicrosoft"].Index)
            {
                if ((cultureSelected != null) && (cultureReference != null) && (cultureSelected.Id == cultureReference.Id))
                {
                    var       header    = "";
                    Exception exception = null;
                    MicrosoftTranslatorApiHelper.GetAccessToken(out header, out exception);

                    cultureTranslation.ValueReference = cultureTranslation.Value;
                    this.DataGridView.Rows [e.RowIndex].Cells [e.ColumnIndex].Value = cultureTranslation.Value;

                    this.DataGridView.Refresh();
                }
            }
        }
Example #2
0
        private void ToolStripButtonMicrosoftTranslatorFill_Click(object sender, EventArgs e)
        {
            if ((this.ListViewCultures.SelectedItems.Count == 1) && (this.ResourceContainer.CultureTranslations.Any()) && (this.ToolStripComboBoxCultureEntryReference.SelectedIndex > 0))
            {
                var cultureTarget     = (this.ListViewCultures.SelectedItems [0].Tag as Culture);
                var cultureSource     = (this.ToolStripComboBoxCultureEntryReference.SelectedItem as Culture);
                var cultureInfoTarget = CultureInfo.GetCultureInfo(cultureTarget.Name);
                var cultureInfoSource = CultureInfo.GetCultureInfo(cultureSource.Name);

                var message
                    = "This action will overwrite any changes"
                      + Environment.NewLine
                      + "you have made to the targeted culture."
                      + Environment.NewLine
                      + Environment.NewLine
                      + "Source Culture: "
                      + (cultureSource).ToString()
                      + Environment.NewLine
                      + "Target Culture: "
                      + (cultureTarget).ToString()
                      + Environment.NewLine
                      + Environment.NewLine
                      + "Are you sure you want to continue?";

                var result = MessageBox.Show(this, message, Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);

                if (result == DialogResult.Yes)
                {
                    try
                    {
                        var       header    = "";
                        Exception exception = null;

                        this.Enabled = false;
                        this.Cursor  = Cursors.WaitCursor;

                        if (MicrosoftTranslatorApiHelper.GetAccessToken(out header, out exception))
                        {
                            var cultureTranslations = this.ResourceContainer.CultureTranslations.Where(ct => ct.Culture.Name == cultureTarget.Name);

                            foreach (var cultureTranslation in cultureTranslations)
                            {
                                var textSource     = "";
                                var textTranslated = "";

                                textSource = cultureTranslation.ValueReference;

                                if (MicrosoftTranslatorApiHelper.GetTranslation(header, textSource, cultureInfoSource, cultureInfoTarget, out textTranslated, out exception))
                                {
                                    cultureTranslation.Value = textTranslated;

                                    this.DataGridView.Refresh();
                                }
                                else
                                {
                                    throw (exception);
                                }
                            }
                        }
                        else
                        {
                            throw (exception);
                        }
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(this, exception.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                    finally
                    {
                        this.Cursor  = Cursors.Default;
                        this.Enabled = true;

                        this.DataGridView.Refresh();
                    }
                }
            }
        }