0

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'))
Badr Hari
  • 8,114
  • 18
  • 67
  • 100

3 Answers3

0

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;
}
?>
Community
  • 1
  • 1
0

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;
  }
}
?>
Dave Goten
  • 701
  • 4
  • 13
  • Cheers, maybe not the most efficient way, but this is something I was thinking about, but couldn't think of myself. – Badr Hari Jan 08 '15 at 15:40
0

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)) {
    // ...
}
Gio
  • 3,242
  • 1
  • 25
  • 53