PHP can read the ‘HTTP_ACCEPT_LANGUAGE’ header from the browser request, but this might not be easy to parse if there is more than one accepted language set. For the simplest case, when one language is set as accepted, you need to read only first 2 characters from this header:
$language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
But, most WordPress language strings are more complex than that (there are few variations of English, and some languages have many variations too), so you would need to convert 2 letter language code into WordPress language code.
Also, HTTP_ACCEPT_LANGUAGE might not be included at all in the request, it depends on the browser.
Related Posts:
- How to get a translated string from a language other than the current one?
- How to get a localized version of WordPress from a repository?
- Working with l10n contexts (_x etc)
- How to escape the single quote character in i18n translation strings?
- How to escape single and plural i18n text strings?
- What are the most used WordPress localization?
- Difference between 2 internationalization (i18n) functions __() & _e()
- Localization works but still get “This plugin is not properly prepared for localization” msg in directory
- How to load theme textdomain from plugin?
- Can’t we use strings defined as PHP constants if we want to translate them in a plugin?
- get_the_date() for translating date format
- Should i use _n for pronouns in translation?
- POEdit with Custom mu-plugins code
- How does WordPress choose which translation file to use?
- Proper way to create / manage gettext (.po, .pot) files?
- Should I include colon in my msgid in PO file?
- _e() function get text_string from function
- Translations not returning translated strings
- Force translated language on front
- Translating dynamic strings
- WordPress translation loading english file
- Easy export/import of data for Translation
- How can I see my theme’s text domain?
- WordPress Theme .mo-file won’t load
- How to become a translator on wordpress.org?
- Translation from .mo file not working in theme
- How to prevent someone from entering strings without making it available for translation?
- Localization, escape html elements mid-sentence
- How to translate “$before” with get text in get_the_term_list?
- WordPress localization – gettext and git
- How to ignore extra whitespaces in translation functions like _e?
- /languages/ folder missing in default themes
- How to translate functions.php?
- Localization of WP theme
- WordPress Translations
- Post types translation
- Translate arguments of a user added custom code [closed]
- Internationalize plugin so strings can be translated in WPML [closed]
- localization of theme and plugins to English (originally in another language)
- Interface translation without plugins
- Translation not working for Constant strings in Plugin
- Plugin localization persistance (woocommerce)
- WordPress Localization error within return value
- Custom theme .mo translation file not working
- Plugin translation not displaying
- WordPress translation
- Tweak the loop for translation
- Translating form labels shortcode output
- Does WPML translate numbers automatically, or do I have to do the string translation? [closed]
- Website Language/Locale Switching
- .po file is loaded but changes are not appearing
- How to create .pot files with POedit?
- Translate a plugin using .po .mo files
- How to make a WordPress plugin translation ready?
- Theme localization of “slugs” (custom post types, taxonomies)
- Override default WordPress core translation
- Different Language for Frontend vs Backend
- Custom strings for translation using Polylang plugin
- How to print translation supported text with HTML URL
- Localization: I want the backend: english and frontend in defined language
- How-to: Translate plural forms for themes/plugins with PoEdit
- generating po mo translating files from scratch in a wordpress theme
- How to integrate get_post_time with date_i18n function?
- gettext does not translate when called in ajax
- How to debug failed load_theme_textdomain()?
- How to provide translations for a WordPress TinyMCE plugin?
- How do translated, escaped strings (esc_attr) in Themes work?
- Changing the language of a single page
- what’s different between _x() and _()
- How to handle text with function __(?
- Obtain a list of available translations
- How do I translate: “2 hours and 15 minutes”?
- Disabling Translation Update
- Gettext details
- custom theme: english .mo file not working
- Unknown language json files
- How to save a translation of a plugin in “CodeStyling Localization”?
- Best pratice to make taxonomy terms translatable without changing slugs?
- Set language per post
- List of Default Translated Phrases
- What is the purpose of an extra file for translation?
- Localizing text from XML files
- How to add extra language packs to WordPress
- load_plugin_textdomain in `plugins_loaded` or `init`?
- How to extract translatable (double underscores and _e) strings?
- How to Translate Contact Form 7 using qTranslate? [closed]
- How to set locale in ajax requests?
- Translate Navigation Menu & Sidebar Widget Titles
- Problems with localization
- Translate content, not instance, per-post
- Best location for theme translation files
- How does gettext works for translating readme file of plugin?
- `wp_set_script_translations` with `wp.i18n` does not return translated strings in simple plugin
- Localization / textdomain is slow and chaotic
- Why should I escape translatable strings? and how shall i do that?
- Can’t translate the post meta data (Date) in another language
- Change language of comments template
- How to handle single quote in a translatable string in WordPress
- Make theme translatable for WPML
- WordPress: After Gutenberg plugin migration to block.json the localization/ translations with PolyGlot in JavaScript does not work anymore