Advertisement
Guest User

Untitled

a guest
Sep 25th, 2011
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.96 KB | None | 0 0
  1. <?php (defined('BASEPATH')) OR exit('No direct script access allowed');
  2.  
  3. // Originaly CodeIgniter i18n library by Jérôme Jaglale
  4. // http://maestric.com/en/doc/php/codeigniter_i18n
  5. //modification by Yeb Reitsma
  6.  
  7. /*
  8. in case you use it with the HMVC modular extention
  9. uncomment this and remove the other lines
  10. load the MX_Loader class */
  11.  
  12. //require APPPATH."third_party/MX/Lang.php";
  13.  
  14. //class MY_Lang extends MX_Lang {
  15.    
  16. class MY_Lang extends CI_Lang {
  17.  
  18.  
  19.     /**************************************************
  20.      configuration
  21.     ***************************************************/
  22.  
  23.     // languages
  24.  
  25.     private $languages = array(
  26.         'en' => 'english',
  27.         'de' => 'german',
  28.         'fr' => 'french',
  29.     'nl' => 'dutch',
  30.  
  31.     );
  32.  
  33.     // special URIs (not localized)
  34.     private $special = array (
  35.         "admincp"
  36.     );
  37.    
  38.     // where to redirect if no language in URI
  39.     private $uri;
  40.     private $default_uri;
  41.     private $lang_code;
  42.  
  43.     /**************************************************/
  44.  
  45.  
  46.  
  47.    
  48.     function MY_Lang()
  49.     {
  50.  
  51.         parent::__construct();
  52.  
  53.         global $CFG;
  54.         global $URI;
  55.         global $RTR;
  56.        
  57.         $this->uri = $URI->uri_string();
  58.         $this->default_uri = $RTR->default_controller;
  59.        
  60.         $uri_segment = $this->get_uri_lang($this->uri);
  61.         $this->lang_code = $uri_segment['lang'] ;
  62.        
  63.         $url_ok = false;
  64.         if ((!empty($this->lang_code)) && (array_key_exists($this->lang_code, $this->languages)))
  65.         {
  66.             $language = $this->languages[$this->lang_code];
  67.             $CFG->set_item('language', $language);
  68.             $url_ok = true;
  69.         }
  70.        
  71.         if ((!$url_ok) && (!$this->is_special($uri_segment['parts'][0]))) // special URI -> no redirect
  72.         {
  73.             // set default language
  74.             $CFG->set_item('language', $this->languages[$this->default_lang()]);
  75.            
  76.             $uri = (!empty($this->uri)) ? $this->uri: $this->default_uri;
  77.             $uri = ($uri[0] != '/') ? '/'.$uri : $uri;
  78.             $new_url = $CFG->config['base_url'].$this->default_lang().$uri;
  79.            
  80.             header("Location: " . $new_url, TRUE, 302);
  81.             exit;
  82.         }
  83.     }
  84.  
  85.    
  86.    
  87.     // get current language
  88.     // ex: return 'en' if language in CI config is 'english'
  89.     function lang()
  90.     {
  91.         global $CFG;        
  92.         $language = $CFG->item('language');
  93.        
  94.         $lang = array_search($language, $this->languages);
  95.         if ($lang)
  96.         {
  97.             return $lang;
  98.         }
  99.        
  100.         return NULL;    // this should not happen
  101.     }
  102.    
  103.    
  104.     function is_special($lang_code)
  105.     {
  106.         if ((!empty($lang_code)) && (in_array($lang_code, $this->special)))
  107.             return TRUE;
  108.         else
  109.             return FALSE;
  110.     }
  111.    
  112.    
  113.     function switch_uri($lang)
  114.         {
  115.             if ((!empty($this->uri)) && (array_key_exists($lang, $this->languages)))
  116.             {
  117.  
  118.                 if ($uri_segment = $this->get_uri_lang($this->uri))
  119.                 {
  120.                     $uri_segment['parts'][0] = $lang;
  121.                     $uri = implode('/',$uri_segment['parts']);
  122.                 }
  123.                 else
  124.                 {
  125.                     $uri = $lang.'/'.$this->uri;
  126.                 }
  127.             }
  128.  
  129.             return $uri;
  130.         }
  131.    
  132.     //check if the language exists
  133.     //when true returns an array with lang abbreviation + rest
  134.     function get_uri_lang($uri = '')
  135.     {
  136.         if (!empty($uri))
  137.         {
  138.             $uri = ($uri[0] == '/') ? substr($uri, 1): $uri;
  139.            
  140.             $uri_expl = explode('/', $uri, 2);
  141.             $uri_segment['lang'] = NULL;
  142.             $uri_segment['parts'] = $uri_expl;     
  143.            
  144.             if (array_key_exists($uri_expl[0], $this->languages))
  145.             {
  146.                 $uri_segment['lang'] = $uri_expl[0];
  147.             }
  148.             return $uri_segment;
  149.         }
  150.         else
  151.             return FALSE;
  152.     }
  153.  
  154.    
  155.     // default language: first element of $this->languages
  156.      function default_lang()
  157.     {
  158. //      $browser_lang = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strtok(strip_tags($_SERVER['HTTP_ACCEPT_LANGUAGE']), ',') : '';
  159. //      $browser_lang = substr($browser_lang, 0,2);
  160. //      return (array_key_exists($browser_lang, $this->languages)) ? $browser_lang: 'en';
  161.  
  162.             $lang = array_search(config_item('language'), $this->languages);
  163.             if ($lang)
  164.             {
  165.                 return $lang;
  166.             }
  167.             return 'en';    // return en if undefined languae
  168.  
  169.     }
  170.    
  171.    
  172.     // add language segment to $uri (if appropriate)
  173.     function localized($uri)
  174.     {
  175.         if (!empty($uri))
  176.         {
  177.             $uri_segment = $this->get_uri_lang($uri);
  178.             if (!$uri_segment['lang'])
  179.             {
  180.  
  181.                 if ((!$this->is_special($uri_segment['parts'][0])) && (!preg_match('/(.+)\.[a-zA-Z0-9]{2,4}$/', $uri)))
  182.                 {
  183.                     $uri = $this->lang() . '/' . $uri;
  184.                 }
  185.             }
  186.         }
  187.         return $uri;
  188.     }
  189. }
  190.  
  191. // END MY_Lang Class
  192.  
  193. /* End of file MY_Config.php */
  194. /* Location: ./system/application/core/MY_Lang.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement