How to detect browser language in PHP

Ashish Kumar
Jan 11 · 1 min read

Here, going to explain how to detect client browser languages using PHP.
As, we know today’s world application are coming in multi languages, As a developer, developing the web application with multi languages support is little challenging for us, anyway we have many plugins and extensions are available for solving this issues. If you want to do without using any plugins or extension use this code to easily get the client browser default language.


/* Detect Browser langagues using PHP */function tbs_get_client_browser_lang( $checklanguages, $default= "en" ){if( isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ){
$langs = explode( ',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
foreach ( $lang as $value) {
$getlang = substr( $value, 0,2 );
if( in_array( $getlang, $checklanguages ) ){
return $getlang;
}
}
}//Return default.
return $default;
}

I have used this code for many project and tested well.
I would thank to my organisation TechBrise Solution for providing such great environment to read and research.

Ashish Kumar

Written by

Software Engineer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade