How To Detect a Client Language in an Active Server Pages Page in IIS (315616)
The information in this article applies to:
- Microsoft Internet Information Server 3.0
- Microsoft Internet Information Server 4.0
- Microsoft Internet Information Server 5.0
This article was previously published under Q315616 We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 6.0 running on Microsoft Windows Server 2003. IIS 6.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site: SUMMARY
This step-by-step article describes how to use Active Server Pages (ASP) to test the value of HTTP_ACCEPT_LANGUAGE and return a greeting in an appropriate language.
ASP pages generate dynamic content at the Web server and return the content to the Web browser. An ASP page can return different content, depending on the type of browser that made the request. Similarly, an ASP page can return different content that is based on the natural language that is selected in the browser.
You can use ASP to detect the default natural language that is selected at the browser. This information is held in an HTTP header variable named HTTP_ACCEPT_LANGUAGE. For example, the value "es-mx,fr,de-ch,en-us" means that the browser accepts the following languages, in order of preference:
- Spanish (Mexico)
- French (France)
- German (Switzerland)
- English (United States)
back to the top
Requirements
The following items describe the recommended hardware, software, network infrastructure, skills and knowledge, and service packs that you need:
- Microsoft Internet Explorer 3.0 or later
- Basic knowledge of ASP
back to the top
Detect the Client Language in an ASP Page- Open a text editor such as Notepad.
- Paste the following code in the file:
<%@ LANGUAGE="VBSCRIPT" %>
<% Option Explicit %>
<%
Dim userLocale
userLocale = request.servervariables("HTTP_ACCEPT_LANGUAGE")
The <% delimiter marks the start of Visual Basic Scripting Edition (VBScript) code. The code retrieves the HTTP_ACCEPT_LANGUAGE variable from the HTTP header.
- Add the following code to display the HTTP_ACCEPT_LANGUAGE variable in the browser for diagnostic purposes:
Response.Write "Language string: <B>" & userLocale & "</B><BR><BR>"
- Add the following code to split the HTTP_ACCEPT_LANGUAGE variable into an array of substrings. Each substring holds one language choice, such as es-mx:
Dim languages
languages = Split(userLocale, ",", -1)
- Add the following code to test the language choices and return a greeting in German, French, Spanish, or English:
Dim language
For Each language in languages
If InStr(language, "de") <> 0 Then ' Any kind of German?
Response.Write "Guten Tag" ' Return German greeting
Response.End
ElseIf InStr(language, "fr") <> 0 Then ' Any kind of French?
Response.Write "Bonjour" ' Return French greeting
Response.End
ElseIf InStr(language, "es") <> 0 Then ' Any kind of Spanish?
Response.Write "Hola" ' Return Spanish greeting
Response.End
ElseIf Instr(language,"en-us") <> 0 Then 'US English?
Response.Write "Hello"
Response.End
End If
Next
- Add a delimiter to terminate the VBScript in the ASP page, as follows:
%>
- Save the file as Sayhello.asp in the default Web folder on your computer (this is usually C:\Inetpub\Wwwroot).
back to the top
Test the Sample- Start Internet Explorer.
- On the Tools menu, click Internet Options.
- In the Internet Options dialog box, click the General tab and then click Languages.
- In the Language Preference dialog box, view the languages that your browser already accepts.
- Click Add. Select another language from the list, such as Spanish, French, or German, and then click OK.
- In the Language Preference dialog box, select the language that you just added. Click Move Up several times to move the language to the top of the list. This makes this language the preferred language for your browser. Click OK to accept these language preferences.
- In the Address field in Internet Explorer, type localhost/SayHello.asp.
- Verify that the ASP page returns a greeting in your preferred language. Also observe the value of the HTTP_ACCEPT_LANGUAGE variable, which is displayed at the start of the page.
- Select a different language preference in Internet Explorer and press F5 to run the ASP page again. Verify that the ASP page returns an appropriate greeting.
back to the top
REFERENCES
For more information about localization issues with scripting, see the following Microsoft Developer Network (MSDN) Web site:
back to the top
Modification Type: | Major | Last Reviewed: | 4/21/2006 |
---|
Keywords: | kbhowto kbHOWTOmaster KB315616 kbAudDeveloper |
---|
|