Redirecting visitors to their own language preference based on browser language or IP address is a feature often requested by our TranslatePress users. This can be easily achieved using the Automatic User Language Detection add-on.
Below we’ll go through the reasons for using automatic user language detection as well as how it’s implemented in TranslatePress.
Why Automatic User Language Detection Matters
First-time visitors expect to see the website in a language they understand. With such a short attention span it’s easy to lose users who are not finding the language switcher soon enough. This is why it is important not only to have their language available, but to also point this out to them or even automatically redirect to it.
The way automatic language detection works is that the last visited language is remembered through a cookie, so returning visitors will always visit your website directly in their chosen language.
With the Automatic User Language Detection add-on active, first-time visitors whose browser settings or IP reveal a language different than your site’s default one, will be prompted with a pop-up or a hello bar giving them the option to switch to their automatically detected language.
Alternatively, you can set the functionality up so that your website automatically redirects the user to their language, without a prior notification. However, this could affect your site’s indexing, because of Google’s frequent algorithm changes, hence why we recommend choosing the first option.
How Automatic User Language Detection Works in TranslatePress
The Automatic User Language Detection add-on works out-of-the-box, with no required setup steps.
There is no need to register for IP address language services, because the language recognition based on IP happens locally with the help of GeoLite2 databases from MaxMind.
There are two ways of finding out what languages the visitors speak: either by browser language setting or by IP address. In the community, the browser language is seen as the most reliable information, but it depends on who your targeted audience is in order to decide which works best for your website.
If you want to change the default setting of taking into account the browser language first and then IP address, you can do so with a simple select found in Settings → TranslatePress → Advanced. You can choose to determine language based solely on IP address or solely on browser language, or a combination between these two.
Next, you can choose whether you want your visitors to be notified via a pop-up or hello bar about the language detection, or if you want them to be redirected right away. Then you can move on to customizing the messages to be displayed.
Here’s a short video tutorial that takes you through everything you need to know in order to enable automatic user language detection on your WordPress site:
Hard Redirects vs. Soft Redirects: A Technical Issue
When we use redirects, w e chose not to have any hard redirects in place for several reasons. Hard redirects, meaning those that happen on the server side, simply do not work with caching systems, be it a WordPress plugin, or host caching system. The page served from cache would not take into account the correct language for each visitor, since the code on the server never gets executed.
So how is our add-on solving all of these issues caused by the hard redirects?
The Automatic User Language Detection add-on uses only soft redirects, which happen on the client side. But there’s a problem: finding out the language preference sent by the browsers through the Accept-Language request HTTP header can only be read on the server side. The solution is to make a fast custom ajax request internally to get our needed information, all without using the slow-loading WordPress environment.
A Well-Thought-Out Language Detection Solution
The final result is a versatile and fast solution, that works extremely well with a wide range of environments seen both on server side and on client side.
See how you can take advantage of the Automatic User Language Detection Add-on and other useful features that TranslatePress offers.
Do you have any more questions about automatic user language detection? Let us know in the comments below.
If you found this post helpful, please check out our YouTube channel, where we constantly upload short & easy-to-follow video tutorials. You can also follow us on Facebook and Twitter to be the first to know each time we post.