Blue Theme Orange Theme Green Theme Red Theme
 
Safari Books Online
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
ASP.Net 4 Hosting is here
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » ASP.NET and Web » How to build Multi-Language Web Sites with ASP.NET 2.0 and VB.NET 2005

How to build Multi-Language Web Sites with ASP.NET 2.0 and VB.NET 2005


In this article, we will explore the necessary details for working with resources in ASP.NET applications and for creating international ASP.NET applications based on embedded resources and the integrated localization support.

Author Rank:
Total page views :  130752
Total downloads :  2013
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
LocalizationDemo.zip
 
Become a Sponsor

Introduction:

In order to reach international markets through the Internet, supporting different cultures through our applications is essential for being successful. The .NET Framework 2.0 as well as 1.x comes with an integrated infrastructure for creating international applications. Basically, the CLR supports a mechanism for packaging and deploying resources with any type of application. The CLR and the base class library of the .NET Framework come with several classes for managing and accessing resources in applications. These classes are located in the System.Resources and System.Globalization namespaces. Here we will explore the necessary details for working with resources in ASP.NET applications and for creating international ASP.NET applications based on embedded resources and the integrated localization support.

Assumptions:

This article assumes that you already know how to build web forms and to use controls and validation controls.

Localization and resource files:

Localization support in .Net Framework 2.0 in general and in ASP.Net 2.0 specifically become much more easier and brings fun during localization process. Usually resources are created for every culture the application should support. More specifically, each Web Form -Page- in your web site should have a resources for every culture -language- it should support. For example:

If you have a web form with name default.aspx and your web site support English, German and Arabic, then you should have 3 resource files for each culture. The CLR defines a behavior for finding culture-specific resources. With that said, every set of resources has to define a base name that is specified through the first part of the name of the resource file. The second part of the name defines the culture. If the culture portion in the name is not specified, the resources defined in the resource file are used as default resources. For example:

Your page name is default.aspx., you have 3 resource files as mentioned earlier, each one resource file should be named as:

default.aspx.en-US.resx, default.aspx.de-DE.resx and default.aspx.ar-EG.resx. Not here that we are using United States English, German's Gemran, and Egyptian's Arabic. You can use general English or general German or general Arabic like this:

default.aspx.en.resx, default.aspx.de.resx and default.aspx.ar.resx. Also you can use another specific culture like using Switzerland German culture this way: default.aspx.de-CH.resx. For list of supported cultures in .Net Framework return to
MSDN.

How to build Multi-Language Web Sites with ASP.NET 2.0 and Visual Studio.Net 2005:

For localizing a page, just select Tools > Generate Local Resources. Visual Studio then generates a resource file in the App_LocalResources folder, which includes the values for every control of the page currently open in design view.





Visual Studio generates resources for several properties of each control. The resources are always prefixed with the name of the control and postfixed with the name of the property. Visual Studio automatically generates the default resources for the controls of the page only. You must add any further culture-specific resources manually by copying the generated resources and giving them the appropriate name (for example, default.aspx.ar-EG.resx) then translate the values.

The resource generation tool creates an entry for every property that is marked with the [Localizable] attribute in the control. Therefore, if you want to create a custom, localizable control, you have to mark all [Localizable] properties with this attribute.

Copying the resources created previously and renaming this copy to default.aspx.de.resx and default.aspx.ar-EG.resx adds the culture-specific resources for the German and Arabic cultures to the application.

Arabic Resources German Resources

In addition to generating the resource file, Visual Studio has changed the page's source code. For every [Localizable] property of each control placed on the page, it has added a localization expression, as shown in the following code snippet:

<asp:Label ID="LegendFirstname" runat="server" Text="Firstname:" meta:resourcekey="LabelResource1"> </asp:Label>

Localization expressions are identified by the meta:resourceKey attribute of the tag.

The localization expression in the previous code is called implicit localization expression. Implicit localization expressions are something like shortcuts to resource keys included in the embedded resources for a page. They have to adhere to the naming conventions used by Visual Studio for generating the resources. Implicit localization expressions just specify the base resource key for the embedded resource without a property name. Property names are derived from the second part of the name. Note that you can use implicit localization expressions for [Localizable] properties only.

In the attached sample, there are RegularExpressionValidator controls. Although the RegularExpressionValidator control is included in the generated resources, the validation expression property is not included, because it is not marked with the [Localizable] attribute. But the validation of both the birth date and the annual salary has to happen based on the culture settings of the user browsing to the page, because US visitors want to add their birth date in the format they are used to (and the same goes for Germans and Arabs or any other visitors).

Therefore, you need to do some additional work before you are finished localizing the application. Basically, two ways for localizing the validation of those two text fields are available. The first one is to automatically generate the regular expression for the validation based on the CultureInfo object created for the user's culture. The second approach is to add an entry to the embedded resources for the validation expression. But we will go for the second approach to discuss how explicit localization expressions work. First, you have to add two new entries, containing the regular expression for validating the user's birth date and annual salary input, to the embedded resources. Afterward, you need to change the definition of those controls as follows (assuming that the resource entries are called RegularExpressionValidatorResource1.Validation and RegularExpressionValidator-Resource2.Validation):

<asp:RegularExpressionValidator ControlToValidate="BirthdateText" ErrorMessage="Invalid date!!" ID="RegularExpressionValidator1" runat="server" ValidationExpression= '<%$ Resources: RegularExpressionValidatorResource1.Validation %>' meta:resourcekey="RegularExpressionValidatorResource1" />

You can probably see that the previous validator still contains some static text-in this case ErrorMessage. Don't worry about that. Because the validation control has a meta:resourcekey, the control will ignore the static text, and the runtime will get its data from the generated resources. As soon as a control has such a meta:resourcekey attribute, it ignores static text and reads all information from embedded, localized resources. In the case of the ValidationExpression, you have to use explicit localization expressions, because automatic localization is not provided for this property. The general format for explicit localization expressions follows this syntax:

<%$ Resources: [ApplicationKey, ] ResourceKey %>

The application key identifies shared application resources and therefore can be omitted when accessing local resources.

The following screen shot shows that localized properties are marked with special icons in the Properties window. The localization expressions themselves leverage the new expression engine included with ASP.NET 2.0.

Sharing Resources Between Pages:

Generating local resources for a single page might lead to a duplication of resource strings or other resource information. Therefore, it is definitely useful to share resources between pages through global resources. Global resources are placed in the App_GlobalResources folder. One good use for it for identifying system messages for each culture, or text direction setting, or common validation expressions for regular expression validators. For example, the validation expressions for date formats and number formats used earlier are definitely good candidates to be reused in several pages of the application, it is useful to put them into a global resource file. For this purpose you just need to add a new, global resource file and then add the values to those resources.

Now you have to adopt the explicit localization expression for the two validation controls. For this purpose you have to change the name and add the ApplicationKey parameter. Because we already named the global resource file ValidationResources.resx previously, this will be the value for the ApplicationKey property (without the .resx extension).

<asp:RegularExpressionValidator ControlToValidate="BirthdateText" ErrorMessage="Invalid date!!" ID="RegularExpressionValidator1" runat="server"
ValidationExpression='<%$ Resources:ValidationResources, DateFormat %>' meta:resourcekey="RegularExpressionValidatorResource1" />

Also you may need a way to specify the text direction in international applications, because some cultures read from left to right and others read from right to left. You can use a couple of controls in ASP.NET, such as the Panel control and the WebPart control, to deal with this. Therefore, it makes sense to define a property in either the global resources or the local resources for the text direction and to use explicit localization expressions for setting the direction property of these controls. For setting this property directly in the root element of your HTML file, you must use the runat="server" attribute to the <html> tag itself, and then you can apply explicit localization expressions to it, as shown in the following code excerpt:

<html runat="server" dir='<%$ Resources:ValidationResources, TextDirection %>' >

Setting Page Culture:

To set the page culture, you can set the Culture Property of the page and UICulture to use specific culture or set it to Auto to automatically Client Browser Language. Also you can make a default culture for your application by adding the following code to your web.config under system.web tag:

<globalization enableClientBasedCulture="true" culture="ar-EG" uiCulture="ar-EG"/>

Or switching the culture programmatically by overriding Page.InitializeCulture Method:

Protected Overrides Sub InitializeCulture()

Dim _culture As String = Request.Form("cmbCulture")
'Use this
If (String.IsNullOrEmpty(Culture)) Then

culture = "Auto"
Me.UICulture = _culture
Me.Culture = _culture

End If
'OR This
If (_culture <> "Auto") Then

Dim ci As New System.Globalization.CultureInfo(Culture)
System.Threading.Thread.CurrentThread.CurrentCulture = ci
System.Threading.Thread.CurrentThread.CurrentUICulture = ci

End If
MyBase.InitializeCulture()

End Sub

The above code is using a DropDownList with ID cmbCulture.

One Important thing about the InitializeCulture method, it is called very early in the page life cycle, before controls are created or properties are set for the page. Therefore, to read values that are passed to the page from controls, you must get them directly from the request using the Form collection. Just as we did above.

This is all, test it and have fun with localization support of ASP.NET 2.0.

References:


Login to add your contents and source code to this article
 About the author
 
Muhammad Mosa
Muhammad M. Mosa Soliman: Software Engineer, graduated from the Faculty of Computers & Information Systems year 2003-Ain Shams University- in Cairo. Working with Microsoft .NET technology since early beta releases. Main experiance based on ASP.NET, SharePoint Portal 2003 & SQL Server. Worked as trainer for Microsoft .NET for 2 years in Cairo. Likes to read about new technologies and self-learning. Extremly Hard worker when motivated. MCT MCSD.NET MCTS: .Net 2.0 Web/Windows Applications MCPD: Enterprise Application Developer MCTS: WSS 3.0 & MOSS 2007 Config
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
SQL and .NET performance profiling in one place
Investigate SQL and .NET code side-by-side with ANTS Performance Profiler 6, so you can see which is causing the problem without switching tools.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from DevExpress - Absolutely Free-of-Charge without any royalties or distribution costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin edit, tab control and so much more!

DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
Localization by Abdul On March 20, 2006

 

Hi bro

            How do we Localize the error messages or information Boxes ..Can u please Help

    

Reply | Email | Delete | Modify | 
Re: Localization by Muhammad On March 22, 2006

You should create a global resource file, in App_GlobalResources folder, just as the one in the sample.
Then put all your messages in these global resource files. of cource there is global resources for each language.

Review the attached sample to see how we are using global resources to change language direction. or to set common validation pattern for each language used in Regulare Expression Validation controls

Reply | Email | Delete | Modify | 
User Input by Mandar On March 21, 2006

?i Friend,

Thanks for the detailed Article.

Can you pls give some information on inputs taken from user in Multi Lingual web sites.

If user enters text in different languages then how can we validate them in  those languages at run time.

And how .Net can read the text entered in diferent languages.

Is it advisable to restrict English for taking user inputs.

 

Thanks in advance.

Reply | Email | Delete | Modify | 
Re: User Input by Muhammad On March 22, 2006

No, of cource localization for user input is required in many application, so they have to enter information using there own langunage.

Please review the sample. It is mentioned how to set common validation for each language, like date validation.. as in Egypt. we use this pattern dd/MM/yyyy, but in someother countries they use MM/dd/yyyy.

so It is discussed on the sample. Just have a look at the App_GlobalResources Folder.

Hope this helped

Reply | Email | Delete | Modify | 
resources in vb.net by le On April 11, 2006

hi !

now, i must built an program  in vb.net, my respobsible is to change language for this program. I can't change language for column header in listview, nodes  in treeview and toolbar. I can't add images by using resources. Can you help me. Thankyou very much!

Reply | Email | Delete | Modify | 
Re: resources in vb.net by Muhammad On April 13, 2006

Sorry for being late,

I think it need a lettile bit work,

For your listView, use labels as headers, create resource key for each label, and then use the expressions in your property grid to map the resources to these labels

I tree views you need to create resource keys, then use experssions from your property grid to map your reasource key to your tree nodes text properties. each node should have a resource key I think

 

Reply | Email | Delete | Modify | 
vs2002 by toshesh On March 22, 2007
Hi Muhammad, I think you have done a wonderful job up on this site. I just wish there was the same level of detail and accuracy for vs2002 vb.net..... Perhaps I have already checked all the sites trying to tell me how to use it and it simply isn't easy to use???
Reply | Email | Delete | Modify | 
vs2002 by toshesh On March 22, 2007
Hi Muhammad, I think you have done a wonderful job up on this site. I just wish there was the same level of detail and accuracy for vs2002 vb.net..... Perhaps I have already checked all the sites trying to tell me how to use it and it simply isn't easy to use???
Reply | Email | Delete | Modify | 
Re: vs2002 by Muhammad On March 22, 2007

In VS 2002 and .NET 1.x it is not that easy, you'll need to do coding and expose ResourceManager class etc...

I'm sorry that I couldn't help more

Reply | Email | Delete | Modify | 
Re: Re: vs2002 by hamid On September 3, 2007

Dear Mohamed

Thank you very much, it is a nice atricle, my question how to make

sitemap to appear from right to left in arabic.

thanks

 

Reply | Email | Delete | Modify | 
calendar by Imad On May 2, 2007
great article... small problem, if i set the culture to arabic then the calendar turns to hijri calendar, is there a way to change the culture to arabic and still use the solar calendar ?
Reply | Email | Delete | Modify | 
Re: calendar by Muhammad On May 2, 2007

Well, if you for example used culture ar-EG you'll use Georgian calendar, but if you used ar-SA this will configure Hijri Calendar.

Iraq and lebanon use deferent type of Georgian calendar but I don't know its name.

may be your default arabic is KSA. hope this would help

Reply | Email | Delete | Modify | 
Re: Re: calendar by Imad On May 6, 2007
i also figured out another way: if i set the Page.UICulture property, and i do not set the Page.Culture property, everything works fine then,
Reply | Email | Delete | Modify | 
Web User Controls by Rui On May 11, 2007
Hi, Muhammad. Great article, very well explained. Does this work for web user controls? I'm tring but with no sucess... Please help. Thanks
Reply | Email | Delete | Modify | 
Web User Controls by Rui On May 11, 2007
done ;)
Reply | Email | Delete | Modify | 
Validating user input on client side by krishna On May 15, 2007
How to validate user input on clinet side for Multi language websites. Second question is How to render a page with multi language details. We have an place where in user can search other users in the app. the search results should display details of the all the app users. So we have to render page based on the user language in user object.
Reply | Email | Delete | Modify | 
Editing Resource Files after publishing in ASP.NET 2.0 ? by NiNe On November 27, 2007
dear all i created my control site to be mutlilanguage when i publish my web site to the server i didn't find App_GlobalResources folder on the server , i found every thing converted to assembly files so i can't edit these msgs in the resouse file . i want the client to edit these msgs as he wants and edit these resx file through notepad. i found this article but this not help me http://geekswithblogs.net/vivek/archive/2006/12/14/101119.aspx plz any help coz this problem made me so mad coz i'm in final step before lunching my 1st release from my CMS i'm using VS2005 C# ASP.NET2.0 any more info u need ask me plz Thanks
Reply | Email | Delete | Modify | 
Insert data yn MS SQL Server in different languages by Gabriela On January 14, 2008
Hi Muhammad, I have an Access database which contain tables with data in different languages: German, Ukranian, Russian, etc. I've prepared a program in VB.Net 2005 to read this data and copy to a MS SQL server. All is ok except when I insert the data in Ukranian and Russian, it stores "?????????" these symbols instead of the right data. Could you help me with this please? Thanks in advance. My email is glcis@yahoo.com
Reply | Email | Delete | Modify | 
Re: Insert data yn MS SQL Server in different languages by Muhammad On February 12, 2008
hey Gaby, I've faced such issue before, but actually I do not recall how exactly I resolved this issue. I'll contact you once I found it. and sorry for my late response
Reply | Email | Delete | Modify | 
Controls by Eltigani On December 27, 2008
it's really beneficial.. I've a small question. I'm localizing to Arabic and I've to change controls(labels, textboxes) positions. So, how can I? Thanks,
Reply | Email | Delete | Modify | 
Efficiency by satu On March 23, 2009
Hi Mosa. 1st, i would like to say this article is very good. detailed and the example also simple and easy to understand. i know many of programmer helped by reading this. i got question. to think that if i have 10 pages. 3 languages. then i need to make 30 pages of resx , isnt it ? and each page controls need to be inserted with "meta" ? is there anyway to avoid this ? cant we just create one global resx for each language ? i noticed that the direction in this sample is taking from globalresources. is it proper to practice that ? thanks for your reply.
Reply | Email | Delete | Modify | 
Desktop application in C Sharp in English and Spanish by DMendez On June 9, 2009
Hello,
I want to ask a big issue I have right now, it is with a desktop application in C Sharp, the original language is English, I work on localization into Spanish, and right now the application works on PCs which have the Operating System in Spanish, however in PCs with OS in English, the application turns into English; users need the Application to run in Spanish. 
The application takes the Culture with this piece of code:
System.Threading.Thread.CurrentThread.CurrentCulture

How should we do to obtain the Application in Spanish eventhough the Operating System is in English?

Thank you for your suggestions,
DMendez
Reply | Email | Delete | Modify | 
great tut by Jonathan On December 28, 2009
thanks for great tut. it's usefull
Reply | Email | Delete | Modify | 
Thanks! by mr On July 27, 2010
Thank you very much for this tutorial.
Made me understand better how everything works!
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2010.8.14
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.