Step 4.4 – Returning to our MY_Controller. Doing everything that needs to be done for better working with languages

Created at: March 17, 2015; Last update: December 7, 2015

(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:


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)…

7 thoughts on “Step 4.4 – Returning to our MY_Controller. Doing everything that needs to be done for better working with languages

  1. Rogerio Taques

    Hey there!

    Your approach of how to implement a multilingual CI website is awesome! Congrats.

    Let me just ask you if have you ever tried to implement it using the CI_Lang library, instead of store your language keys and translations on database?



    1. avenirer Post author

      That’s exactly the point with MY_Controller. What I did in MY_Controller is exactly for being used with the CI_Lang. Now we can use the language library to retrieve whatever we want.

      For example, we can have a single view of a page for two or more than two languages. As you can see, at line 41 we’ve defined $this->data[‘current_lang’]. That is an array containing all the data needed to work with the current language (the language that the visitor selected).

      We start by creating a language file, let’s say blog_lang.php, inside the application/language/english, application/language/french or whatever other languages we might have. Then, in the constructor of the controllers, or even in the MY_Controller, we load that language file with:

      $language = $_SESSION[‘set_language’];
      $idiom = $this->langs[$language][‘language_directory’];
      $this->lang->load(‘blog’, $idiom);

      And after that, in the views we simply replace our strings with what we’ve defined inside the language files. Instead of writing:

      echo ‘This is a great page’;

      …we write:

      echo $this->lang->line(‘the_teaser’);

      And we define the ‘the_teaser’ in every language file:

      $lang[‘the_teaser’] = ‘This is a great page’;

      This way, when the visitor changes the language, he will be served with that line in his language.

      I guess I need to do another tutorial after this one, don’t I?

  2. Federico

    “I guess I need to do another tutorial after this one, don’t I?”
    It would be greta to better understanding 🙂

  3. Kiwiguy1967

    Missing single-quote on line #19 (just before language_directory means this version of MY_Controller will never work.

    $this->langs[$lang->slug] = array(‘id’=>$lang->id,’slug’=>$lang->slug,language_directory’=>$lang->language_directory,’language_code’=>$lang->language_code,’default’=>$lang->default);

    Should be:
    $this->langs[$lang->slug] = array(‘id’=>$lang->id,’slug’=>$lang->slug,’language_directory’=>$lang->language_directory,’language_code’=>$lang->language_code,’default’=>$lang->default);

  4. Carlos

    Hi ,

    I’m developping a multilingual site using your tutorial. Thanks.

    I have created my owns libraries, everything works correctly till I use ajax. The ajax method works correctly and do what i want, but “regenerate” the session variable with default language.

    For example, if I choose English, everything is ok, and load the langs files properly, but when i try to comment o rate (ajax), the libraries load the langs files again and the session variable contain the default language.

    i read this, but i’m can’t find the solution.

    Could you help me out?

    Thanks in advance.


Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.