ARCHIVED - Change Language Script Examples

Warning This information has been archived because it is outdated and no longer relevant.

Archived Content

Information identified as archived on the Web is for reference, research or recordkeeping purposes. It has not been altered or updated after the date of archiving. Web pages that are archived on the Web are not subject to the Government of Canada Web Standards. As per the Communications Policy of the Government of Canada, you can request alternate formats by contacting us.

Table of Contents

1.0 Purpose

Sample scripts to toggle between French and English web pages.

Note: The scripts are provided as examples only. They can be modified as needed.

2.0 Change Language Script in PERL

# get the URL for the Web page that called this script
$calling_page = $ENV{'HTTP_REFERER'};
# when the browser is Netscape, the referrer can contain the # and anchor name: REMOVE IT! (eg. http://clf-nsi/w3c.htm#clf)

if($calling_page =~ /(.*)\#.*/) {
    # only take the first part up to the #
    $calling_page = $1;}

# ignore any file that is not -eng.htm or -fra.htm and do nothing!

# is this an -eng.htm file?

if($calling_page =~ /-eng\.htm/) {
    # replace the suffix
    $calling_page =~ s/-eng\.htm/-fra\.htm/;
    print "Location: $calling_page\n\n";

# then is this an -fra.htm file?

} elsif($calling_page =~ /-fra\.htm/) {
    # replace the suffix
    $calling_page =~ s/-fra\.htm/-eng\.htm/;
    print "Location: $calling_page\n\n";}

2.1 Link from Web pages - PERL Solution

<a lang="fr" href="/cgi-bin/lang_change.pl" title="Français - Version française de cette page>Français</a>

3.0 Change Language Script in ASP 3.0 for dynamic sites (.asp extension)

Function LangTog-LangBasc()
 Dim path
 Dim positionOfExtension
 Dim currentLanguage
 Dim newLanguage
 Dim positionOfTarget

 path = Request.ServerVariables("PATH_INFO")
 positionOfExtension = instrrev(path,".")
 currentLanguage = mid(path,positionOfExtension-3,3)
 newLanguage = currentLanguage

 If currentLanguage = "eng" then
   newLanguage = "fra"
 ElseIf currentLanguage = "fra" then
   newLanguage = "eng"
 End If

 newPath = mid(path,1,positionOfExtension-4)+newLanguage+mid(path,positionOfExtension)
 'trim off target tag, if we want to, (it is not retained by all browsers anyway)
 positionOfTarget = instrrev(newPath,"#")

 If positionOfTarget > 0 then
  newPath = mid(newPath,1,positionOfTarget-1)
 End If

 LangSwitch = newPath
End Function

3.1 Link from Web pages - ASP Solution

<a href="<%=LangTog-LangBasc()%>" lang="fr" title="Français - Version française de cette page">Français</a>

Note: The function LangTog-LangBasc() must be visible in scope to the calling page. This can be done through the use of include files.

4.0 Change Language Script in ASP 3.0 for static sites (.htm/.html extension)

Option Explicit

Call altLangRedirect(Request.ServerVariables("SERVER_NAME"), Request.ServerVariables("HTTP_REFERER"))

Sub altLangRedirect(serverName, referringURL)

  ' altLangRedirect : Redirects to a new URL based on the referringURL.
  ' If no referer, then redirects to the serverName (root page)
  ' If servername not in referer, then sends to serverName (root page)
  ' ServerName : The server
  ' referringURL : The referring URL hitting this subroutine.

  Dim newURL 'The url to redirect to
  Dim positionOfExtension 'position of last dot in URL
  Dim currentLanguage 'language of referring page
  Dim newLanguage 'language of page to redirect to

  'Use this if we want to trim the query string off
  'Dim positionOfQueryString 'position of "?" in URL
  'Use this if we want to trim the target tag off

  Dim positionOfTarget 'position of "#" in URL

  'Use these if using the parallel directories portion
  'Dim replaceWhat 'What to Replace
  'Dim replaceWith 'what to Replace With
  'By default, we'll set the new URL to the serverName.
  'If nothing further happens, this will default the redirect to the root page,
  'to avoid any errors caused by blank or bad HTTP_REFERER.
  newURL = "http://" & serverName

  'First check to see if referringURL exists, if length > 0 then ok,
  'if not then perhaps REFERER was stripped or altered, and
  'we need to default to avoid an error.

  If Len(Trim(referringURL)) > 0 Then
   'Now, check to make sure that the serverName appears in the referringURL
   'If not, we hit this page unintentionally and need to default to avoid an error.

   If Instr(referringURL, serverName) > 0 Then
    'Use this if there is always an eng or fra before the last dot.
    positionOfExtension = instrrev(referringURL,".")
    'This would fail if there is an e or f before the dot that does not really indicate language.
    currentLanguage = mid(referringURL,positionOfExtension-3,3)
    newLanguage = currentLanguage

    If currentLanguage = "eng" then
      newLanguage = "fra"
    ElseIf currentLanguage = "fra" then
      newLanguage = "eng"
    End If

    'substitute an eng for an fra or vice versa
    newURL = mid(referringURL,1,positionOfExtension-4)+newLanguage+mid(referringURL,positionOfExtension)

    'trim off target tag, if we want to, (it is not retained by all browsers anyway)

    positionOfTarget = instrrev(newURL,"#")
      If positionOfTarget > 0 then
        newURL = mid(newURL,1,positionOfTarget-1)
      End If

   End If

  End If

  Response.Redirect(newURL)
End Sub