
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Simplest way to detect client locale in PHP
PHP provides a function beginning from 5.3.0 to parse the ‘$_SERVER['HTTP_ACCEPT_LANGUAGE']’ variable into a locale −
Example
$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); echo $locale;
The ‘$_SERVER['HTTP_ACCEPT_LANGUAGE']’ function helps detect the locale by taking the current locale’s language as the parameter.
Output
This will produce the following output −
en_US
Most browsers submit an Accept-Language HTTP header that specifies en-us if they are from the US. Some older browsers use en only.
English-UK based users usually set their system or user locale to English-UK, which is the default browser configuration. This would result in en-gb as the Accept Language header. Other countries have en locales, such as en-za (South Africa), and so on.
Geo-IP based guesses would less likely be correct on the preferred language/locale.
- Related Articles
- Simplest way to detect keypresses in JavaScript?
- How to Detect User Timezone in PHP?
- What is the simplest way to SSH using Python?
- Detect base64 encoding in PHP?
- what is the simplest way to print a java array
- Detect language from string in PHP
- What's the simplest way to print a Java array?
- Simplest way to copy data from one table to another new table in MySQL?
- How to detect search engine bots with PHP?
- PHP – How to detect character encoding using mb_detect_encoding()
- What is the simplest way to make Matplotlib in OSX work in a virtual environment?
- The simplest way to get the user's current location on Android
- The easiest way to concatenate two arrays in PHP?
- Fastest way to store easily editable config data in PHP?
- The simplest way to get the user's current location on Android using Kotlin?
