Google Translate API vs Resource Files

This is probably not a new idea but I was copy / pasting content into Google translate as a rough French resource file to test the localization that I was building. I thought I could use the translate API to do this for me. The result is this really quick file auto translator.

Translator class

namespace TranslatorBot
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Google.API.Translate;

    public class Translator
    {
        public string TranslateEngToFr(string txt)
        {
            TranslateClient client = new TranslateClient("http://cfpc.ca");
            string translated = client.Translate(txt, Language.English, Language.French);
            Console.WriteLine(txt + " => " + translated);
            return translated;
        }
    }
}

Main class

namespace TranslatorBot
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Google.API.Translate;
    using System.Xml.Linq;
    using System.Xml;
    using System.Text.RegularExpressions;

    class Program
    {
        static void Main(string[] args)
        {           
            string path = "C:\\inetpub\\wwwroot\\cfpcDev\\App_GlobalResources\\";

            //load the doc to be trsanslated 
            XDocument doc = XDocument.Load(path + "Global.resx");

            //load the doc to be changed
            XDocument docTranslated = XDocument.Load(path + "Global.resx");

            //remove all the data elements
            docTranslated.Descendants("data").Remove();

            Translator bot = new Translator();
            var ListToTranslate = doc.Descendants("data");
            var root = doc.Element("root");
            var rootTrans = docTranslated.Element("root");
            XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
            //  // Directories //
            foreach (var node in ListToTranslate)
            {
                if (node.Attribute("name").Value.StartsWith("img") || node.Attribute("name").Value.StartsWith("lnk") || node.Attribute("name").Value.ToLower().Contains(@"image"))
                {
                    node.Value = Regex.Replace(node.Value, @"\s", @"");
                    node.Value = node.Value.Replace(@"/en/", @"/fr/");
                    var datanode = new XElement("data");
                    datanode.Add(new XAttribute("name", node.Attribute("name").Value));
                    datanode.Add(new XAttribute(XNamespace.Xml + "space", "preserve"));
                    var valueNode = new XElement("value");
                    valueNode.Value = node.Value;
                    datanode.Add(valueNode);
                    rootTrans.Add(datanode);
                }
                else
                {
                    var datanode = new XElement("data");
                    datanode.Add(new XAttribute("name", node.Attribute("name").Value));
                    datanode.Add(new XAttribute(XNamespace.Xml + "space", "preserve"));
                    var valueNode = new XElement("value");
                    valueNode.Value = bot.TranslateEngToFr(node.Value);
                    valueNode.Value = Regex.Replace(valueNode.Value, @"\((\d{1,3})\)", @"{$1}");

                    datanode.Add(valueNode);
                    rootTrans.Add(datanode);
                }


            }
            docTranslated.Save(path + "Global.fr-CA.resx");
        }
    }
}

I will tidy it up and add command line switches and use it in my Continuous Integration build in the future to localize my content.

1 Comment

Add a Comment
  1. Will be used to show your gravatar.

Tags: ASP.Net