Last active
December 16, 2021 16:37
-
-
Save techjewel/fc026460e8a0fbc135c46833f4deb780 to your computer and use it in GitHub Desktop.
Add Browser Language smartcode on Fluent Forms Editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Add Browser Language smartcode on Fluent Forms Editor | |
*/ | |
add_filter('fluentform_editor_shortcodes', function ($groups) { | |
$groups[0]['shortcodes']['{browser_lang}'] = 'Browser Language'; | |
return $groups; | |
}); | |
add_filter('fluentform_editor_shortcode_callback_browser_lang', function ($val) { | |
if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) { | |
$http_accept = $_SERVER["HTTP_ACCEPT_LANGUAGE"]; | |
} else { | |
return 'en-us'; | |
} | |
$deflang = 'en-us'; | |
if(isset($http_accept) && strlen($http_accept) > 1) { | |
# Split possible languages into array | |
$x = explode(",",$http_accept); | |
foreach ($x as $val) { | |
#check for q-value and create associative array. No q-value means 1 by rule | |
if(preg_match("/(.*);q=([0-1]{0,1}.\d{0,4})/i",$val,$matches)) | |
$lang[$matches[1]] = (float)$matches[2]; | |
else | |
$lang[$val] = 1.0; | |
} | |
#return default language (highest q-value) | |
$qval = 0.0; | |
foreach ($lang as $key => $value) { | |
if ($value > $qval) { | |
$qval = (float)$value; | |
$deflang = $key; | |
} | |
} | |
} | |
return strtolower($deflang); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment