Introduction
This utility is developed to generate Japanese and French resource files from default resource files of an ASP.NET web page or user control. This utility is for translating resource files using the Google translation API.
Before running this utility, create a folder "conversion" in your c: drive and put all your default resource files (.resx) within the folder.
Background
Couple of days back, my team was working on converting my applications for multiple languages (English, French, and Japanese) to address audiences of different regions. Enabling an ASPX page and user control (.ascx) for multiple languages is very easy, and for doing so, we need to maintain resource files (*.resx) for each and every language, and easily generate resource file(s).
To generate a resource file, open the ASPX (or ASCX) page in Design mode, then go to Tools-->Generate Local resource. Now, we will have a default resource file, like Webform1.aspx.resx. Now we need to create Webform1.aspx.fr.resx for French and Webform1.aspx.ja.resx for Japanese.
For translating any string, go to the Google language translation page: http://translate.google.com/translate_t#.
I own around 40 applications, a lot of dashboards (user controls), and a huge portal application. For me, this would be a very tiresome method for language translation. So, I have developed a console utility for it. You have to create a folder "conversion" under your c drive and put all your default RESX files in it.
Code Discussion
I search for all RESX files in c:\Converion and create Japanese and French RESX files, and each file is translated by calling the Dotranslation
function.
foreach (string file in files)
{
Program _p = new Program();
Console.WriteLine("Conversion of file started: {0} {1}",
file, " to Japanese");
_p.DoTranslation(Google.API.Language.Japanese, file);
Console.WriteLine("Conversion of file completed.");
Console.WriteLine("Conversion of file started: {0} {1}",
file, " to French");
_p.DoTranslation(Google.API.Language.French, file);
Console.WriteLine("Conversion of file completed.");
}
In the DoTranslation
function, I pass the language of type Google.API.Language
and the default resource file. I open a .resx file by using XmlDocument
and read all data nodes. Each data node again gets iterated to get the child nodes. A node value is translated using the Translate
function of the Translate
class of the Google API. I have attached GoogleTranslateAPI.dll, provided by Google-api-for-dotnet.
protected void DoTranslation(Google.API.Language language, string resxFilePath)
{
XmlDocument doc = new XmlDocument();
doc.Load(resxFilePath);
XmlNodeList dataNodes = doc.SelectNodes("//root/data");
using (ResXResourceReader rr = new ResXResourceReader(resxFilePath))
{
string outputFilePath = resxFilePath.Replace("resx", "") +
language.ToString().Substring(0, 2).ToLower() + ".resx";
using (ResXResourceWriter rw = new ResXResourceWriter(outputFilePath))
{
IDictionaryEnumerator di = rr.GetEnumerator();
foreach (DictionaryEntry de in rr)
{
string key = de.Key as string;
string value = de.Value as string;
if (!String.IsNullOrEmpty(key) &&
!String.IsNullOrEmpty(value))
{
string translatedValue = Translator.Translate(value,
Google.API.Language.English, language);
rw.AddResource(key, translatedValue);
}
}
rw.Generate();
}
}
}
Utility screenshot:
Points of Interest
This is a very small utility, but saves a lot of time when a large enterprise web application needs to be converted for multi-lingual support.
History
- Uploaded on April 23, 2009.