Reputation: 31
I have a multilingual site with an 'index.html' sitting on example.com domain. This index.html should have some redirection code so when users go to 'example.com' they get redirected to either the spanish or English version of the site.
In its simplest form I'd like the conditional statement to read:
If IP is based in spanish speakers countries redirect to example.com/es else if anywhere else in the world redirect to example.com/en
How might I set this up using PHP or Javascript?
Upvotes: 1
Views: 2967
Reputation: 31
I finally did it making a few modifications to the Stony's code, my server didn't let me to use header because it was a free subdomain hosting site. So I changed by META instead... And it works I tested changing the language configuration of my browser, Check it out and thanks
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch($lang) {
case "es":
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=http://example_site.com\">";
break;
case "es-ES":
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=http://example_site.com\">";
break;
case "es-AR":
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=http://example_site.com\">";
break;
default:
include("index.html");
break;
}
?>
Upvotes: 0
Reputation: 4113
Stony has a great response to doing this via php, and I'd very much recommend that if that's your backend. But, you can also do this on the client with javascript several ways.
First is to get the user's coordinates via the browser using javascript. To do this, though, your users will have to allow you to view their location. You can do this pretty simply:
let userLocationData = {};
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(function(position){
userLocationData.lat = position.coords.latitude;
userLocationData.long = position.coords.longitude;
})
}
Of course, this is just the lat/long. You'll then need to use some api to get better data from that. I use GeoNames. Now again, this depends on if you're doing this on the client or server, but I'll give you an example with Meteor's HTTP service, though you could do this with ajax or express or anything.
let locationResults = HTTP.call('GET',`http://api.geonames.org/findNearbyPostalCodesJSON?formatted=true&lat=${userLocationData.lat}&lng=${userLocationData.long}&username=YOURUSERNAMEHERE`;
This will return an array of objects. So you could just do something like this:
userLocationData.country = locationResults[0].countryCode;
You can visit this url to see what that data might look like. http://api.geonames.org/findNearbyPostalCodesJSON?formatted=true&lat=40.987148&lng=-90.0803689&username=thatgibbyguy
Now, you have their location and country. But can you tell language from that? I'd say no. You should probably just check for their language:
userLocationData.language = navigator.language;
That's going to use the user's own settings for language that you can use immediately. From there you reroute pretty easily:
window.location.replace(`http://yoursite.com/${userLocationData.language}`/);
Upvotes: 0
Reputation: 1146
If you want to use IP address to determinate user location, you should use special databases such as GeoIP2
, MaxMind
, etc. Usually, they are not free, so you should dig this area to understand what you can use in this situation.
There's also another, less accurate but simple way: HTTP headers. Modern browsers send specific set of user locales to show what language(s) visitor prefer.
Keep in mind that browsers usually send list of accept locales (languages): ru-ru,ru;q=0.8,en-us;q=0.6,en;q=0.4
. Most of answers in the Internet ignore this issue and use only the first one option (or even only first two chars in the worst case).
For instance, you may notice that @Stony even suggested you use switch
operator for the whole string which won't work in 99% cases because $_SERVER['HTTP_ACCEPT_LANGUAGE']
usually contains much more complex string than de-DE
or fr-EN
.
Upvotes: 0
Reputation: 27325
What you try is not a good idea because then you have to check the ip on every request and that can cause some useless timeout until the user is redirected to the site. And you need a service to check the URL which can cost.
Normally you use the browser language to redirect. For this your browser sends a header with the language.
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
Then you can check the language and redirect to your site you want.
switch($lang) {
case "de-DE":
case "fr-EN":
header("Location: http://www.example.com/$lang/index.html");
break;
default:
header("Location: http://www.example.com/en-GB/index.html");
break;
}
You can try something like this.
Upvotes: 1