CodeIgniter Forums
Is my implementation of multi language okay? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Is my implementation of multi language okay? (/showthread.php?tid=66896)



Is my implementation of multi language okay? - jethro - 12-15-2016

Hi guys,

There are so many different ways to add multi-language functionality to a CI installation. I went ahead and put together something that looks like it will work and I would like some feedback.

Background:

My website purpose is to sell online English lessons to people in Europe. It is crucial that my site is multilingual.

Caveat:

I use HMVC modules to organize my site... as a result I am unable to implement /en/ or /fr/ urls.

My website has four user areas:

  1. frontend (public access)
  2. student area (login protected)
  3. teacher zone (login protected)
  4. my administration panel
The areas that need multi-lang are the frontend and student area.

Here is what I am doing:

I am storing a users choice in a session variable "language".

The default session language is set to English in the templates __constructor() like this:


PHP Code:
   /**
     * handles the default language for the template
     */
 
   public function __construct()
 
   {
 
       parent::__construct();
 
       // set language
 
       if ($this->session->userdata('language') === null) {
 
           $this->session->set_userdata('language''english');
 
       }
 
   


So when someone requests my home page I call the templates controller and the frontend method.


PHP Code:
   /**
     * the public frontend
     * @param array $data
     * @return html
     */
 
   public function frontend($data)
 
   {
 
       // get template language
 
       $this->lang->load('frontend/frontend'$this->session->userdata('language'));

 
       // load template
 
       $this->load->model('time/Time_Model');
 
       $this->load->view('frontend_template'$data);
 
   


So my language translations are of course in the Application->language folder... here is how I have it structured:

   

Because there is actually quite a bit of language that needs to be translated (e.g. footer, form validation, actual content and paragraphs, menus, blurbs) I was thinking of splitting up the translations to different files such as frontend/footer_lang.php, frontend/header_lang.php etc... and then including them all into a master "frontend_lang.php"... is that a good idea?

As for the actual implementation of the language switcher... I have a dropdown menu with my different language options:

   

So when a user selects 'french' for example, they get redirected to base_url/language/switch_language/french controller that looks like this:


PHP Code:
class Language extends MX_Controller
{
    public function switch_language($language)
    {
        $this->session->set_userdata('language'$language);
        redirect($_SERVER['HTTP_REFERER']);
    }



So basically I am just setting the user language session variable and redirecting to the last url page they were on.

I am interested to hear some feedback on my solution.

Many thanks.

Jethro.