Presentation is loading. Please wait.

Presentation is loading. Please wait.

Globalization support in ASP.NET

Similar presentations


Presentation on theme: "Globalization support in ASP.NET"— Presentation transcript:

1 Globalization support in ASP.NET

2 Globalization Globalization is the process of creating an application that meets the needs of users from multiple cultures. This process involves more than just translating the user interface elements of an application into multiple languages—it also includes using the correct currency, date and time format, calendar, writing direction, sorting rules, and other issues. Accommodating these cultural differences in an application is called localization.

3 Localization Localization – providing content in a region specific culture and language. Culture determines date display settings, e.g.: mm-dd-yyyy or dd/mm/yyyy, currency display formats etc.

4 Ways to Globalize Web Applications
There are several different approaches to creating Web applications that support multiple cultures: Detect and redirect Run-time adjustment Satellite assemblies (using Resource Files) – preferred method

5 Detect and Redirect Create a separate Web application for each supported culture, and then detect the user’s culture and redirect the request to the appropriate application.

6 Run-time adjustment Create a single Web application that detects the user’s culture and adjusts output at run time using format specifiers and other tools.

7 Satellite assemblies Create a single Web application that stores culture-dependent strings in resource files that are compiled into satellite assemblies. At run time, detect the user’s culture and load strings from the appropriate assembly.

8 Detecting the User’s Culture
Microsoft ASP.NET uses the Request object’s UserLanguages property to return a list of the user’s language preferences. The first element of the array returned by UserLanguages is the user’s current language. Use that value to create an instance of the CultureInfo class representing the user’s current culture. To get the user’s culture at run time, follow these steps: Get the Request object’s UserLanguages property. Use the returned value with the CultureInfo class to create an object representing the user’s current culture.

9 Detecting the User’s Culture
Dim sLang As String ' Get the user's preferred language. sLang = Request.UserLanguages(0) ' Create a CultureInfo object from it. Dim CurrentCulture As New CultureInfo(sLang) lblCulture.Text = CurrentCulture.EnglishName  & ": & _ CurrentCulture.Name

10 sLang = sLang.Substring(0, 2)
If sLang = "en“ Then ' Use US site. Response.Redirect(" ElseIf sLang = "es“ ' Use Spanish site. Response.Redirect(" ElseIf sLang = "de“ ' Use German site.              Response.Redirect(" End If

11 Setting Culture in Web.config
Use the Web.config file’s globalization element to create a culture-specific Web application. The culture attribute of the globalization element specifies how the Web application deals with various culture-dependent issues, such as dates, currency, and number formatting. <globalization requestEncoding="utf-8" responseEncoding="utf-8"    culture=“en-US“ uiCulture=“en-US" />

12 Setting culture for Web page
Auto detection of culture (default): Page culture="auto" uiculture="auto" %> Explicitly setting culture: Page culture=“ru-RU“ uiculture=“ru-RU" %>

13 Adjusting to Current Culture at Run Time
When you set the culture attribute for a Web application in Web.config, ASP.NET assigns that culture to all the threads running for that Web application. Threads are the basic unit to which the server allocates processor time—ASP.NET maintains multiple threads for a Web application within the aspnet_wp.exe worker process. Using Web.config to set the culture creates a static association between the application and a specific culture. Alternatively, you can set the culture dynamically at run time using the Thread class’s CurrentCulture property

14 Setting Culture at Run Time
Dim sLang As String      ' Get the user's preferred language. sLang = Request.UserLanguages(0) ' Set the thread's culture to match the user culture.       Thread.CurrentThread.CurrentCulture = New CultureInfo(sLang) The preceding code detects the user’s culture and then sets the culture of the current thread to match. ASP.NET will then format date, currency, and numeric strings using the user’s culture

15 Setting Culture at Run Time
Using Page properties Culture and UICulture to set culture: Culture– determines results of culture dependent functions, accepts cultures that define both language and country(region), e.g. en-US, fr-FR but not just: en, fr UICulture – determines which resources are loaded for the page, accepts both neutral and specific cultures e.g.: en, en-US

16 Setting Culture at Run Time
To draw form components with new culture set, Culture setting should be done in InitializeCulture() not in Page_Load() Protected Overrides Sub InitializeCulture() If Not (Request.Form(“ddC") Is Nothing) Then Culture = Request.Form("ddC") UICulture = Request.Form("ddC") MyBase.InitializeCulture() End If End Sub

17 Globalization using Resource Files
Local resources - Implicit localization Uses page specific resource files, stored in App_LocalResources folder Global resources - Explicit localization Uses single resource file, should be used when it is required to access a single resource from multiple pages, stored in App_GlobalResources folder

18 Local resources (Implicit localization)
Should be stored in subfolder App_LocalResources of the folder containing web pages. Resource file should be named using format: PageName<.culture>.resx e.g.: Default.resx Default.en-US.resx Default.ru.resx

19 Creating local resource files
Could be created: Manually Generate using VS.NET tool (preferred method) Create resource files when you complete creation of your page and you will have all components you need on the page

20 Creating local resource files manually
Create App_LocalResources sub folder in the same folder where you have you web page In App_LocalResources create resource files with names matching pattern: PageName<.culture>.resx For every text entry that needs to be translated create a Name/Value in resource file, e.g.: Label1Resource1.Text - Username

21 Creating local resource files manually
In web page add meta:resourcekey attributes to all components that need to be translated <asp:Label ID="Label1" runat="server" meta:resourcekey="Label1Resource1" Text=“Username"></asp:Label>

22 Creating local resource files automatically
All these steps described above could be done by VS.NET automatically: Open web page in design view Go to top menu: Tools -> Generate Local Resources

23 Creating global resources (explicit localization)
Create App_GlobalResources in the root folder of the web site In App_GlobalResources create resource file with any name. For every text entry that needs to be translated create a Name/Value in resource file, e.g.: global.AppTitle – My Web App For every culture you want to support create a resource file with corresponding translations and add culture identifier immediately before the .resx extention, e.g.: myGlobal.ru.resx

24 Associating controls with global resources – step 1

25 Associating controls with global resources – step 2

26 Associating controls with global resources – results
<asp:Label ID="Label5" runat="server" Text="<%$ Resources:translations, title %>"> </asp:Label>

27 Access resource values programmatically
After saving resource file VS.NET creates strongly typed members for every global resource value, e.g.: Label5.Text = Resources.Translations.title

28 References us/library/c6zyy3s9%28v=VS.90%29.aspx ationByVivekTakur.aspx


Download ppt "Globalization support in ASP.NET"

Similar presentations


Ads by Google