(created at: March 17, 2015; last update: December 7, 2015)
Well… This is getting rather complicated… After we’ve “personalized” the controllers and methods for a multilanguage site, it’s time to make sure we won’t do something wrong once we start developing it. So why not set everything that is related to the language inside our MY_Controller.
We will retrieve the languages from our language_model we’ve created in Step 4.1 – Create the admin area for a multilanguage site in CodeIgniter. We will use the language slug as a key in order to identify the language. Also, we will need ‘language_directory’, which will help us identify the language file inside ‘application/language’ directory. ‘Default’ will simply tell us if that particular language is the default language (1 = default). We need to find out if the language is the default language because we don’t need that language’s slug to pe appended to our urls if it’s the default language.
Now, let me show you MY_Controller, and after that I will explain you what were my thoughts. Bare in mind that this is a continuation of what we’ve already written in our MY_Controller file:
class MY_Controller extends CI_Controller
protected $data = array();
protected $langs = array();
// First of all let's see what languages we have and also get the default language
$available_languages = $this->language_model->get_all();
foreach($available_languages as $lang)
$this->langs[$lang->slug] = array('id'=>$lang->id,'slug'=>$lang->slug,'language_directory'=>$lang->language_directory,'language_code'=>$lang->language_code,'default'=>$lang->default);
if($lang->default == '1') $this->default_lang = $lang->slug;
// Verify if we have a language set in the URL;
$lang_slug = $this->uri->segment(1);
// If we do, and we have that languages in our set of languages we store the language slug in the session
if(isset($lang_slug) && array_key_exists($lang_slug, $this->langs))
$this->current_lang = $lang_slug;
$_SESSION['set_language'] = $lang_slug;
// If not, we look to see if the language session variable is set
$this->current_lang = $_SESSION['set_language'];
// If not, we set the language session to the default language
$this->current_lang = $this->default_lang;
$_SESSION['set_language'] = $this->default_lang;
// Now we store the languages as a $data key, just in case we need them in our views
$this->data['langs'] = $this->langs;
// Also let's have our current language in a $data key
$this->data['current_lang'] = $this->langs[$this->current_lang];
// For links inside our views we only need the lang slug. If the current language is the default language we don't need to append the language slug to our links
if($this->current_lang != $this->default_lang)
$this->data['lang_slug'] = $this->current_lang.'/';
$this->data['lang_slug'] = '';
$this->data['page_title'] = 'CI App';
$this->data['page_description'] = 'CI_App';
$this->data['before_head'] = '';
$this->data['before_body'] = '';
protected function render($the_view = NULL, $template = 'master')
if($template == 'json' || $this->input->is_ajax_request())
$this->data['the_view_content'] = (is_null($the_view)) ? '' : $this->load->view($the_view, $this->data, TRUE);
$this->load->view('templates/' . $template . '_view', $this->data);
As you can see, we’ve started by defining some properties: the $langs, the $current_lang, and the $default_lang. The $langs property will contain the languages we’ve talked about earlier. The $current_lang will hold the current language data, and the $default_lang will contain data regarding the default language. Inside the comments starting with line … I left you something that we will use later, after we’ve added the languages module inside our admin area.
Now… according to the inline comments you can see that we will search for the slug inside the urls. If there is a slug and that slug exists in our defined languages array, we will set the $current_lang property to the slug. Also, we will keep that in our session, so make sure you’ve autoloaded the sessions library.
We will pass all our defined languages to a $this->data[‘langs’] so that it will be available to our views. Who knows, maybe later we will create a menu where we will allow the visitors to select the language. Also we will pass the view the current language.
I’ve made a $this->data[‘lang_slug’] because we need to be brief when we will create links. This way, when we will create links we will simply do something like: echo site_url($lang_slug.’/controller/method’);
If the site were to be in the default language, that won’t affect our links.
And that’s about it… Now let me give you an archive with our application. It also contains the sql file, the CI and everything else I might have missed (the admin credentials are username: administrator and password: password; don’t forget to change the database connection configuration)…