Is it possible to write this shorter? I can't make changes to accept_lang method, and it always returns a boolean.
if ($ci->agent->accept_lang('en-US') OR $ci->agent->accept_lang('en-GB') OR $ci->agent->accept_lang('en-au'))
Is it possible to write this shorter? I can't make changes to accept_lang method, and it always returns a boolean.
if ($ci->agent->accept_lang('en-US') OR $ci->agent->accept_lang('en-GB') OR $ci->agent->accept_lang('en-au'))
How about use followings? It's copy from
Detect Browser Language in PHP
why dont you keep it simple and clean
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "fr":
//echo "PAGE FR";
include("index_fr.php");//include check session FR
break;
case "it":
//echo "PAGE IT";
include("index_it.php");
break;
case "en":
//echo "PAGE EN";
include("index_en.php");
break;
default:
//echo "PAGE EN - Setting Default";
include("index_en.php");//include EN in all other cases of different lang detection
break;
}
?>
Maybe not shorter, but a simple loop could help. Add the most common regions first to reduce the number of checks and the break to stop checking if you find one.
<?php
foreach(array('en-US','en-GB','en-au') as $lang) {if($ci->agent->accept_lang($lang)){stuff();break;}}
?>
or broken out:
<?php
foreach(array('en-US','en-GB','en-au') as $lang) {
if($ci->agent->accept_lang($lang)){
stuff(); // or whatever else you need, or set a $flag = true
break;
}
}
?>
Basically your are looking for true within an array of results, using in_array
i.c.w. array_map
you can construct the following one-liner;
if (in_array(true, array_map(array($ci, "accept_lang"), array("en-US", "en-GB", "en-au"))))
Broken out a bit to show more clearly what is happening;
$langAccept = array_map(array($ci, "accept_lang"), array("en-US", "en-GB", "en-au"));
if (in_array(true, $langAccept)) {
// ...
}