Posts filed under ‘.net 2.0’
In my previous article – Localization (a brief introduction) – I gave a very brief overview about how localization can be done, what would be the list of things to be taken into consideration and had given a very small example to how this can be done at the beginners level.
In this article I would like to move on to the intermediary level… Now, that we have identified the localizable entities of the application and how the resource information has to be stored. Let us progress onto
1. Naming Convention
2. How to create resource files
3. Where to store the resource files
4. Retrieving resource information
I have also given a list of steps to create satellite assembly.
I. Naming convention –
The naming convention of .resources file (the binary form of .resx file) is dependent on the namespace of the application.
And the naming convention of a satellite assembly depends upon the assembly name of the application.
basetype.culture.resx – is the naming convention for .resx files.
Example: MyResource.de-DE.resx – this will indicate that this resource file will be used if the culture is set to German(Germany), where “MyResouce” is the basetype and “de-DE” is the culture… (msdn link to CultureInfo class ).
Let us suppose that the namespace of your application is “Chk1.Test1.TestLocalization” and the assembly name has been set as “Test” . The .resources file should be named “Chk1.Test1.TestLocalization.MyResource.de-DE.resources” and the satellite assembly – “Test.resources.dll”
II. Steps to create the resource files–
· Add a default resource file (Let it be – MyResource.resx). For this file the culture needn’t be specified. Add the keys and their respective values. Now, this is the information which will be used by the browser in case the resource files for the set culture is missing.
· Create resource files for the required culture.See to it that the naming convention is followed. Let me assume that I require resources for ja (japanese) and de(German) culture. The name of the resource files should be – MyResource.ja.resx and MyResource.de.resx.
Note 1: One can alternately make use of the option “Generate Local Resource” which has been provided in Tools menu. But, in case of an application with many pages this will not be a very viable option.
Note 2: For the web pages to make use of these resource files based on the UI culture of the browser, the attributes Culture=”auto” and UICulture=”auto” has to be set in the page directive of all the pages being used.
III. Storing resource files –
All the resource files can be stored in the application itself. But, as the number of pages in your application increases, the number of resource files also increases.
Let us assume that you have three resource files and that your application has to be localized into four languages(en-GB, de-DE, fr, ja). The number of resource files now becomes 3×4 = 12 files!
A better and recommended way is to make use of satellite assemblies, i.e the hub and spoke model. Here, the hub constitutes of the main assembly and the default resource; and the spokes are the different satellite assemblies, one for each language.
IV. Retrieving information from the resource files –
You can internationalize your web application by either making use of the default .resx file or Resource Manager.
ForExample, let us consider that your MyResource.resx file contains a keyword – “Button1.Text”
String str = MyResource[“Button1.Text”];
ResourceManager rm = new ResourceManager(“MyApp.MyResource”, Assembly.GetExecutingAssembly());
String str = rm.GetString(“Button1.Text”);
String str = rm.GetObject(“Button1.Text”); // useful in case the value is not an string.
These above steps retrieve the information based on the culture selected.
Note: Do check this link to know more about how the hub and spoke model for packaging and deploying resourcesuses a fallback process to locate appropriate resources.
V. Steps to create satellite assemblies :
Step 1: Create the required culture’s .resx file –
Let us create resource files for Japanese culture – MyResource.ja.resx
Step 2: Create .resources file (which will have binary format) –
Use ResGen.exe to create the bin file. ResGen can be found in – \Microsoft Visual Studio 8\SDK\v2.0\Bin
Syntax – ResGen.exe basetype.resx AppNamespace.basetype.resources
Example – ResGen.exe MyResource.ja.resx MyApp.MyResource.ja.resources
In this case MyApp is the namespace of the application. In cases where in your app has namespace MyApp.App1.App2 also “MyApp” keyword can be used.
Step 3: Use Al.exe to create the satellite assemblies –
Al.exe can be found in – \Microsoft.NET\Framework\v2.0.50727
Al.exe /t:lib /embed:AppNamespace.basetype.resources /culture:culture /out :AppNamespace.resources.dll
- Says you are interested in a .dll.
/embed:AppNamespace.basetype.resources – Embeds the resource to a target name to match the Visual Studio IDE naming structure.
/culture:culture - Identifies the culture in which you are interested.
/out: AppNamespace.resources.dll – Name of the DLL in which you are interested.
Al.exe /t:lib /embed:MyApp.MyResource.ja.resources /culture:ja /out:MyApp.resources.dll
This will create the satellite assembly which is specific to Japanese (ja) locale.
Step 4: Place these satellite assemblies in appropriate location
These satellite assemblies should be placed in the bin\culture folder.
In this case folder named ja should be created in bin folder of the application and the newly created satellite assembly AppNamespace.resources.dll should be placed in it.
The advantages of precompiling a web application is that
1. It ensures faster page-load and
2. provides more security to the code.
Here are few articles which will help in understanding precompilation better and also how to precompile an ASP.NET2.0 web application…
quote – “The primary disadvantages of deployment pre-compilation are that you have to perform these steps prior to deployment and that you can’t change your Website after it has been deployed. If you want to make changes, you have to recompile the Website and redeploy it”
Also chk out this article by Rick Starhl titled – “Watch out with precompiled ASP.NET 2.0 Applications if you have Orcas installed” – http://west-wind.com/WebLog/posts/73663.aspx
I have already mentioned in one of my previous blog articles, I am working on a web application which has been converted from 1.1 to 2.0. And because of the many minor changes made in the 2.0 framework, I keep encountering many exceptions!!!
One such exception was raised while making use of Convert.ChangeType method.
When i tried to convert a string to timespan an InvalidCastException was raised. Reason??? In this framework, the method does not support this type of a conversion….
There is the option of using Parse method or TryParse method in TimeSpan class… (TryParse is a newly added method which is very much similar to the Parse method but this doesn’t throw an exception when the conversion fails).
Another thing to keep in mind while using this method is that it doesn’t handle nullables.