Simple

Image | CC0 pixabay

Simple Language Selection for One Pager

When building a One Pager with Bootstrap, I sometimes need to implement a simple language switcher. I usually do this with PHP and have the index.php include head, header, content and footer according to the selected language. This approach has a significant advantage regarding design: I can set different languages depending on text length and make them all pretty independently.

Let’s assume we build our One-Pager in three languages: German, French and English with English being the default language. This is what you want to include at the top of your index.php.

Explanation

The first thing we need to check is if the user has just switched languages with a click on the appropriate button or link. If french was clicked we would load the page with a GET-parameter ?fr, telling us that the french version has been requested. We can now write the choice to a cookie for later use and display everything in french.

If there has not been a GET-Parameter the user did not actively chose the language this time, but maybe he/she did before and we have that stored in a cookie already. If this is the case, we just take that language to display our page.

If neither of this has happened, we ask the user’s browser which language it accepts per configuration, and try to set our cookie and display-language accordingly.