Internationalization in ASP.NET
Internationalization in ASP.NET - Globalization and Localization
What is Globalization?
Globalization is defined as the process of developing a program or an application so that it is usable across multiple cultures and regions, irrespective of the language and regional differences.What is Localization?
Localization is the process to create content, input and output data, in a region specific culture and language.
This means translating strings into a particular local language. This covers putting language specific strings in the resource files.
Globalization starts in the main construction phase along with the code development. Localization generally comes later.Quick notes on Internationalization
1) What is a culture or Locale?: A Language in itself is not a culture. Languages also depend upon the geographical location. For e.g.: French is spoken in France as well as Canada. But, Canadian French is quite different from French spoken in France. Thus a culture is a combination of language and location.
For e.g.: fr is the code for French language. Fr-FR means French language in France. So fr specifies only the language whereas fr-FR is the locale. Similarly fr-CA defines another locale implying French language and culture in Canada. If we use only fr, it implies a neutral culture (i.e. location neutral).
2) Basic Approach: For each form in the website, resource files(.resx) will be made to support each language. For example say there is a Form called Default.aspx, and we wish to support 5 languages, we will be making 5 Resource Files to support these cultures.
3) What do Resource Files Contain?: These resource files will contain the labels or messages to be displayed on the form and other forms of static content which we will need to support different cultures. Dynamic content will have to be stored in the database.
4) How are Resource Files Named?: In .NET each resource file is name as FormName.culture.resx, for example in case of a form named "Default.aspx", for English language the Resource File will be named default.aspx.en-US.resx, for German language it will be default.aspx.de-DE.resx and so on..
5) How to define or change the current culture?: There are two properties of CultureInfo class in .NET Framework
CurrentUICulture: It deals with the user interface related setting to be made. This property helps the runtime to load the resource strings from a
specific resource file.
For e.g. Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr");
CurrentCulture: This property is used to set and get the non-user interface settings like the formats of currency, dates etc.
For e.g. Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-CA");
6) Approach to switch cultures: There are two approaches to switch cultures:
1. Using the Browser Settings (Not Recommended): This approach uses the browser settings of the client. For e.g. in IE the user can change the culture by going to Internet Options->General->Languages.
2. Using User Specified Settings: We can give an option for user to specify and change the culture and the language at runtime. This is the recommended approach as sometimes the browser itself may not have the user specific culture set (for example a French tourist might be surfing net in India). Also, sometimes changing Language settings via the browser is blocked.
Note: Culture will be site specific.
7) Resource Files and Satellite Assemblies: All resources files of a specific culture or locale compile into a single satellite assembly. For e.g. All resource files for en-Us will compile into a single satellite assembly. The runtime loads the appropriate assembly for the specified culture or locale.Actual Implementation
Actual implementation is divided into 2 parts.
Part – I : Presentation/Interface Level Changes
Part – II : Database Level ChangesPart – I: Presentation/Interface Level Changes
There is a need to create resource files for each and every user facing pages of the project.
While defining the dos and don'ts of the project, one should take care that in all the User Side pages, we need to use Label Controls to place any static text that may appear on the page.
Step 1: Loading the appropriate culture based on the Site
protected override void OnLoad(System.EventArgs e)
{
string strCulture;
//To set the culture, same as that of the Site.
//The Session["LanguageId"] will contain the LanguageId of the Site which is specified in the URL
//Session["LanguageId"] is set through the SetSiteSession() function which is called in the preint event.
if (Session["LanguageId"] != null)
{
Language objLanguage = new Language();
objLanguage.Load(Convert.ToInt32(Session["LanguageId"].ToString()));
strCulture = objLanguage.Culture;
}
else
{
strCulture = "en-us";
}
Session["Culture"] = strCulture;
Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
base.OnLoad(e);
}
Step 2: Creation of files in the App_LocalResources folder.
- Note the file naming conventions followed for .resx files.
- We have created files for English and Spanish (Mexico).
- The resource files without a culture name act as fall back files, such that if no culture is defined, these files will be loaded.
- The keys defined in the above .resx files are local to the respective pages, i.e. the Keys defined in the Login.aspx.resx/login.aspx.es-mx.resx are available only in the Login page.
- It is necessary to add the App_LocalResources folder at the same level where the pages are placed.
- There can be multiple App_LocalResources folder in the application structure.
Step 3: Creation of files in the App_GlobalResources folder.
- Note the file naming conventions followed for .resx files.
- These files define the keys that are global and that may be used in any of the pages in the application.
Step 4: Loading the values of resource keys in Labels
App_LocalResources
lblEmail.Text = GetLocalResourceObject("lblEmail").ToString();
lblPassword.Text = GetLocalResourceObject("lblPassword").ToString();
btnLogin.Text = GetLocalResourceObject("btnLogin").ToString();
App_GlobalResources
CommonMessage.LoginFailed
The same process can also be through Implicit and Explicit Localization as follows:
Implicit Localization
...asp:Label id=lblWelcome meta:resourcekey="lblWelcome" Text="Welcome" runat="server" ...
Explicit Localization
...asp:Label id=lblWelcome Text="<%$Resources:TestSiteResources, Welcome %>" runat="server"...Part – II: Database Level Changes
Step 1: Creation of Language Table
Step 2: Changing DB field type varchar to nvarchar, text to ntext
Step 3: Using appropriate datatypes in SP and use of N'
ALTER PROCEDURE [dbo].[Member]
(
@UserName NVARCHAR(400) = '',
@Email NVARCHAR(400) = ''
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @SqlString NVARCHAR(4000)
SET @SqlString = 'SELECT * FROM [dbo].[MemberLogin]
INNER JOIN Member on MemberLogin.[MemberID] = Member.[MemberID] WHERE '
IF @UserName <> '' AND @Email = ''
BEGIN
SET @SqlString = @SqlString + ' MemberLogin.[UserName]= N''' + @UserName + ''''
END
EXECUTE sp_executesql @SqlString
END
Note the following points in the above stored procedure.
1) Parameters @UserName and @Email are declared as nvarchar
2) The string @SqlString that stores the query is declared as nvarchar
3) N' is used to mark the parameter as Unicode. The final where clause that will be generated is as follows :
WHERE
MemberLogin.[UserName]= N'??ML'

Hai,
Good article on globalization and localization...there is always some confusion on me regarding the internationalization, globalization and localization...
this helped me in good way to clearly understand all...these concepts.