Simple Language Selection for One Pager

Simple
Image | CC0 pixabay

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. 

Share

Related Posts

Time to Update

Maintenance of High Turnover WooCommerce Stores

What to do when plugin updates suddenly cause problems for a high-traffic online store? We were faced with this task when a maintenance interval looked promising at first, but then led to inaccessibility of our client’s high-volume WooCommerce store under load.

WooCommerce SQL

Howto Manipulate WooCommerce Metadata via SQL

With Germanized on board, WooCommerce has several ways to save article numbers. Has the intern spent two days entering the EAN in the wrong field for the 3000 articles in the WooCommerce online shop? Then an SQL superhero has to save the day who can copy content from one field to another in no time.

GET IN TOUCH

Contact.