MyBB Community Forums

Full Version: How to detect user's language: via IP location or browser?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to personalize the content of the website to its visitors based on what language they speak. I see two ways to do this:

1) locate a visitor's IP. My logic is: if visitors access from IP in Paris, they speak French and would like to read content in French. All I need is to use a geolocation API that is easy to find, like this one so far https://www.abstractapi.com/ip-geolocation-api.

However, if I have visitors from Canada, they can speak English or French or both. How to know which language to use in that case?

So, I'm thinking of option #2.

2) rely on a visitor's browser accept-language header. But I'm wondering how accurate is it? What can be the drawbacks of this approach?

Let me know please how you tailor the language of the content to website visitors.

Thanks!
I think that IP is a bad choice, because of:
- as you said, some countries have multiple official language (Canada, Belgium, Swiss, ...)
- more and more users use VPN
- you'll be quickly limited with geoip providers quotas unless you pay
- it could use too much resources from your server
- probably other negative things Smile

I think the accept-language works well, I was used to use it when working in web-agency. But you must let the ability to user to change his language.
I'd agree with the above - using what the browser sends you will be more reliable than trying to look up the location by IP. I used to use this to select the initial language but then still give the option to change it. IP lookups can often be a bit slow to do too so adds additional page load latency for users.
Thanks for sharing your thoughts! Will proceed with the accept-language and allow users to choose the language.
Your better choice should be detecting browser language, thats the language the user is probably using as per his own choice. This detection is js based:
<script type="text/javascript">
var Lang = navigator.language || navigator.userLanguage; 
</script>


Or you can attempt to detect best available locale based on HTTP "Accept-Language" header
$lang = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
The second option is better than the first one.
Hi Karmadele, for your second option there's a plugin here you might find useful :

Browser-based Language :
https://community.mybb.com/mods.php?action=view&pid=636