Skip to content

URI Language Identifier

World Wide Web Server edited this page Jul 4, 2012 · 83 revisions

Category:Core Extensions::Language Category:Libraries::Language

This language class extension allows you to automatically prefix all site urls with a language abbreviation that is pre-defined in your config file or from a link and automatically load the corresponding language translation file, the route will then be corrected by the route regex for everything to work as normal.

Somewhere in the site you can provide the user with links allowing them to change their desired language name. [color=red]http://[/color][color=red]domain.tld/en/controller/method[/color] [color=red]http://[/color][color=red]domain.tld/es/controller/method[/color] [color=red]http://[/color][color=red]domain.tld/de/controller/method[/color]

application/config/routes.php [code]

//route example: http://domain.tld/en/controller => http://domain.tld/controller $route['(\w{2})/(.*)'] = '$2'; $route['(\w{2})'] = $route['default_controller'];

[/code]

application/config/config.php [code]

$config['language'] = "english";

//default language abbreviation $config['language_abbr'] = "en";

//set available language abbreviations $config['lang_uri_abbr'] = array("es" => "spanish", "en" => "english");

//ignore this language abbreviation $config['lang_ignore'] = "en";

[/code]

application/libraries/MY_Language.php [code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /**

  • URI Language Identifier

  • Adds a language identifier prefix to all site_url links

  • Loads the default language file

  • version 0.11 (c) Wiredesignz 2008-04-21 */ class MY_Language extends CI_Language { function MY_Language() { parent::CI_Language();

      global $RTR;
      
      //get the config values
      $index_page    = $RTR->config->item('index_page');
      $lang_uri_abbr = $RTR->config->item('lang_uri_abbr');
      
      //get the language from uri segment
      $lang_abbr = $RTR->uri->segment(1);
    
      //if invalid abbreviation reload the page using the default language
      if(!isset($lang_uri_abbr[$lang_abbr]))
      {            
          //get the default config values
          $base_url      = $RTR->config->item('base_url');
          $deft_abbr     = $RTR->config->item('language_abbr');
          
          //replace an invalid abbreviation with default language abbreviation
          if (strlen($lang_abbr) == 2)
          {
              $uri_string = str_replace("/{$lang_abbr}", '', $RTR->uri->uri_string);
          }
          else 
              //or just get the URI if no abbreviation is found
              $uri_string = $RTR->uri->uri_string;
          
          //check and set the language identifier
          $index_page .= ($index_page) ? '/'.$deft_abbr : $deft_abbr;
              
          header("Location:".$base_url.$index_page.$uri_string);
          exit();
      }
      else
      {
          //get the user language name
          $user_lang = $lang_uri_abbr[$lang_abbr];
      
          //reset config language to match the user language
          $RTR->config->set_item('language', $user_lang);
          $RTR->config->set_item('language_abbr', $lang_abbr);
      
          //check for language abbreviation to be ignored
          if ($lang_abbr != $RTR->config->item('lang_ignore'))
          {
              //check and set the language identifier
              $index_page .= ($index_page) ? '/'.$lang_abbr.'/' : $lang_abbr.'/';
              
              //reset the config index_page value
              $RTR->config->set_item('index_page', $index_page);
          }
          
          //load the user language file
          parent::load($user_lang, $user_lang);
      }
    

    } } [/code]

Clone this wiki locally