Welcome Guest, Not a member yet? Register   Sign In
How do I retrieve a uri segment in a hook?
#18

[eluser]Salvatore Formisano[/eluser]
Yeah it finally worked!

now my pick_language hook is:

Code:
<?
function pick_language() {

  require_once(APPPATH.'/config/language.php');

  session_start();

  // Lang set in URL
  if(substr( load_class('URI')->_parse_request_uri(), 1))
  {
    $lang = substr( load_class('URI')->_parse_request_uri(), 1);
    $lang = array_shift( explode( '/', $lang ) ); // takes the first segment
    $_SESSION['lang_code'] = $lang;
  }


  // Lang has already been set and is stored in a session
  elseif( !empty($_SESSION['lang_code']))
  {
    $lang = $_SESSION['lang_code'];
  }

  // Still no Lang. Lets try some browser detection then (this happens on the base_url, where no segment is available)
  else if (!empty( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ))
  {
    // explode languages into array
    $accept_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);

    log_message('debug', 'Checking browser languages: '.implode(', ', $accept_langs));

    // Check them all, until we find a match
    foreach ($accept_langs as $lang)
    {
      // Turn en-gb into en
      $lang = substr($lang, 0, 2);

      // Check its in the array. If so, break the loop, we have one!
      if(in_array($lang, array_keys($config['supported_languages'])))
      {
        break;
      }
    }
  }

  // If no language has been worked out - or it is not supported - use the default
  if(empty($lang) or !in_array($lang, array_keys($config['supported_languages'])))
  {
    $lang = $config['default_language'];
  }

  // Whatever we decided the lang was, save it for next time to avoid working it out again
  $_SESSION['lang_code'] = $lang;

  // Load CI config class
  $CI_config =& load_class('Config');

  // Set the language config. Selects the folder name from its key of 'en'
  $CI_config->set_item('language', $config['supported_languages'][$lang]['folder']);


  // Sets a constant to use throughout ALL of CI.
  define( 'CURRENT_LANGUAGE', $lang );



}

?>


and the get_route function which is use is
Code:
if ( ! function_exists('get_route'))
{

    function get_route($route,$parameters=NULL)
    {
      /*

      $parameters can be a string if you need to pass just one parameter
      or it can be an array, in which case you have to make sure you pass
      all the parameters in the right order

      example:

      if your route is something like

      blog/browse_by_category/:num/page/:num

      where the first :num is the blog category id and the second :num is the
      page number

      you would pass

      $parameters=array($blog_cat_id,$page_n)

      no regex allowed for this helper, yr
      :any | :num | (:any) | (:num)

      anything else will not work

      */

      // BEGIN

      if($parameters==NULL)
      {
        if(strpos($route,':')) return "get-route-error__did-not-expect-parameters-to-be-null.html";
      }

      else
      {
        if(!is_array($parameters)){
          $parameters=array($parameters);
        }

        $expected_parameters_n=substr_count($route,':');
        $parameters_n=count($parameters);

        if($expected_parameters_n!==$parameters_n) return "get-route-error__n-of-parameters-mismatch-of-expected-parameters.html";

        $route=str_replace("(","",$route);
        $route=str_replace(")","",$route);
        for( $i=0; $i<$parameters_n; $i++ ) {
          $pos=strpos($route,':');
          $sub=substr($route,$pos,4);
          $route=substr_replace($route,$parameters[$i],$pos,4);
        }

      }

      return $route;

  }
}

thanks a lot for the help man!


Messages In This Thread
How do I retrieve a uri segment in a hook? - by El Forum - 08-09-2010, 12:43 PM
How do I retrieve a uri segment in a hook? - by El Forum - 08-09-2010, 01:11 PM
How do I retrieve a uri segment in a hook? - by El Forum - 08-09-2010, 01:26 PM
How do I retrieve a uri segment in a hook? - by El Forum - 08-09-2010, 01:29 PM
How do I retrieve a uri segment in a hook? - by El Forum - 08-09-2010, 01:31 PM
How do I retrieve a uri segment in a hook? - by El Forum - 08-09-2010, 01:40 PM
How do I retrieve a uri segment in a hook? - by El Forum - 08-09-2010, 01:43 PM
How do I retrieve a uri segment in a hook? - by El Forum - 08-09-2010, 01:54 PM
How do I retrieve a uri segment in a hook? - by El Forum - 08-09-2010, 02:03 PM
How do I retrieve a uri segment in a hook? - by El Forum - 08-09-2010, 02:07 PM
How do I retrieve a uri segment in a hook? - by El Forum - 08-09-2010, 02:28 PM
How do I retrieve a uri segment in a hook? - by El Forum - 08-09-2010, 03:20 PM
How do I retrieve a uri segment in a hook? - by El Forum - 08-09-2010, 03:53 PM
How do I retrieve a uri segment in a hook? - by El Forum - 08-09-2010, 06:49 PM
How do I retrieve a uri segment in a hook? - by El Forum - 08-10-2010, 03:47 AM
How do I retrieve a uri segment in a hook? - by El Forum - 08-10-2010, 03:57 AM
How do I retrieve a uri segment in a hook? - by El Forum - 08-10-2010, 04:12 AM
How do I retrieve a uri segment in a hook? - by El Forum - 08-10-2010, 04:49 AM



Theme © iAndrew 2016 - Forum software by © MyBB