Beispiel #1
0
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            int count = 1;

            foreach (string to in sel)
            {
                string tex = "Translating " + count + " of " + sel.Count + "   From " + from + "  to  " + to;
                worker.ReportProgress(count * 100 / sel.Count);
                UpdateMyDelegatedelegate UpdateMyDelegate = new UpdateMyDelegatedelegate(UpdateMyDelegateLabel);
                console_output.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, UpdateMyDelegate, tex);

                TranslateLanguage(stringtext, stringoutfolder, from, to, check);
                count++;
                console_output.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, UpdateMyDelegate, "Done... Task is finished now!\n");
                worker.ReportProgress(0);
            }
        }
Beispiel #2
0
        public void TranslateLanguage(string input, string output, string sourceLanguage, string targetLanguage, bool overrideIfExists)
        {
            Console.WriteLine("Start translating {0} -> {1}", sourceLanguage, targetLanguage);
            UpdateMyDelegatedelegate UpdateMyDelegate = new UpdateMyDelegatedelegate(UpdateMyDelegateLabel2);

            //    console_output.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, UpdateMyDelegate, "Translating " + sourceLanguage + "  to  " + targetLanguage +" . . . .");
            try
            {
                string outputPath = System.IO.Path.Combine(output, string.Format("res/values-{0}", targetLanguage));

                if (!Directory.Exists(outputPath))
                {
                    Directory.CreateDirectory(outputPath);
                }

                outputPath = System.IO.Path.Combine(outputPath, "strings.xml");

                Console.WriteLine("Output File: {0}.", outputPath);

                //source xml document
                XmlDocument sourceDoc = new XmlDocument();
                sourceDoc.Load(input);

                //find source resources
                XmlNode sourceResources = FindResourcesRootElement(sourceDoc);

                //destination xml document
                XmlDocument destinationDoc       = null;
                XmlNode     destinationResources = null;
                //optimization for quick find a node
                Dictionary <string, XmlNode> destinationNodes = new Dictionary <string, XmlNode>();
                if (File.Exists(outputPath))
                {
                    destinationDoc = new XmlDocument();
                    destinationDoc.Load(outputPath);

                    //find destination resources
                    destinationResources = FindResourcesRootElement(destinationDoc);

                    if (destinationResources == null)
                    {
                        //force null
                        destinationDoc = null;
                    }
                    else
                    {
                        for (int i = 0; i < destinationResources.ChildNodes.Count; i++)
                        {
                            XmlNode node = destinationResources.ChildNodes[i];

                            if (node.NodeType == XmlNodeType.Element)
                            {
                                string name = node.Attributes["name"].Value;
                                destinationNodes.Add(name, node);
                            }
                        }
                    }
                }

                //Display the contents of the child nodes.
                if (sourceResources != null && sourceResources.HasChildNodes)
                {
                    for (int i = 0; i < sourceResources.ChildNodes.Count; i++)
                    {
                        XmlNode node = sourceResources.ChildNodes[i];

                        if (node.NodeType == XmlNodeType.Element)
                        {
                            string name = node.Attributes["name"].Value;
                            string text = node.InnerText;

                            if (destinationDoc == null) //no destination
                            {
                                node.InnerText = translator.Translate(text, sourceLanguage, targetLanguage);
                                node.InnerText = node.InnerText.Replace("'", "\'");
                                //     BingTranslator.Translate(sourceLanguage, , text);
                            }
                            else
                            {
                                XmlNode outputNode = destinationNodes.ContainsKey(name) ? destinationNodes[name] : null;

                                if (outputNode == null || overrideIfExists)
                                {
                                    node.InnerText = translator.Translate(text, sourceLanguage, targetLanguage);
                                    node.InnerText = node.InnerText.Replace("'", "\'");
                                }
                                else
                                {
                                    node.InnerText = outputNode.InnerText;
                                    node.InnerText = node.InnerText.Replace("'", "\'");
                                }
                            }
                        }
                    }
                }

                sourceDoc.Save(outputPath);
                Console.WriteLine("Finish translating {0} -> {1}", sourceLanguage, targetLanguage);
                //   console_output.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, UpdateMyDelegate, "  ... Done \n");
            }
            catch (Exception ex)
            {
                console_error.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, UpdateMyDelegate, "  ERROR: " + ex.Message + "\n");

                Console.WriteLine("Error in translation from {0} -> {1}", sourceLanguage, targetLanguage);
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }