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:
frontend (public access)
student area (login protected)
teacher zone (login protected)
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'));
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.