1
\$\begingroup\$

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

Overview

I am using HMVC modules to organize my site and my website has four main user areas:

  1. Frontend (public access)
  2. Student area (login protected)
  3. Teacher zone (login protected)
  4. My administration panel

The areas that need multilang functionality are the frontend and student area.

Here is what I am doing

I am storing a user's language choice in a session variable called "language".

The default session language is set to English in the template module (which renders HTML templates for the frontend, teacher zone, student area etc..) __constructor() like this:

/**
 * 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.

/**
 * 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);
}

My language translations are of course in the application->language folder... here is how I have it structured:

enter image description here

As for the actual implementation of the language switcher, I have a drop-down menu with my different language options:

enter image description here

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

class Language extends MX_Controller
{
    /**
     * url: /language/switch_language
     * @param string $language
     */
    public function switch_language($language)
    {
        $this->session->set_userdata('language', $language);
        redirect($_SERVER['HTTP_REFERER']);
    }
}

This just sets the user language session variable and redirects to the last URL page they were on.

In the language files (suffixed with _lang.php) there is just one variable ($lang) which has a huge array of language translation key => value pairs:

// overview
$lang['book_overview'] = "Overview";
$lang['book_course_title'] = "Course";
$lang['book_duration_of_each_class'] = "Duration of Each Class";
$lang['book_minutes'] = "Minutes";
$lang['book_number_of_classes'] = "Number of Classes";
$lang['book_order_key'] = "Order Key";

To echo out the language string I simply replace the sentence with this code:

<?php echo $this->lang->line('book_duration_of_each_class'); ?>

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? Are there any potential pitfalls to my implementation?

\$\endgroup\$
  • \$\begingroup\$ Can you show your implementation. You have some good contextula background here, but is hard to give review of you implementation of frontend_lang.php and similar without actually seeing that code as well. \$\endgroup\$ – Mike Brant Dec 16 '16 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.