You can use some features.
When the browser has access to geolocation
You can use javascript to get the request geolocation. However, you will have to know that longitude A and latitude B, is part of country X. I believe this will generate you some work:
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var la = position.coords.latitude;
var lo = position.coords.longitude; console.log([la, lo]);
});
}
Rely on the ip that made the request
There are companies that give you the exact location of the request based on ip, for example, the company: https://www.maxmind.com/
Deliver what the user wants to see (as the staff usually does)
One of the ways to identify which language the user whether receive the content of the post (the answer) is evaluating the variable Accept-Language
that comes in the request.
For example, in php it would be:
<?php
echo $_SERVER['HTTP_ACCEPT_LANGUAGE'];
// "en-US,en;q=0.8"
In this case, the user accepts (wishes to) receive the content in en-US
. So, you know exactly where to redirect a post that comes from meusite.com/
for meusite.com/en
for example.
This is the most common way of identifying "where the user comes from", because the user usually configures what he wants to receive according to the place he is: if he is in the United States - en-US, if he is in Brazil - en-BR. However, this is not a rule. Some users can configure their browsers en-US
, be in Brazil and ignore all other languages. So this way of identifying the language is not based on where the user is, but rather what the user wants to receive - which is much better in my understanding.
If the user is Japanese and is in Brazil, the ideal is to redirect him to meusite.com/jp
, whereas this user will be more comfortable with his native language.
To redirect in PHP
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
header("Location: http://www.meusite.com/$lang" ) ;
Alexander the
en
means "I prefer English", theen-US
means I accept any English language from the United States and theq=0.8
is called the relative quality factor (relative quality factor), and represents the estimation of user preference. More details on: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4– anmaia
First: you don’t know how to express very well what you want. Have you seen how many questions you have within your question? Every question mark is a question. Second, I am not here to teach you anything. Every answer is a help. I am not your teacher! You should pay more attention to the comments you make and try to avoid being less rude to those who try to help you with a problem.
– anmaia
The answer is great, @Alexandrelopes; super complete, thanks Maia! If what you want is just copy/Paste, then the option to buy a script was very wise.
– brasofilo
Now yes! Thank you!!!!!!!!!!!!!!!!!!!!!!!!!! Got better than the script! : D
– Alexandre Lopes
Yes. The final code is in trouble. I put single quotes and should be double quotes to concatenate the $lang variable with the string. Fixed.
– anmaia
Now went to
meusite.com/pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
should only be up to the first comma.– Alexandre Lopes
Then just treat the $lang string and get only the part you want. For example:
$lang = split(",", $_SERVER['HTTP_ACCEPT_LANGUAGE'])[0];
– anmaia
This code above is wrong. You are a Genius!!!! and if I only want what is inside the second comma, in case:
pt
? Sorry for so many questions. : 3– Alexandre Lopes
Use the next index. In this case, change the index
[0]
for[1]
. If you don’t know how to handle string and handle array`s, I suggest you take a look at: http://www.php.net/manual/en/book.strings.php and http://www.php.net//manualpt_BR/book.array.php. It will help you to understand php better.– anmaia
The more the code is wrong, Dreamweaver says it’s wrong, and when I open in the browser is only a white screen.
– Alexandre Lopes
That part that’s wrong:
$lang = split(",", $_SERVER['HTTP_ACCEPT_LANGUAGE'])[0];
– Alexandre Lopes