php - Codeigniter keep selected language -
i have codeigniter application default language english , have tow anchor switch language english.
the problem when switch language english
eg:arabic
same page changing when go page english again.
my_lang.php
<?php (defined('basepath')) or exit('no direct script access allowed'); // originaly codeigniter i18n library jérome jaglale // http://maestric.com/en/doc/php/codeigniter_i18n //modified tobin thomas class my_lang extends ci_lang { /************************************************** configuration ***************************************************/ // add languages here private $languages = array( 'en' => 'english', 'ar' => 'arabic' ); // special uris (not localized) private $special = array ( 'admin', 'assets', 'editor' ); // redirect if no language in uri private $uri; private $default_uri; private $lang_code; /**************************************************/ function my_lang() { parent::__construct(); global $cfg; global $uri; global $rtr; $this->uri = $uri->uri_string(); $this->default_uri = $rtr->default_controller; $uri_segment = $this->get_uri_lang($this->uri); $this->lang_code = $uri_segment['lang'] ; $url_ok = false; if ((!empty($this->lang_code)) && (array_key_exists($this->lang_code, $this->languages))) { $language = $this->languages[$this->lang_code]; $cfg->set_item('language', $language); $url_ok = true; } if ((!$url_ok) && (!$this->is_special($uri_segment['parts'][0]))) // special uri -> no redirect { // set default language $cfg->set_item('language', $this->languages[$this->default_lang()]); $uri = (!empty($this->uri)) ? $this->uri: $this->default_uri; $uri = ($uri[0] != '/') ? '/'.$uri : $uri; $new_url = $cfg->config['base_url'].$this->default_lang().$uri; header("location: " . $new_url, true, 302); exit; } } // current language // ex: return 'en' if language in ci config 'english' function lang() { global $cfg; $language = $cfg->item('language'); $lang = array_search($language, $this->languages); if ($lang) { return $lang; } return null; // should not happen } function is_special($lang_code) { if ((!empty($lang_code)) && (in_array($lang_code, $this->special))) return true; else return false; } function switch_uri($lang) { if ((!empty($this->uri)) && (array_key_exists($lang, $this->languages))) { if ($uri_segment = $this->get_uri_lang($this->uri)) { $uri_segment['parts'][0] = $lang; $uri = implode('/',$uri_segment['parts']); } else { $uri = $lang.'/'.$this->uri; } } return $uri; } //check if language exists //when true returns array lang abbreviation + rest function get_uri_lang($uri = '') { if (!empty($uri)) { $uri = ($uri[0] == '/') ? substr($uri, 0): $uri; $uri_expl = explode('/', $uri, 2); $uri_segment['lang'] = null; $uri_segment['parts'] = $uri_expl; if (array_key_exists($uri_expl[0], $this->languages)) { $uri_segment['lang'] = $uri_expl[0]; } return $uri_segment; } else return false; } // default language: first element of $this->languages function default_lang() { $browser_lang = !empty($_server['http_accept_language']) ? strtok(strip_tags($_server['http_accept_language']), ',') : ''; $browser_lang = substr($browser_lang, 0,2); return (!empty($browser_lang) && array_key_exists($browser_lang, $this->languages)) ? $browser_lang: 'en'; } // add language segment $uri (if appropriate) function localized($uri) { if (!empty($uri)) { $uri_segment = $this->get_uri_lang($uri); if (!$uri_segment['lang']) { if ((!$this->is_special($uri_segment['parts'][0])) && (!preg_match('/(.+)\.[a-za-z0-9]{2,4}$/', $uri))) { $uri = $this->lang() . '/' . $uri; } } } return $uri; } } // end my_lang class /* end of file my_lang.php */ /* location: ./application/core/my_lang.php */
and view.php
<div class="lang-bar pull-left"> <a href="<?php echo site_url($this->lang->switch_uri('ar'));?>" class="btn btn-success">ar</a> <a href="<?php echo site_url($this->lang->switch_uri('en'));?>" class="btn btn-success">en</a> </div>
so how keep language until close page
i don't know how library works, maybe don't load session library keep language values accross pages. here's lib use: https://github.com/devtime-share/codeigniter-kitlang
Comments
Post a Comment