2

is there any method to know the language of the user i think i have to know his Country first and detemine the language according to it

Jon
  • 428,835
  • 81
  • 738
  • 806
Mostafa Elkady
  • 5,645
  • 10
  • 45
  • 69

8 Answers8

5

Any reason $_SERVER["HTTP_ACCEPT_LANGUAGE"] won't work?

Contents of the Accept-Language: header from the current request, if there is one. Example: 'en'.

http://php.net/manual/en/reserved.variables.server.php

Dutchie432
  • 28,798
  • 20
  • 92
  • 109
  • 3
    Note that *Accept-Language* is a list of weighted values. So it’s not necessarily just one value and the first value is not necessarily the most preferred one. – Gumbo Jan 09 '11 at 13:55
  • @moustafa: The Google Bot does not send a *Accept-Language* header field at all, if that’s it what you’re asking for. That means it accepts any language. In that case you should return a page with links to all available language variants. – Gumbo Jan 10 '11 at 14:25
2

There are multiple approaches. However, I would recommend looking at Accept-Language first.

Only if that's absent should you consider falling back on the approach you've given. That's basically using geolocation to get the country or region, then guessing the language.

The latter is pretty flawed, because many countries and even regions are very multi-lingual.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
2

First of all: There is a difference between the geographical location and the preferred language of a user and you can’t imply one information based on the knowledge of the other.

The geographical location of a user can be determined by geo-locating the IP address. And the best solution is to simply ask the user for his/her preferred language.

Because although the browser generally does send some language preferences along with the request (see Accept-Language header field and my answer on Detect Browser Language in PHP), these do not be the actual language preferences of the current user using the browser.

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 1
    Of course, you can use the best choice (if any) from `Accept-Language` as the default, then make it easy for the user to select an alternative. – Matthew Flaschen Jan 09 '11 at 14:04
  • @Matthew Flaschen: Yes, you can use that information as an initial value. But it’s always better to ask the user. – Gumbo Jan 09 '11 at 14:08
  • 2
    What’s the reason for the down vote? Is there something wrong with my answer? – Gumbo Jan 09 '11 at 14:10
  • There is no point in your wordy answer. Accept-Language is a perfect choice for the initial guess. Just throw in a language selector at the top of the page and you will cover any possible solution. No need for empty blab about weights, exceptional cases, etc. Keep it simple – Your Common Sense Jan 09 '11 at 14:20
  • 1
    @Col. Shrapnel: But the quality parameter of an *Accept-Language* value can also forbid a value: `en;q=0` means *English is inacceptable*. And why just take an arbitrary value if you can take an optimal value? – Gumbo Jan 09 '11 at 14:24
  • OMG, have you seen such a setting in real? And why choose ONLY ONE solution? To ask a user is okay. By why throw automatic detection away? USE BOTH. – Your Common Sense Jan 09 '11 at 14:26
  • @Col, he **already** said (in the second comment) you can use both. And if you implement the standard, you don't have to guess which parts clients use (there could be stupid programs writing Accept-Language headers). – Matthew Flaschen Jan 09 '11 at 14:32
  • 2
    @Col. Shrapnel: I didn’t argue for not using automatic detection. I said that automatic detection is possible but not that easy as just taking the first value of *Accept-Language* as that’s not the best choice. And that automatically detected value should only be used until the user chose his/her actual preferred language. – Gumbo Jan 09 '11 at 14:33
1

most browsers send their default locale with the request. you can retrieve this information through
$userLang = $_SERVER['HTTP_ACCEPT_LANGUAGE']

Samuel Herzog
  • 3,561
  • 1
  • 22
  • 21
1

Check this out

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

Also

check this link

Wazy
  • 8,822
  • 10
  • 53
  • 98
1

If $_SERVER['HTTP_ACCEPT_LANGUAGE'] is not helpful enough you can try this service

http://www.geoplugin.com/webservices/php

mr.Shu
  • 478
  • 5
  • 9
0

You can use

http_negotiate_language($langs)

link to documentation

Mark
  • 1
0

In case you need the full language names instead of the abbreviations (en, fr, etc.) try this basic function:

function parse_language( $languages = '' ) {
    $translation = array( 'en' => 'English', 'fr' => 'French', 'es' => 'Spanish', 'de' => 'German', 'ja' => 'Japanese');
    if( $languages == '' ) { //Default to HTTP accept language header
        $languages = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    }
    $languages = explode( ',', strtolower( $languages ) ); //Separate individual languages
    foreach( $languages as &$language ) { //Filter out any county codes like -US
        if( strpos( $language, '-' ) !== false ) {
            list( $language, ) = explode( '-', $language, 2 );
        }
    }
    //Find the intersections between the translation keys and the language values 
    return( array_values( array_intersect_key( $translation, array_combine( $languages, array_fill( 0, count( $languages ), '' ) ) ) ) );
}

//Sample usage
print_r( parse_language( 'en-US,de-GR,fr,es' ) );

If you need more language support, simple add more key/value pairs to the translation array.

Bailey Parker
  • 15,599
  • 5
  • 53
  • 91