-
Notifications
You must be signed in to change notification settings - Fork 0
URI Language Identifier
Category:Core | Category:Core::Community | Category:Core::Language | Category:Core::URL
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. http://domain.tld/[color=red][b]en[/b][/color]/controller/method, http://domain.tld/[color=red][b]es[/b][/color]/controller/method, http://domain.tld/[color=red][b]de[/b][/color]/controller/method
[b]application/config/routes.php[/b] [code]//route example: http://domain.tld/en/controller => http://domain.tld/controller $route['(\w{2})/(.*)'] = '$2'; $route['(\w{2})'] = $route['default_controller'];[/code]
[b]application/config/config.php[/b] [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]
[b]application/libraries/MY_Language.php[/b] [code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /**
-
URI Language Identifier
-
Adds a language identifier prefix to all site_url links
-
@copyright Copyright (c) Wiredesignz 2009-09-05
-
@version 0.18
-
Permission is hereby granted, free of charge, to any person obtaining a copy
-
of this software and associated documentation files (the "Software"), to deal
-
in the Software without restriction, including without limitation the rights
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-
copies of the Software, and to permit persons to whom the Software is
-
furnished to do so, subject to the following conditions:
-
The above copyright notice and this permission notice shall be included in
-
all copies or substantial portions of the Software.
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-
THE SOFTWARE. */ class MY_Language extends CI_Language { function MY_Language() {
global $RTR; $index_page = $RTR->config->item('index_page'); $lang_uri_abbr = $RTR->config->item('lang_uri_abbr'); /* remove the lang_abbr from uri segments preserving keys */ $lang_abbr = array_shift_left($RTR->uri->segments); /* check for invalid abbreviation */ if( ! isset($lang_uri_abbr[$lang_abbr])) { $deft_abbr = $RTR->config->item('language_abbr'); /* check for abbreviation to be ignored */ if ($deft_abbr != $RTR->config->item('lang_ignore')) { /* check and set the default uri identifier */ $index_page .= empty($index_page) ? $deft_abbr : '/'.$deft_abbr; /* redirect after inserting language id */ header('Location: '.$RTR->config->item('base_url').$index_page.$RTR->uri->uri_string); } /* get the language name */ $user_lang = $lang_uri_abbr[$deft_abbr]; } else { /* get the 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 abbreviation to be ignored */ if ($lang_abbr != $RTR->config->item('lang_ignore')) { /* check and set the user uri identifier */ $index_page .= empty($index_page) ? $lang_abbr : '/'.$lang_abbr; } } /* reset the index_page value */ $index_page .= empty($index_page) ? '' : '/'; $RTR->config->set_item('index_page', $index_page); /* load the user language file */ parent::load($user_lang, $user_lang); log_message('debug', "MY_Language Class Initialized");} }
/* shift array values left */ function array_shift_left(&$arr1) { $shift = array_shift($arr1); foreach (array_keys($arr1) as $k) { $arr2[$k+1] = $arr1[$k]; } isset($arr2) AND $arr1 = $arr2; return $shift; }
/* Override CI current_url helper (from CI 1.7.0) */
function current_url() {
global $URI;
$arr = explode('/',rtrim($URI->config->site_url(),'/'));
if ($URI->config->item('language_abbr') == array_pop($arr)) {
return implode('/',$arr).$URI->uri_string;
}
return $URI->config->site_url($URI->uri_string);
}
[/code]
[b]Note: Updated URI Language Identifier does not load language files[/b] Use the $config['language'] value to determine the language files to load for your application or module.
[h3]Extending url helper to display links to content in another language[/h3] If you want to display links to another languages for your content, you can extend url helper. Add to the config file a few lines like this: [code]//language descriptions $config['lang_desc']=array("es"=>"Versión en español", "en" => "English version", "gl" => "Versión en galego");[/code]
Place this file in your application
[code]
if you want to display images or text you can add to the config file [code]//language use images $config['lang_useimg'] = true; //or false to use only text [/code]
[b]MY_url_helper.php[/b] [code]<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /**
- Alternative languages helper
- Returns a string with links to the content in alternative languages
- version 0.2
- @author Luis luis@piezas.org.es
- @modified by Ionut contact@quasiperfect.eu */
function alt_site_url($uri = '') { $CI =& get_instance(); $actual_lang=$CI->uri->segment(1); $languages=$CI->config->item('lang_desc'); $languages_useimg=$CI->config->item('lang_useimg'); $ignore_lang=$CI->config->item('lang_ignore'); if (empty($actual_lang)) { $uri=$ignore_lang.$CI->uri->uri_string(); $actual_lang=$ignore_lang; } else { if (!array_key_exists($actual_lang,$languages)) { $uri=$ignore_lang.$CI->uri->uri_string(); $actual_lang=$ignore_lang; } else { $uri=$CI->uri->uri_string(); $uri=substr_replace($uri,'',0,1); } } $alt_url='
- ';
//i use ul because for me formating a list from css is easy
foreach ($languages as $lang=>$lang_desc)
{
if ($actual_lang!=$lang)
{
$alt_url.='
- ';
if ($languages_useimg){
//change the path on u'r needs
//in images u need to have for example en.gif and so on for every
//language u use //the language description will be used as alternative $alt_url.= '
';
}
else
{
$alt_url.= $lang_desc.'';
}
}
}
$alt_url.='
- Original author: Derek Jones
- How to extend helpers: See User Guide
- Modified by: Thomas Stapleton (id, classes, selected country option and all option)
- Modified by: Bradley De-Lar (construct, setLayout example)