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

1
  • 1
    you can use apache geo_ip mod to find the country
    – Haim Evgi
    Jan 9, 2011 at 13:51

8 Answers 8

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

2
  • 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 9, 2011 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, 2011 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.

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.

8
  • 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. Jan 9, 2011 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 9, 2011 at 14:08
  • 2
    What’s the reason for the down vote? Is there something wrong with my answer?
    – Gumbo
    Jan 9, 2011 at 14:10
  • 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 9, 2011 at 14:24
  • 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 9, 2011 at 14:33
1

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

1

Check this out

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

Also

check this link

1

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

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

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.

0

You can use

http_negotiate_language($langs)

link to documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.