Previous Section  < Day Day Up >  Next Section

Recipe 22.19. Using Content Negotiation to Deliver Pages in Different Languages

22.19.1 Problem

Your web site visitors speak a number of different languages, and you would like Apache to recognize the correct language for each visitor and serve up pages in that language.

22.19.2 Solution

Apache has all the tools to make this work on the server side. You need to supply pages translated into whatever languages you wish to serve. Then configure a type map file to point to the different pages. This file must have a .var extension.

On the client side, your visitors need to configure their web browsers to have a language preference.

The default Apache index page is a good model to see how this works (see Figure 22-1 in Recipe 22.2). Find your htdocs directory. This contains all the variations of the default index.html:

/var/www/index.html.ca

/var/www/index.html.cz.iso8859-2

/var/www/index.html.de

/var/www/index.html.dk

/var/www/index.html.ee

/var/www/index.html.el

/var/www/index.html.en

/var/www/index.html.es

/var/www/index.html.et

/var/www/index.html.fr

Now open the /var/www/index.html.var file:

URI: index.html.ca

Content-language: ca

Content-type: text/html

   

URI: index.html.cz.iso8859-2

Content-language: cs

Content-type: text/html;charset=ISO-8859-2

    

URI: index.html.de

Content-language: de

Content-type: text/html

   

URI: index.html.dk

Content-language: da

Content-type: text/html

As you can see, all you need are the filepaths, a Content-language directive specifying the language, and the Content-type: text/html directive for each file.

The last entry in the file should function as a default, in case the Content Negotiation does not work. This should point to a page that has links to your index pages in the various languages:

URI: fallback.html

Content-type: text/html

Finally, this line needs to be uncommented in httpd.conf (which it should be by default):

AddHandler type-map .var

22.19.3 Discussion

Many multilanguage sites also have links to their various language pages on their front pages (for example, see http://httpd.apache.org/docs-2.0).

While Content Negotiation is an official part of the HTTP/1.1 standard, it is not universally supported. Not all web browsers support it, and not all users bother to configure their browsers appropriately. Using Content Negotiation also slows down performance. It's a really nice feature, but if it bogs down your server too much, you may as well stick to plain old links to your different language editions.

Figure 22-2 shows how to configure language preferences in Mozilla.

Figure 22-2. Configuring language preferences in Mozilla


When the user of this browser visits an Apache-served web site that offers multilanguage pages, she will automatically be served the French language pages. This is what Apache sees in the HTTP headers:

Accept-Language: fr; q=1.0, en; q=0.5

If there are no French-language pages, Apache will dish out the second choice, English. If the content negotiation fails, the visitor will get a 406 error:

Not Acceptable

An appropriate representation of the requested resource /foo/index.html could not be found

on this server.  Available variants:

index-en.html

index-fi.html

index-de.html

index-sv.html

Of course, you can create a custom 406 page to suit your needs (see Recipe Recipe 22.16).

22.19.4 See Also

  • RFC 2616, "Hypertext Transfer Protocol—HTTP/1.1"

  • http://localhost/manual/content-negotiation.html

    Previous Section  < Day Day Up >  Next Section