Microsoft KB Archive/315616

= How To Detect a Client Language in an Active Server Pages Page in IIS =

Article ID: 315616

Article Last Modified on 4/21/2006

-

APPLIES TO


 * Microsoft Internet Information Server 3.0
 * Microsoft Internet Information Server 4.0
 * Microsoft Internet Information Services 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:

http://www.microsoft.com/technet/security/prodtech/IIS.mspx





IN THIS TASK
SUMMARY
 * Requirements
 * Detect the Client Language in an ASP Page
 * Test the Sample

REFERENCES



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 &quot;es-mx,fr,de-ch,en-us&quot; 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=&quot;VBSCRIPT&quot; %> <% Option Explicit %> <% Dim userLocale userLocale = request.servervariables(&quot;HTTP_ACCEPT_LANGUAGE&quot;) 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 &quot;Language string: &quot; & userLocale & &quot;&quot;   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, &quot;,&quot;, -1) </li>  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, &quot;de&quot;) <> 0 Then         ' Any kind of German? Response.Write &quot;Guten Tag&quot;              ' Return German greeting Response.End ElseIf InStr(language, &quot;fr&quot;) <> 0 Then     ' Any kind of French? Response.Write &quot;Bonjour&quot;                 ' Return French greeting Response.End ElseIf InStr(language, &quot;es&quot;) <> 0 Then     ' Any kind of Spanish? Response.Write &quot;Hola&quot;                     ' Return Spanish greeting Response.End ElseIf Instr(language,&quot;en-us&quot;) <> 0 Then 'US English? Response.Write &quot;Hello&quot; Response.End End If Next

</li>  Add a delimiter to terminate the VBScript in the ASP page, as follows: %>                   </li> Save the file as Sayhello.asp in the default Web folder on your computer (this is usually C:\Inetpub\Wwwroot).</li></ol>

back to the top

Test the Sample

 * 1) Start Internet Explorer.
 * 2) On the Tools menu, click Internet Options.
 * 3) In the Internet Options dialog box, click the General tab and then click Languages.
 * 4) In the Language Preference dialog box, view the languages that your browser already accepts.
 * 5) Click Add. Select another language from the list, such as Spanish, French, or German, and then click OK.
 * 6) 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.
 * 7) In the Address field in Internet Explorer, type localhost/SayHello.asp.
 * 8) 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.
 * 9) 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

<div class="references_section">