Automatically detect location and set language

Discussion in 'Programming' started by chewie49, Nov 2, 2007.

  1. #1
    Hello!

    I wanto know How Do i do that when visitors from Spain enter in my website, they see the text in Spanish, bu when visitors from Portugal enter in my web, they see the text in portugues.

    I have entered in many international websites and they automatically open in portugues if I am in Portugal or in Spanish if I am in Spain.

    Any comments?

    Regards,

    Chewie.
     
    chewie49, Nov 2, 2007 IP
  2. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Normally it is not based on where you are but what language you have set as default on your web browser.

    With .Net it is simply Request.UserLanguages(0) to get the language back
     
    AstarothSolutions, Nov 2, 2007 IP
  3. goliathus

    goliathus Peon

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    how about to tell us in which language you need that?

    Here is a piece of code PHP, but based on browser language. So if I will be Spanish with english browser, I will get it in english.

    $checklang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    if(substr($checklang, 0, 2)=="es"){
    $lang = "spanish";
    }elseif(substr($checklang, 0, 2)=="en"){
    $lang = "english";
    }

    etc...

    substr for 2 first chars is only bcoz there are spanish in Peru, Mexico, Spain, etc, as well as English speaking poeple in USA, UK, ... all those have different language codes (e.g. en-gb , en-us, en-ca, ...) some codes are here: webmasterworld.com/forum24/320.htm
     
    goliathus, Nov 4, 2007 IP
  4. chewie49

    chewie49 Well-Known Member

    Messages:
    305
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    Hi! Thanks!

    I need it in Spanish (when people enter from Spain) and in Portuguese (When they enter from Portugal)


    In your code Goliathus, where I do set to what page redirect the visitors? I mean, I was thinking when the page detects you are from spain, automatically takes you to the root mywebsite/ESP/index.htm, but if you are from Portugal, takes you to mywebsite/PT/index.htm.

    Regards,

    Chewie.
     
    chewie49, Nov 5, 2007 IP
  5. goliathus

    goliathus Peon

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    if you need only those 2 languages, the best is do it like this.

    $checklang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    if(substr($checklang, 0, 2)=="pt"){
    header("Location: mywebsite/PT/index.htm");
    }else{
    header("Location: mywebsite/ESP/index.htm");
    }

    It means guys from Portugal, Brazil, .. will get Portuguese, other will get Spanish, which is default.

    If you need Portuguese as default, change it like this:

    $checklang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    if(substr($checklang, 0, 2)=="es"){
    header("Location: mywebsite/ESP/index.htm");
    }else{
    header("Location: mywebsite/PT/index.htm");
    }


    (reputation points appreciated :D)
     
    goliathus, Nov 5, 2007 IP
  6. chewie49

    chewie49 Well-Known Member

    Messages:
    305
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #6
    Thanks again Goliathus!!!

    Where exactly do I put that code?
     
    chewie49, Nov 5, 2007 IP
  7. goliathus

    goliathus Peon

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    index.php in ROOT directory should be like this:

    <?php
    $checklang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    if(substr($checklang, 0, 2)=="es"){
    header("Location: mywebsite/ESP/index.htm");
    }else{
    header("Location: mywebsite/PT/index.htm");
    }
    ?>

    that's all...
     
    goliathus, Nov 5, 2007 IP
  8. Seiya

    Seiya Peon

    Messages:
    4,666
    Likes Received:
    404
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You sure he knows it needs to be in a php file? =)
     
    Seiya, Nov 6, 2007 IP
  9. NetherBen

    NetherBen Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You might want to also look into gettext if you want to have translated pages on your site.

    // I cannot post links yet:
    us2.php.net/manual/en/ref.gettext.php

    It is the most common way of having multiple language support on a website. (It's somewhat a pain at the beginning, but over time it is pretty useful)

    -Ben
    hab.la - Chat with visitors to your website for free using your existing IM client.
     
    NetherBen, Nov 6, 2007 IP
  10. chewie49

    chewie49 Well-Known Member

    Messages:
    305
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #10
    I don't understand. Do I create a PHP file just with that code or I put in in my index page and just rename it as a PHP ??

    Thanks!
     
    chewie49, Nov 10, 2007 IP