-
Notifications
You must be signed in to change notification settings - Fork 0
Lang Uri
WIKI
Lang_uri Lib Supplementary code for URI Language Identifier.
Lang_uri.php [code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /**
- ********** COPYRIGHT-VERSION NOTICE **********
- @author osci
- @copyright Copyright (c) 2011, NetID.gr.
- @license Read License Notice Below
- @link http://www.netid.gr/
- @version Version 0.9
- *************** CREDITS NOTICE ***************
- This code originated, although quite modified, from
- MY-Url_helper version 0.2 (supplament to wiredsignz's URI Language Identifier)
- @author Luis [email protected]
- @modified by Ionut [email protected]
- *************** LICENSE NOTICE ***************
- 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 Lang_uri { var $uri = ''; //Leave blank for current url var $slug = array(); //pass array in the format ('lang_abbr'=>'slug-to-add'), ie var $stripsegment = FALSE; //if needed to delete last segment (when using slug) var $exclude_current = FALSE; //don't return anything for current language var $nolink_current = FALSE; //don't return link for current language but display var $link_open_tag = ''; //open tag for link var $link_close_tag = ''; //close tag for link var $nolink_open_tag = ''; //open tag for no link var $nolink_close_tag = ''; //close tag for no link var $divider = ''; //divider, ie '
', leave empty for no use var $link_extra_global = ''; //accepts a string for global link class, ie 'class="lang_links"' var $link_extra_lang = ''; //array for specific lang details ('lang_abbr'=>'class="some" id="en_lang"'), leave empty for no use var $lang_useimg = NULL; //Values TRUE/FALSE.if not set it reads setting from config file var $lang_img_path = 'img/'; //path for flag imgs. needs trailing slash. path is absolute (using base_url()). empty for value from config var $lang_img_ext = 'png'; //file extension for flag imgs. imgs must have the lang_ as filename, ie en.png, el.png, etc/** * Constructor * * @access public * @param array initialization parameters */ public function __construct($params = array()) { log_message('debug', "Lang_uri Class Construct"); if (count($params) > 0) { $this->initialize($params); } }
/**
* Initialize Preferences
*
* @access public
* @param array initialization parameters
* @return void
*/
function initialize($params = array())
{
if (count($params) > 0)
{
foreach ($params as $key => $val)
{
if (isset($this->$key))
{
$this->$key = $val;
}
}
}
log_message('debug', "Lang_uri Class Initialized");
}
// --------------------------------------------------------------------
/**
* Generate the language uri links
*
* @access public
* @return string
*/
function create_links()
{
log_message('debug', "Lang_uri Class create_links start");
$output = '';
$CI = & get_instance();
if ($this->uri == '') {
$this->uri = $CI->uri->uri_string();
$this->uri = (substr($this->uri, 0, 1) == '/' ? $this->uri : "/$this->uri");
}
$this->uri = ($this->stripsegment) ? substr($this->uri, 0, strrpos($this->uri, '/')) : $this->uri;
$languages = $CI->config->item('lang_desc');
if ($this->lang_useimg===NULL)
{
$this->lang_useimg = $CI->config->item('lang_useimg');
}
if ($this->lang_img_path=='')
{
$this->lang_img_path = $CI->config->item('lang_img_path');
}
$ignore_lang = $CI->config->item('lang_ignore_abbr');
$current_lang = $CI->config->item('language_abbr');
$i=0;
foreach ($languages as $lang => $lang_desc)
{
if (!($this->exclude_current AND $lang==$current_lang))
{
if ($i>0)
{
$output .= $this->divider;
} else {
$i++;
}
if (!($this->nolink_current AND $lang==$current_lang))
{
$output.=$this->link_open_tag.'<a > $ignore_lang) {
$slash = (substr($this->uri, 0, 1) == '/' ? '' : '/');
$new_uri = '/'.$lang.$slash.$this->uri;
} else {
$new_uri = $this->uri;
}
$new_uri = substr_replace($new_uri, '', 0, 1);
if (!empty($this->slug))
{
$slugitem = (! isset($this->slug[$lang]) OR $this->slug[$lang] == "") ? '' : $this->slug[$lang];
$new_uri .= ( $slugitem == '') ? '' : '/' . $slugitem;
}
$output.= $new_uri.'"';
$output.= $this->link_extra_global;
if ($this->link_extra_lang != NULL)
{
$alt_uri .= (! isset($this->link_extra_lang) OR $this->link_extra_lang == "") ? '' : $this->link_extra_lang[$lang];
}
$output.= '>';
if ($this->lang_useimg) {
$output.= '<img >lang_img_path.$lang.'.'.$this->lang_img_ext.'" alt="'.$lang_desc.'"></a>'.$this->link_close_tag;
} else {
$output.= $lang_desc.'</a>'.$this->link_close_tag;
}
} else {
if ($this->lang_useimg) {
$output.= $this->nolink_open_tag.'<img >lang_img_path.$lang.'.'.$this->lang_img_ext.'" alt="'.$lang_desc.'">'.$this->nolink_close_tag;
} else {
$output.=$this->nolink_open_tag.$lang_desc.$this->nolink_close_tag;
}
}
}
}
return $output;
log_message('debug', "Lang_uri Class create_links end");
} }
// END Lang_uri Class
/* End of file Lang_uri.php / / Location: ./application/libraries/Lang_uri.php */ [/code]
Installation copy Lang_uri.php to application/libraries Autoload or load whenever needed.
Configuration (config.php) [code] //from ci configuration $config['language'] = "greek";
//added for multilanguage
//for URI Language Identifier ------------- //default language abbreviation $config['language_abbr'] = "el"; //set available language abbreviations $config['lang_uri_abbr'] = array("el" => "greek", "en" => "english"); //ignore this language abbreviation $config['lang_ignore'] = TRUE; // redirect on invalid language abbreviation $config['lang_redirect_url'] = $config['base_url'].$config['index_page']."el/error_404";
//for Lang_uri ------------- $config['lang_ignore_abbr'] = "el"; //language descriptions $config['lang_desc'] = array("el"=>"Ελληνικά","en" => "English"); //language use images $config['lang_useimg'] = TRUE; //or false to use only text //language use images $config['lang_img_path'] = 'img/'; //
//end of multilanguage [/code]
Parameters [quote] uri Leave blank for current url
slug pass array in the format ('lang_abbr'=>'slug-to-add')
stripsegment if needed to delete last segment (when using slug)
exclude_current don't return anything for current language
nolink_current don't return link for current language but display
link_open_tag open tag for link
link_close_tag close tag for link
nolink_open_tag open tag for no link
nolink_close_tag close tag for no link
divider divider, ie '
', leave empty for no uselink_extra_global accepts a string for global link class, ie 'class="lang_links"'
link_extra_lang array for specific lang details ('lang_abbr'=>'class="some" id="en_lang"'), leave empty for no use
lang_useimg Values TRUE/FALSE.if not set it reads setting from config file
lang_img_path path for flag imgs. needs trailing slash. path is absolute (using base_url()). empty for value from config
lang_img_ext file extension for flag imgs. imgs must have the lang_ as filename, ie en.png, el.png, etc [/quote]
Usage. Load your library [code] $this->load->library('lang_uri'); [/code]
To Simply display all language urls of current url use [code] $this->lang_uri->create_links(); [/code]
To add configuration to exclude current Parameters (passed during loading or through initialize function if you are autoloading the library). [code] $config['exclude_current'] = TRUE; $this->lang_uri->initialize($config); $this->lang_uri->create_links(); [/code]
to make the links be ie list items (common use for menus) and assign a class or style in the links [code] $config['link_open_tag'] = '
To use slugs for languages, ie news/article/post-title-in-english, news/article/post-title-in-greek Note if your current url is news/article/post-title-in-english you'll need to set stripsegment to true too.
[code] $config['stripsegment'] = TRUE; $config['slug'] = array('el' => 'post-title-in-greek', 'en' => 'post-title-in-english'); $this->lang_uri->initialize($config); $this->lang_uri->create_links(); [/code]