Wednesday, October 10, 2007

Moving to .NET : Multilanguage support

Best MultiLanguage Example

The best example no doubt is the one provided by Microsoft :

The rest as found in code project are some singalimgam saying and quoting each other and giving half-baked solution.

Multilanguage in .NET (C#)
You will meet a load of problems if you dont follow these steps.
Basically, u need to create Satellite (*.dlls) that contains the resource for each of the language. These dlls are stored in the local directory where the .EXE will be running.

The filenames are language-culture shortcut names to use. These are not just alias, it must be EXACT.
Then make sure your ASSEMBLY name and NAMESPACE is same. This is to avoid asking yourself funny questions later on like, what is the resource name to load. You can always rename the *.exe output yourself later on and it will work.

Your code will be put before the InitializeComponent:

Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(“en-US”); // example, or “” for neutral
MyManager = new System.Resources.ResourceManager(“APpopup.Resources.MyResource”, System.Reflection.Assembly.GetExecutingAssembly());
string str = MyManager.GetString(“form2.renew”);

On your C# projects you have to create 2 additional directories, 1 for resource (the resx files) and the other for satellites. You can do this using console but heck its a mess, btter use the IDE. You create this folder by :

1. Right click on your solution on the solution explorer, add new project, select VC++, empty project.Name your project “Resource” for example.
2. Edit the new project properties, set the type to “Utility”. This will get rid of the C++/Linker etc tabs which u wont be using.
3. Set the files need to clean up (eg if resources, *.resources)
4. Add the *.resx files here. You need the names to be like this. If your namespace, assembly is : APpopup. You might want to call it : APpopup.MyResource.resx for the main one. For english-USA (en-US), you call it : APpopup.MyResource.en-US.resx etc.
5. Under the custom build, command line : resgen.exe “$(InputPath)”

Under the output path : $(InputName).resources
Set the dependency of your main project to “Resource” (the above project) so that it will always build that one first before building ur main project.
Now do the same for Satellite folder (Step 1-3). Except you dont have any .resources files to add here. So you need to build the “Resource” project up there first. Now add all the “*.resources” to this project.

Now one more VERY important STEP.On the custom build, command line :
mkdir ../windowsapplication1/bin/$(OutDir)/en-US al.exe /culture:en-US /out:”../windowsapplication1/bin/$(OutDir)/en-US/APpopup.Resources.dll” /embed:”$(InputPath)”,”APpopup.Resources.$(InputFileName)” /template:”../windowsapplication1/obj/$(IntDir)/APpopup.exe” Output : ../windowsapplication1/bin/$(OutDir)/en-US/APpopup.Resources.dll

Create a “Resources” folder directly under your main project (not your solution which u already have created Resources Project). Under this folder put in the neutral resource, MyResource.resx or MyResource.resource. Without this you always get a "tut-tut-tut-censored (these are vulgar curses..LOL)" error on your application when your culture info is neutral : CultureInfo(”")

There, thats all you need to do.

No comments:

Post a Comment