private void btnTranslate_Click(object sender, EventArgs e)
        {
            if (cmbDesc.SelectedIndex == -1 || cmbSrc.SelectedIndex == -1)
            {
                MessageBox.Show("Please select source and destination languages correctly.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (txtSrc.Text.Length == 0)
            {
                MessageBox.Show("The text body can not be empty.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            var lngSrc  = ((KeyValuePair <string, string>)cmbSrc.SelectedItem).Key;
            var lngDest = ((KeyValuePair <string, string>)cmbDesc.SelectedItem).Key;
            var text    = txtSrc.Text;

            // There is no longer a key to validate
            var textTranslatorUrlKey = "";

            IsBusy(true);
            GTranslateService.TranslateAsync(
                text, lngSrc, lngDest, textTranslatorUrlKey,
                (success, result, type) =>
            {
                SetResult(result, type);
                IsBusy(false);
            });
        }
Exemple #2
0
        private void btnTranslate_Click(object sender, EventArgs e)
        {
            var a = new Action(() =>
            {
                var result = GetGoogleTranslatorKey_UiThread(txtSrc.Text);
                if (result.Success)
                {
                }
            });

            a.BeginInvoke((v) => { }, null);
            return;

            if (cmbDesc.SelectedIndex == -1 || cmbSrc.SelectedIndex == -1)
            {
                MessageBox.Show("Please select source and destination languages correctly.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (txtSrc.Text.Length == 0)
            {
                MessageBox.Show("The text body can not be empty.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            var lngSrc  = ((KeyValuePair <string, string>)cmbSrc.SelectedItem).Key;
            var lngDest = ((KeyValuePair <string, string>)cmbDesc.SelectedItem).Key;
            var text    = txtSrc.Text;

            var textTranslatorResult = GetGoogleTranslatorKey(text);

            if (!textTranslatorResult.Success)
            {
                txtDesc.Text = textTranslatorResult.Result;
                return;
            }
            var textTranslatorUrlKey = textTranslatorResult.Result;

            IsBusy(true);
            GTranslateService.TranslateAsync(
                text, lngSrc, lngDest, textTranslatorUrlKey,
                (success, result) =>
            {
                SetResult(result);
                IsBusy(false);
            });
        }
Exemple #3
0
        private async void btnTranslate_ClickAsync(object sender, EventArgs e)
        {
            if (cmbDesc.SelectedIndex == -1 || cmbSrc.SelectedIndex == -1)
            {
                MessageBox.Show("Please select source and destination languages correctly.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (txtSrc.Text.Length == 0)
            {
                MessageBox.Show("The text body can not be empty.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            var lngSrc  = ((KeyValuePair <string, string>)cmbSrc.SelectedItem).Key;
            var lngDest = ((KeyValuePair <string, string>)cmbDesc.SelectedItem).Key;
            var text    = txtSrc.Text;

            var translationOptions = new TranslationOptions
            {
                ServiceType          = ServiceType,
                MsSubscriptionKey    = txtMsTranslationKey.Text,
                MsSubscriptionRegion = txtMsTranslationRegion.Text
            };


            IsBusy(true);

            if (ServiceType == ServiceTypeEnum.Google)
            {
                // There is no longer a key to validate
                var textTranslatorUrlKey = "";

                GTranslateService.TranslateAsync(
                    text, lngSrc, lngDest, textTranslatorUrlKey,
                    (success, result) =>
                {
                    SetResult(result);
                    IsBusy(false);
                });
            }
            else
            {
                var translationResult = await MsTranslateService.TranslateAsync(text, lngSrc, lngDest, txtMsTranslationKey.Text, txtMsTranslationRegion.Text);

                if (translationResult.Success)
                {
                    SetResult(translationResult.Result);
                }
                else
                {
                    if (!string.IsNullOrEmpty(translationResult.Result))
                    {
                        SetResult(translationResult.Result);
                    }
                    else
                    {
                        SetResult("Translation Failed!");
                    }
                }

                IsBusy(false);
            }
        }
Exemple #4
0
        async void TranslateResxFilesAsync(
            string sourceResx,
            string sourceLng,
            TranslationOptions translationOptions,
            List <string> desLanguages, string destDir,
            ResxProgressCallback progress)
        {
            int    max       = 0;
            int    pos       = 0;
            int    trycount  = 0;
            string status    = "";
            bool   hasErrors = false;

            var sourceResxFilename = ReadLanguageFilename(sourceResx);
            var errorLogFilename   = sourceResxFilename + ".errors.log";
            var errorLogFile       = Path.Combine(destDir, errorLogFilename);

            foreach (var destLng in desLanguages)
            {
                var destFile = Path.Combine(destDir, sourceResxFilename + "." + destLng + ".resx");
                var doc      = new XmlDocument();
                doc.Load(sourceResx);
                var dataList = ResxTranslator.ReadResxData(doc);
                max = dataList.Count;

                pos    = 0;
                status = "Translating language: " + destLng;
                progress.BeginInvoke(max, pos, status, null, null);

                try
                {
                    foreach (var node in dataList)
                    {
                        status = "Translating language: " + destLng;
                        pos   += 1;
                        progress.BeginInvoke(max, pos, status, null, null);


                        var valueNode = ResxTranslator.GetDataValueNode(node);
                        if (valueNode == null)
                        {
                            continue;
                        }

                        var orgText = valueNode.InnerText;
                        if (string.IsNullOrWhiteSpace(orgText))
                        {
                            continue;
                        }


                        if (translationOptions.ServiceType == ServiceTypeEnum.Google)
                        {
                            // There is no longer a key to validate
                            // the key
                            var textTranslatorUrlKey = "";

                            string translated = string.Empty;
                            bool   success    = false;
                            trycount = 0;
                            do
                            {
                                try
                                {
                                    success = GTranslateService.Translate(orgText, sourceLng, destLng, textTranslatorUrlKey, out translated);
                                }
                                catch (Exception)
                                {
                                    success = false;
                                }
                                trycount++;

                                if (!success)
                                {
                                    var key = ResxTranslator.GetDataKeyName(node);
                                    status = "Translating language: " + destLng + " , key '" + key + "' failed to translate in try " + trycount;
                                    progress.BeginInvoke(max, pos, status, null, null);
                                }
                            } while (success == false && trycount <= 2);

                            if (success)
                            {
                                valueNode.InnerText = translated;
                            }
                            else
                            {
                                hasErrors = true;
                                var key = ResxTranslator.GetDataKeyName(node);
                                try
                                {
                                    string message = "\r\nKey '" + key + "' translation to language '" + destLng + "' failed.";
                                    File.AppendAllText(errorLogFile, message);
                                }
                                catch
                                {
                                }
                            }
                        }
                        else if (translationOptions.ServiceType == ServiceTypeEnum.Microsoft)
                        {
                            var translationResult = await MsTranslateService.TranslateAsync(orgText, sourceLng, destLng,
                                                                                            translationOptions.MsSubscriptionKey, translationOptions.MsSubscriptionRegion);

                            if (translationResult.Success)
                            {
                                valueNode.InnerText = translationResult.Result;
                            }
                            else
                            {
                                hasErrors = true;
                                var key = ResxTranslator.GetDataKeyName(node);
                                try
                                {
                                    string message = "\r\nKey '" + key + "' translation to language '" + destLng + "' failed. ";
                                    if (!string.IsNullOrEmpty(translationResult.Result))
                                    {
                                        message += " Error message: " + translationResult.Result;
                                    }

                                    File.AppendAllText(errorLogFile, message);
                                }
                                catch { }
                            }
                        }
                    }
                }
                finally
                {
                    // now save that shit!
                    doc.Save(destFile);
                }
            }

            if (hasErrors)
            {
                status = "Translation finished. Errors are logged in to '" + errorLogFilename + "'.";
            }
            else
            {
                status = "Translation finished.";
            }


            progress.BeginInvoke(max, pos, status, null, null);
        }
Exemple #5
0
 public frmMain()
 {
     InitializeComponent();
     gTranslateService = new GTranslateService();
 }
        void TranslateResxFilesAsync(string sourceResx, string sourceLng, List <string> desLanguages, string destDir,
                                     ResxProgressCallback progress)
        {
            int    max       = 0;
            int    pos       = 0;
            int    trycount  = 0;
            string status    = "";
            bool   hasErrors = false;

            var sourceResxFilename = ReadLanguageFilename(sourceResx);
            var errorLogFilename   = sourceResxFilename + ".errors.log";
            var errorLogFile       = Path.Combine(destDir, errorLogFilename);

            foreach (var destLng in desLanguages)
            {
                var destFile = Path.Combine(destDir, sourceResxFilename + "." + destLng + ".resx");
                var doc      = new XmlDocument();
                doc.Load(sourceResx);
                var dataList = ResxTranslator.ReadResxData(doc);
                max = dataList.Count;

                pos    = 0;
                status = "Translating language: " + destLng;
                progress(max, pos, status);

                try
                {
                    foreach (var node in dataList)
                    {
                        status = "Translating language: " + destLng;
                        pos   += 1;
                        progress(max, pos, status);

                        if (!ResxTranslator.HasDisplayText(node))
                        {
                            continue;
                        }

                        var valueNode = ResxTranslator.GetDataValueNode(node);
                        if (valueNode == null)
                        {
                            continue;
                        }

                        var orgText = valueNode.InnerText;
                        if (string.IsNullOrWhiteSpace(orgText))
                        {
                            continue;
                        }

                        // There is no longer a key to validate
                        // the key
                        var textTranslatorUrlKey = "";

                        string translated = string.Empty;
                        bool   success    = false;
                        trycount = 0;
                        do
                        {
                            if (gibberish.Checked)
                            {
                                translated = getGibberish(orgText.ToCharArray().Length);

                                success = true;
                            }
                            else
                            {
                                try
                                {
                                    success = GTranslateService.Translate(orgText, sourceLng, destLng, textTranslatorUrlKey, out translated);
                                }
                                catch (Exception)
                                {
                                    success = false;
                                }
                                trycount++;
                            }


                            if (!success)
                            {
                                var key = ResxTranslator.GetDataKeyName(node);
                                status = "Translating language: " + destLng + " , key '" + key + "' failed to translate in try " + trycount;
                                progress(max, pos, status);
                            }
                        } while (success == false && trycount <= 2);



                        if (success)
                        {
                            valueNode.InnerText = translated;
                        }
                        else
                        {
                            hasErrors = true;
                            var key = ResxTranslator.GetDataKeyName(node);
                            try
                            {
                                string message = "\r\nKey '" + key + "' translation to language '" + destLng + "' failed.";
                                File.AppendAllText(errorLogFile, message);
                            }
                            catch
                            {
                            }
                            if (leftoverGibberish.Checked)
                            {
                                translated          = getGibberish(orgText.ToCharArray().Length);
                                valueNode.InnerText = translated;
                            }
                        }
                    }
                }
                finally
                {
                    // now save that shit!
                    doc.Save(destFile);
                }
            }

            if (hasErrors)
            {
                status = "Translation finished. Errors are logged in to '" + errorLogFilename + "'.";
            }
            else
            {
                status = "Translation finished.";
            }


            progress(max, pos, status);
        }