In one of my previous tutorial I wrote about how to build a multi-language website. But sometimes you might want to change the design of your website according to the language of your website. In this post I will show you how you can make design changes to your website based on website language.
How does browser identify website language?
There are basically two ways browsers can identity the language of the webpage content: HTTP Content-Language header and HTML lang attribute.
List of all languages code reference can be found here.
If you don’t specify the language then browsers take the page content to not be of any specific language but search engines try their best to guess the language of the content my reading the characters.
Setting webpage language using HTTP response Content-Language header
Setting webpage language using HTML lang attribute
lang attribute can be used to specify the language of the webpage and also specific portion of the webpage.
Specifying the language of whole webpage.
Specifying the language of specific portion of webpage.
<!-- this paragraph content is in english -->
lang attribute overrides the Content-Language header. More on lang attribute.
There was a time when language meta tag was also used to specify the webpage language but in recent times this tag has been expired and is not recognized anymore.
Design based on language using CSS
CSS selector :lang() can be used to style webpage or specific portions based on the language. This selector comes under CSS2 specification and therefore is supported by modern browsers.
//webpage language is french
<!-- overrides content-language and makes the page english -->
/*paragraph elements in english language*/
/*paragraph elements in french language*/
/*all elements in french language*/
<p>This is english text</p>
<!-- overrides the global lang attribute -->
<p lang="fr">Ceci est un texte anglais</p>