Welcome Guest, Not a member yet? Register   Sign In
YAULD : yet another uri language detector
#1

[eluser]xwero[/eluser]
people keep on creating uri language detectors but it seems they don't really use the power CI has. So i want to show how it can be done using CI as much as possible.

I assume the first segment of the url is the language segment and it contains 2 alphabetic characters. And the language segment is optional for the default language.

For this to work you need to abandon the full language name CI uses to identify the language directories. And you need to change the config settings to
Code:
// config.php
$config['language'] = 'en'; // shorter string
$config['allowed_langs'] = array('nl','fr','es'); // add
// routes.php
$route['[a-z]{2}(.*)'] = '$1' // the language doesn't need to be fetched to reroute
To change the default language i use a post_controller_constructor hook
Code:
function get_uri_lang()
{
   $CI =& get_instance();

   if(in_array($CI->uri->segment(1),$CI->config->item('allowed_langs')))
   {
      // to find out in url helper functions if the language has been set by the url
      $CI->config->set_item('uri_lang',true);
      // to load language files from the proper language directory
      $CI->config->set_item('language',$CI->uri->segment(1));
   }

   $CI->lang->load_queue(); // more on this later
}
Because it's possible language files need to be loaded in all methods of the controller and the best way to do it is in the constructor, the language class needs to be extended
Code:
class MY_Language extends CI_Language
{
   var $queue = array();

   function MY_Language()
   {
      parent::CI_language();
   }

   /*
   *  Queues language files that have to be loaded for each method in the controller
   * call this method in the controller constructor
   *
   * @param string/array
   * @return void
   */
   function queue($file)
   {
      if(is_string($file))
      {
         $file = array($file);
      }

      $this->queue = array_merge($this->queue,$file);
   }

   /*
   * loads all the files in the queue
   *
   * @return void
   */
   function load_queue()
   {
      foreach($this->queue as $file)
      {
          $this->load($file);
      }
   }
}
For the language switch and other url generating functions that have to be language aware you need to create a few more functions and i would add them to the url helper using the MY_url_helper.php file.
Code:
/*
* creates an array of urls based on the current url and the allowed languages in the config file
*/
function language_switch()
{
   $CI =& get_instance();
   $urls = array();
   $uri_string = uri_string();

   if($CI->config->item('uri_lang') == true)
   {
      $uri_string = implode('/',array_slice($CI->uri->segment_array(),1);
      
      $urls['default'] = site_url($uri_string);
   }
   else
   {
      $urls['default'] = site_url($uri_string);
   }

   if(strlen($uri_string) > 0 && strncasecmp('/',$uri_string,1) == 0)
   {
      $uri_string = '/'.$uri_string;  
   }

   foreach($CI->config->item('allowed_langs') as $lang)
   {
       $urls[$lang] = site_url($lang.$uri_string);
   }

   return $urls;
}

function site_url_lang($uri = '')
{
    $CI =& get_instance();
    $segments = array();
    
    if($CI->config->item('uri_lang') == true)
    {
        $segments[] = $CI->config->item('language');
    }

    if(strlen($uri) > 0)
    {
       $segments[] = $uri;
    }
    return site_url(implode('/',$segments));
}
The site_url_lang function can be used to create language dependent anchor and anchor_popup functions.
#2

[eluser]xwero[/eluser]
UPDATE 1 : I decided it was wrong to alter the behavior of the existing url helper functions and therefor i flipped the url generation of the site_url_lang function and changed the code to make everything fit in the new vision.
#3

[eluser]xwero[/eluser]
UPDATE 2 : changed the route to fetch site.com/index.php/nl too and improved the language_switch helper function.




Theme © iAndrew 2016 - Forum software by © MyBB