

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Simplest way to detect keypresses in JavaScript?
- What is the simplest way to SSH using Python?
- what is the simplest way to print a java array
- What's the simplest way to print a Java array?
- Simplest way to copy data from one table to another new table in MySQL?
- Detect base64 encoding in PHP?
- The simplest way to get the user's current location on Android
- What is the simplest way to make Matplotlib in OSX work in a virtual environment?
- Detect language from string in PHP
- The simplest way to get the user's current location on Android using Kotlin?
- How to detect search engine bots with PHP?
- PHP – How to detect character encoding using mb_detect_encoding()
- The easiest way to concatenate two arrays in PHP?
- Locale-specific formatting in Java
- Fastest way to store easily editable config data in PHP?