How to Set a Web Page's Language

Why Set the Language of a Web Page?

Hopefully, you understand that many people with different languages from various nations create websites. If you do not speak Russian, it probably wouldn't do you any good to visit a Russian website. Search engines understand this and use various means like your ip address or the text of your search query to determine what language you are requesting. Therefore, if you typed, "More pictures of stupid cats", it would assume that you wanted English pages about that search query. Once they understand the user's language, they check each web page's language that matches the user's language for the web page's content that matches the user's search terms. If a web page does not specify a language, the search engines attempts to guess or omits the web page from their index.

While it seems easy to say that search engines would be able to determine the page language from your website just by the content alone, it really isn't that easy. Consider what would happen if you had comments on your website, and someone from Mexico wrote some Spanish text (it actually happens a lot with spam). The search engine would see that text on the page and flip out, which could likely lead to your web page being omitted from the results. If you would have specified the language, the search engine would have known the primary language immediately and indexed it accordingly. Setting the language of a web page probably isn't the biggest concern in the world because search engines handle it rather well, but it can prevent them from freaking out when another language appears in a small section of the page.

Setting the Page Language

Much like setting the encoding of a page, you can set the language in the document or in the HTTP headers. Although, it does not particularly matter which one you use, you should always be consistent. It is also worth mentioning that and other internet regulators have proposed a solution for a document with multiple languages. In that situation where a comment was in Spanish on an English web page, you could provide an attribute to an HTML element that contains the Spanish text. This attribute would contain a subtag of the language. The language subtag registry contains a list of values that you can use to select the language for your web page.

Setting the Language in the Document

Having the page language set in the "html" tag basically says that the entire page is using that language. In the following example, we are using the subtag for English, "en".

<html lang="en">

Setting the Language in the HTTP Headers

Since this is a HTTP header, your server configuration file or the server's programming language can modify the language header. Google should be able to return several tutorials to set the page language with various sever programming languages or server configuration files. In the following example, we are setting the page language using the popular PHP server programming language.

<?php header('Content-language: en'); ?>

Note: This code would have to be executed before any text is returned to the client.