My first impression about software localization can go back to many years ago. A friend of mine had one comment like this: people from English speaking country had advantages over other language speakers, because all major programming languages used only English keywords; that made writing software in other languages like Chinese very hard. I had the feeling that he misunderstood something. But I didn’t know much about resources, satellite dlls either, our discussion ended with something like how fast can a person input Chinese characters…
Anyway, I wrote a small sample last week. It’s a Winform application, which can localize itself. Here are some details.
- Put strings in resource file. (I don’t know any simple way to read hard-coded string from an assembly, neither know how to replace it)
- Write some code to enumerate all the resources
- Find a translation service
- Create translated resource and create an assembly base on that.
Enumerate resource in managed code is very easy. I assume we don't know how many resource files we have in the manifest.
Assembly assembly = Assembly.GetExecutingAssembly();
string manifests = assembly.GetManifestResourceNames();
Stream MStream = new Stream[manifests.Length];
MStream[i] = assembly.GetManifestResourceStream(manifests[i]);
Once you have all the streams, ResourceReader can walk through all the resources.
IResourceReader reader = new ResourceReader(MStream[i]);
IDictionaryEnumerator en = reader.GetEnumerator();
The tricky part of this application is the translation. If you know this website, you can figure out the rest part by yourself. I simply used a WebBrowser control to grab the translation from the website. The code to create culture specific resource is like this
IResourceWriter writer = new ResourceWriter(resFile);
writer.AddResource((string) allKey[i], (string) allTranslation[i]);
To build a satellite dll, I called assembly linker in my code. People who build resource dll manually may end up in frustration if they don’t know the right arguments to pass. ResourceManager will fallback to English if the satellite dll was not found. It is not easy to figure out the real problem even with the help of ILDASM. Anyway here is the one I used in my code.
That should be long enough for my first blog.