Automatic Resource File Translation with the Microsoft Translator Service

On a recent project that I have been working on, we needed the application to be multi-lingual and localized for different countries. The best option for us on this project was to use resource files (.resx) and let .Net handle the rest for us. Now creating these resource files wasn't really a problem, but adding and translating one for each of the languages we needed was going to take some time. This made me think if there was an easier way to do this. I had also recently been playing around with the Microsoft Translator Service and also wrote a blog post about it.

When using resource files, we already have the English version of the text - so why not simply just translate that into the languages that we need? I decided to write a tool that would use the Microsoft Translator Service to translate and create different resource files in the languages in needed. The tool reads the Resource file (it is an xml file) and makes a request to the service and once complete writes a new resource file in the appropriate language. It uses a separate config file to choose the languages that it needs to translate to.

Pros and Cons

Yeah....I know that using translation services might not always produce entirely accurate translations, but one of the great things about using the Microsoft Translator Service is that it lets you choose a rating level of the translation which ensures a better 'quality' of the translated text. I would also say that there are a lot of common keywords that we use in everyday applications - for example 'email' or 'home page'. Using this tool / service to translate the resource files save time and you could always review the resource files and tidy up the discrepancies afterwards. It also makes testing your multi-lingual applications a breeze. Every time you add new text to your default resource file, just run the tool and it will regenerate the resources files in the languages of your choice.

There are three files that are needed to run this tool - a T4 file, the DLL that does the translation and the config file that contains the languages to translate into - and that's it. All you need to do is drop the 3 files into your App_GlobalResources folder on your MVC application/Webforms application and you are ready to go.

Let's run through a simple example. If you haven't already you will need to sign up for a BING appId. I have also written a previous article on how to use the Microsoft Translator API. You will need to add the BING appId to the config.xml in order for your translations to go through. Next, drop the three files in the App_GlobalResources folder on your Web application.

Next, you are going to need to include the T4 file in your application. Click the "Show all files" button in your Solution Explorer in Visual Studio.

If you expand your App_GlobalResources folder you will notice that there is a TT4 file called "TranslateResources.tt". Right click and choose "Include in project". Once it is included in your project right click the file and choose "Run custom tool".

It's that easy - the T4 file will run in the background and translate your resource files. If you look in the App_GlobalResources folder, you will notice that it has been translated into the languages configured in the config.xml file!

TT4 files are a great way to run custom tasks in Visual Studio. For more information on T4 code generation please check this out.

If you would like to download just the three files that are needed to get translating - click here, else if you would like to download the source and play around - click here