Advertisement
comic0

Untitled

Sep 9th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.48 KB | None | 0 0
  1. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. // CodeIgniter i18n library by Jérôme Jaglale
  4. // http://maestric.com/en/doc/php/codeigniter_i18n
  5. // version 10 - May 10, 2012
  6.  
  7. class MY_Lang extends CI_Lang {
  8.  
  9.     /**************************************************
  10.      configuration
  11.     ***************************************************/
  12.  
  13.     // languages
  14.     var $languages = array(
  15.         'fr' => 'french',
  16.         'en' => 'english',
  17.         'de' => 'deutch',
  18.         'it' => 'italian',
  19.         'es' => 'spanish',
  20.         'nl' => 'netherland',
  21.         'no' => 'norsk',
  22.         'dk' => 'danish'
  23.     );
  24.  
  25.     // special URIs (not localized)
  26.     var $special = array (
  27.         "admin"
  28.     );
  29.    
  30.     // where to redirect if no language in URI
  31.     var $default_uri = '';
  32.  
  33.     /**************************************************/
  34.    
  35.    
  36.     function __construct()
  37.     {
  38.         parent::__construct();     
  39.        
  40.         global $CFG;
  41.         global $URI;
  42.         global $RTR;
  43.        
  44.         $segment = $URI->segment(1);
  45.        
  46.         if (isset($this->languages[$segment]))  // URI with language -> ok
  47.         {
  48.             $language = $this->languages[$segment];
  49.             $CFG->set_item('language', $language);
  50.  
  51.         }
  52.         else if($this->is_special($segment)) // special URI -> no redirect
  53.         {
  54.             return;
  55.         }/*
  56.         else    // URI without language -> redirect to default_uri
  57.         {
  58.             // set default language
  59.             $CFG->set_item('language', $this->languages[$this->default_lang()]);
  60.  
  61.             // redirect
  62.             header("Location: " . $CFG->site_url($this->localized($this->default_uri)), TRUE, 302);
  63.             exit;
  64.         }*/
  65.     }
  66.    
  67.     // get current language
  68.     // ex: return 'en' if language in CI config is 'english'
  69.     function lang()
  70.     {
  71.         global $CFG;       
  72.         $language = $CFG->item('language');
  73.        
  74.         $lang = array_search($language, $this->languages);
  75.         if ($lang)
  76.         {
  77.             return $lang;
  78.         }
  79.        
  80.         return NULL;    // this should not happen
  81.     }
  82.  
  83.     public function getLanguages(){
  84.         return $this->languages;
  85.     }
  86.    
  87.     function is_special($uri)
  88.     {
  89.         $exploded = explode('/', $uri);
  90.         if (in_array($exploded[0], $this->special))
  91.         {
  92.             return TRUE;
  93.         }
  94.         if(isset($this->languages[$uri]))
  95.         {
  96.             return TRUE;
  97.         }
  98.         return FALSE;
  99.     }
  100.    
  101.     function switch_uri($lang)
  102.     {
  103.         $CI =& get_instance();
  104.  
  105.         $uri = $CI->uri->uri_string();
  106.         if ($uri != "")
  107.         {
  108.             $exploded = explode('/', $uri);
  109.             if($exploded[0] == $this->lang())
  110.             {
  111.                 $exploded[0] = $lang;
  112.             }
  113.             $uri = implode('/',$exploded);
  114.         }
  115.         return $uri;
  116.     }
  117.    
  118.     // is there a language segment in this $uri?
  119.     function has_language($uri)
  120.     {
  121.         $first_segment = NULL;
  122.        
  123.         $exploded = explode('/', $uri);
  124.         if(isset($exploded[0]))
  125.         {
  126.             if($exploded[0] != '')
  127.             {
  128.                 $first_segment = $exploded[0];
  129.             }
  130.             else if(isset($exploded[1]) && $exploded[1] != '')
  131.             {
  132.                 $first_segment = $exploded[1];
  133.             }
  134.         }
  135.        
  136.         if($first_segment != NULL)
  137.         {
  138.             return isset($this->languages[$first_segment]);
  139.         }
  140.        
  141.         return FALSE;
  142.     }
  143.    
  144.     // default language: first element of $this->languages
  145.     function default_lang()
  146.     {
  147.         foreach ($this->languages as $lang => $language)
  148.         {
  149.             return $lang;
  150.         }
  151.     }
  152.    
  153.     // add language segment to $uri (if appropriate)
  154.     function localized($uri)
  155.     {
  156.         if($this->has_language($uri)
  157.                 || $this->is_special($uri)
  158.                 || preg_match('/(.+)\.[a-zA-Z0-9]{2,4}$/', $uri))
  159.         {
  160.             // we don't need a language segment because:
  161.             // - there's already one or
  162.             // - it's a special uri (set in $special) or
  163.             // - that's a link to a file
  164.         }
  165.         else
  166.         {
  167.             $uri = $this->lang() . '/' . $uri;
  168.         }
  169.        
  170.         return $uri;
  171.     }
  172.    
  173. }
  174.  
  175. /* End of file */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement