Beispiel #1
0
        //Used to start the translation process
        public void Go()
        {
            XElement TargetRoot;

            //Get the Translation API
            TranslateAPI.TranslateAPIBase translator = null;
            switch (_service.ToLower())
            {
            case "microsoft":
                translator = new TranslateAPI.MicrosoftAPI(this._APIKey, this._SourceLanguage, this._TargetLanguage);
                break;

            default:
                translator = new TranslateAPI.GoogleAPI(this._APIKey, this._SourceLanguage, this._TargetLanguage);
                break;
            }


            //First thing go out and try to open the source file and start processing
            using (TextReader tr = File.OpenText(_SourcePath))
            {
                XDocument xdoc = XDocument.Load(tr);
                XElement  root = xdoc.Element("resources");
                TargetRoot = new XElement("resources");

                foreach (XElement e in root.Elements())
                {
                    if (e.Name.LocalName.Equals("string", StringComparison.CurrentCultureIgnoreCase))
                    {
                        //Normal String Element
                        TargetRoot.Add(ProcessString(translator, e));
                    }
                    else if (e.Name.LocalName.Equals("string-array", StringComparison.CurrentCultureIgnoreCase))
                    {
                        //String Array
                        TargetRoot.Add(ProcessStringArray(translator, e));
                    }
                    else
                    {
                        //Unknown element, just pass it thru
                        TargetRoot.Add(e);
                    }
                }
            }

            //Now that the translation is done we can output the result
            XmlTextWriter tw = new XmlTextWriter(this._TargetPath, Encoding.UTF8);

            tw.Formatting = Formatting.Indented;
            TargetRoot.WriteTo(tw);
            tw.Close();
            Console.WriteLine("Done.");
        }
Beispiel #2
0
        /// <summary>
        /// Used for translating string array
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        private XElement ProcessStringArray(TranslateAPI.TranslateAPIBase translator, XElement e)
        {
            XElement OutputEelement = new XElement("string-array", e.Attribute("name"));

            //loop through the invidual strings and process them one at a time
            foreach (XElement item in e.Elements("item"))
            {
                OutputEelement.Add(new XElement("item", EscapeString(translator.TranslateString(item.Value))));
            }

            return(OutputEelement);
        }