Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        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);
        }