Advertisement
jamboljack

MY_Lang.php

Nov 21st, 2014
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.50 KB | None | 0 0
  1. <?php  if ( ! defined('BASEPATH')) 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 extension
  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.   private $languages = array(
  25.     'en' => 'english',
  26.     'ko' => 'korean'
  27.   );
  28.  
  29.   // special URIs (not localized)
  30.   private $special = array (
  31.     "panel", "session"
  32.   );
  33.  
  34.   // where to redirect if no language in URI
  35.   private $uri;
  36.   private $default_uri;
  37.   private $lang_code;
  38.  
  39.   /**************************************************/
  40.  
  41.  
  42.   function MY_Lang()
  43.   {
  44.     parent::__construct();
  45.  
  46.     global $CFG;
  47.     global $URI;
  48.     global $RTR;
  49.  
  50.     $this->uri = $URI->uri_string();
  51.     $this->default_uri = $RTR->default_controller;
  52.  
  53.     $uri_segment = $this->get_uri_lang($this->uri);
  54.     $this->lang_code = $uri_segment['lang'] ;
  55.  
  56.     $url_ok = false;
  57.     if ((!empty($this->lang_code)) && (array_key_exists($this->lang_code, $this->languages)))
  58.     {
  59.         $language = $this->languages[$this->lang_code];
  60.         $CFG->set_item('language', $language);
  61.         $url_ok = true;
  62.     }
  63.  
  64.     if ((!$url_ok) && (!$this->is_special($uri_segment['parts'][0]))) // special URI -> no redirect
  65.     {
  66.       // set default language
  67.       $CFG->set_item('language', $this->languages[$this->default_lang()]);
  68.  
  69.       $uri = (!empty($this->uri)) ? $this->uri: $this->default_uri;
  70.       //OPB - modification to use i18n also without changing the .htaccess (without pretty url)
  71.       $index_url = empty($CFG->config['index_page']) ? '' : $CFG->config['index_page']."/";
  72.       $new_url = $CFG->config['base_url'].$index_url.$this->default_lang().'/'.$uri;
  73.  
  74.       header("Location: " . $new_url, TRUE, 302);
  75.       exit;
  76.     }
  77.   }
  78.  
  79.   // get current language
  80.   // ex: return 'en' if language in CI config is 'english'
  81.   function lang()
  82.   {
  83.     global $CFG;        
  84.     $language = $CFG->item('language');
  85.  
  86.     $lang = array_search($language, $this->languages);
  87.     if ($lang)
  88.     {
  89.       return $lang;
  90.     }
  91.  
  92.     return NULL;    // this should not happen
  93.   }
  94.  
  95.  
  96.   function is_special($lang_code)
  97.   {
  98.     if ((!empty($lang_code)) && (in_array($lang_code, $this->special)))
  99.       return TRUE;
  100.     else
  101.       return FALSE;
  102.   }
  103.  
  104.  
  105.   function switch_uri($lang)
  106.   {
  107.     if ((!empty($this->uri)) && (array_key_exists($lang, $this->languages)))
  108.     {
  109.  
  110.       if ($uri_segment = $this->get_uri_lang($this->uri))
  111.       {
  112.         $uri_segment['parts'][0] = $lang;
  113.         $uri = implode('/',$uri_segment['parts']);
  114.       }
  115.       else
  116.       {
  117.         $uri = $lang.'/'.$this->uri;
  118.       }
  119.     }
  120.  
  121.     return $uri;
  122.   }
  123.  
  124.   //check if the language exists
  125.   //when true returns an array with lang abbreviation + rest
  126.   function get_uri_lang($uri = '')
  127.   {
  128.     if (!empty($uri))
  129.     {
  130.       $uri = ($uri[0] == '/') ? substr($uri, 1): $uri;
  131.  
  132.       $uri_expl = explode('/', $uri, 2);
  133.       $uri_segment['lang'] = NULL;
  134.       $uri_segment['parts'] = $uri_expl;  
  135.  
  136.       if (array_key_exists($uri_expl[0], $this->languages))
  137.       {
  138.         $uri_segment['lang'] = $uri_expl[0];
  139.       }
  140.       return $uri_segment;
  141.     }
  142.     else
  143.       return FALSE;
  144.   }
  145.  
  146.  
  147.   // default language: first element of $this->languages
  148.   function default_lang()
  149.   {
  150.     $browser_lang = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strtok(strip_tags($_SERVER['HTTP_ACCEPT_LANGUAGE']), ',') : '';
  151.     $browser_lang = substr($browser_lang, 0,2);
  152.     if(array_key_exists($browser_lang, $this->languages))
  153.         return $browser_lang;
  154.     else{
  155.         reset($this->languages);
  156.         return key($this->languages);
  157.     }
  158.   }
  159.  
  160.  
  161.   // add language segment to $uri (if appropriate)
  162.   function localized($uri)
  163.   {
  164.     if (!empty($uri))
  165.     {
  166.       $uri_segment = $this->get_uri_lang($uri);
  167.       if (!$uri_segment['lang'])
  168.       {
  169.  
  170.         if ((!$this->is_special($uri_segment['parts'][0])) && (!preg_match('/(.+)\.[a-zA-Z0-9]{2,4}$/', $uri)))
  171.         {
  172.           $uri = $this->lang() . '/' . $uri;
  173.         }
  174.       }
  175.     }
  176.     return $uri;
  177.   }
  178.  
  179.     function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '', $load_first_lang = false) {
  180.         if ($load_first_lang) {
  181.             reset($this->languages);
  182.             $firstKey = key($this->languages);
  183.             $firstValue = $this->languages[$firstKey];
  184.  
  185.             if ($this->lang_code != $firstKey) {
  186.                 $addedLang = parent::load($langfile, $firstValue, $return, $add_suffix, $alt_path);
  187.                 if ($addedLang) {
  188.                     if ($add_suffix) {
  189.                         $langfileToRemove = str_replace('.php', '', $langfile);
  190.                         $langfileToRemove = str_replace('_lang.', '', $langfileToRemove) . '_lang';
  191.                         $langfileToRemove .= '.php';
  192.                     }
  193.                     $this->is_loaded = array_diff($this->is_loaded, array($langfileToRemove));
  194.                 }
  195.             }
  196.         }
  197.         return parent::load($langfile, $idiom, $return, $add_suffix, $alt_path);
  198.     }  
  199. }
  200.  
  201. /* End of file */
  202. /* Location: ./application/core/MY_Lang.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement