Let me brief you about Resource files.
- "aspx.resx" (resource) files: Resource files are xml files, which stores string, file paths (image), which are required to translate in to other languages.
- "App_GlobalResources": You can keep files which need to be accessed throughout the application. So in our application I have kept "TextDirect" & "EmailFormat" which required to access in maximum pages. In short common strings.
- "App_LocalResources": Local resources are associated with the single web pages, In this folder I have kept lables and strings which are specific to particualt aspx page it self.
For Example:
I have "Registration.aspx" page, for this page I had made files as follows:
- Registration.aspx.ar.resx (for arabic content to be stored).
- Registration.aspx.resx (for english content to be stored).
- MulResource.resx
- MulResource.ar.resx
To create a resource file manually:
Make sure that your Web site has a folder in which to store the resource file by doing one of the following:
If you are creating a global resource file, you must have a folder named App_GlobalResources. To create the folder, in Solution Explorer, right-click the name of your Web site, click Add Folder, and then click App_GlobalResources Folder. There can only be one of these folders in an application, and it must be located at the root of the application.
If you are creating a local resource file, you must have a folder named App_LocalResources. To create the folder, in Solution Explorer, right-click the name of your Web site, click Add Folder, and then click App_LocalResources Folder. There can be many of these folders in an application, and they can be located at any level in the application.
To create a resource file, right-click the App_GlobalResources or App_LocalResources folder, and then click Add New Item.
Global resource files must be in the App_GlobalResources folder. If you try to create a .resx file outside of this folder, Visual Web Developer prompts you to create it in the folder.
Glbalization:
It is process by which you can develope application / program so that it can be used by the various regions/cultures. Let us consider that you have made a product called as "Shopping Cart" and you want to sell it to different regions, since it is online say arabic people should able to read the info in Arabic & English people should read it in English. To achive this we have to use something called as localization
Localization:
It is process of using specific regional/cultural info so that your program uses local language.
Culture:
Every region has different language and language is depend on different geographical location.
For example: "ar-EG" ar is the code for arabic language & EG means this language is spoken in Egypt country/region so "ar-EG" sets for Arabic-Egyption. Similarly "en-US" stands for English-United State.
Note: In IE the user can change the culture by going to Internet Options->General->Languages. For this to work, we need to set both the Culture and the UICulture to auto and enableClientBasedCulture = true
Setting Page Culture:
You can set the page culture by two ways:
-
<globalization enableClientBasedCulture="true" culture="ar-EG" uiCulture="ar-EG"/>
-
Setting culture by programmatically.
{
//string culture = Request.Form["ddSelLanguage"];
string culture = Session["Language"].ToString();
if (string.IsNullOrEmpty(culture))
culture = "Auto";
//Use this
UICulture = culture;
Culture = culture;
//OR This
if (culture != "Auto")
{
System.Globalization.CultureInfo MyCltr = new System.Globalization.CultureInfo(culture);
System.Threading.Thread.CurrentThread.CurrentCulture = MyCltr;
System.Threading.Thread.CurrentThread.CurrentUICulture = MyCltr;
}
base.InitializeCulture();
}
Code:
In "Default.aspx" file I have a drop-down by which user can select specific language, i have Arabic and English languages and by default "Auto" is set. I have used Auto-postback property of the drop-down & even you can make use of submit button for the same.
Once you got the selected language you can hold that value in the session as explained in the above code.
Session["Language"] = Request.Form["ddSelLanguage"];
While overriding the InitializeCulture() method you can assign this to the culture variable.
- User registration form.
- Send Email (In which I have also explained that how can we make use of built-in asp.net validation controls).
Now let us have a look at "Registration.aspx" page code.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Registration.aspx.cs" Inherits="Registration" meta:resourcekey="RegResource" %>
This code is preety much similar with the code which get generated normally for any aspx page, only difference is that:
meta:resourcekey="RegResource"
A resourceKey "RegResource.Title" has value "Registration" similarly in the other language. "RegResource.Title" has value, which is directly accessed as shown above.
<html id="Html1" runat="server" dir='<%$ Resources:MulResource, TextDirection %>' xmlns="http://www.w3.org/1999/xhtml">
In the above tag it should have runat="server" attribute or it will give you error use literal runat="server" .
As i have already explained you about the global & local resources, in the above we have used dir='<%$ Resources:MulResource, TextDirection %>' attribute, now let us go little in dept for this, many of you must be aware of "DIR" attribute, it is nothing but the direction or the text in the page by default it is LTR (left to write) to have arabic type of appearance of the page we have to use it as "RTL". In short this act as common for all the pages either LTR or RTL so i have two global resource files as:
- MulResource.resx
- MulResource.ar.resx
I think so far so clear?
Now let us see how we can access the value of different labels in our project.
For example: On the registratio page i have label called as "lblFirstName" and it's values are stored in the corrosponding resource files viz.
- Registration.aspx.resx
- Registration.aspx.ar.resx
No comments:
Post a Comment